-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c29fd23
commit 89d145b
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
app/src/test/java/com/kylecorry/trail_sense/shared/grouping/mapping/GroupMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.kylecorry.trail_sense.shared.grouping.mapping | ||
|
||
import com.kylecorry.trail_sense.shared.grouping.Groupable | ||
import com.kylecorry.trail_sense.shared.grouping.persistence.IGroupLoader | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
internal class GroupMapperTest { | ||
|
||
@Test | ||
fun mapGroup() = runBlocking { | ||
// Assert | ||
|
||
val loader = mock<IGroupLoader<Groupable>>() | ||
val mapper = MockMapper(loader) | ||
|
||
val children = listOf( | ||
item(2), | ||
group(3), | ||
item(4), | ||
group(5) | ||
) | ||
|
||
whenever(loader.getChildren(1, null)).thenReturn(children) | ||
|
||
// Act | ||
val value = mapper.map(group(1)) | ||
|
||
// Assert | ||
assertEquals(8L, value) | ||
} | ||
|
||
@Test | ||
fun mapItem() = runBlocking { | ||
// Assert | ||
val loader = mock<IGroupLoader<Groupable>>() | ||
val mapper = MockMapper(loader) | ||
|
||
// Act | ||
val value = mapper.map(item(1)) | ||
|
||
// Assert | ||
assertEquals(2L, value) | ||
} | ||
|
||
private fun item(id: Long): Groupable { | ||
val item = mock<Groupable>() | ||
whenever(item.id).thenReturn(id) | ||
whenever(item.isGroup).thenReturn(false) | ||
return item | ||
} | ||
|
||
private fun group(id: Long): Groupable { | ||
val group = mock<Groupable>() | ||
whenever(group.id).thenReturn(id) | ||
whenever(group.isGroup).thenReturn(true) | ||
return group | ||
} | ||
|
||
class MockMapper(override val loader: IGroupLoader<Groupable>) : | ||
GroupMapper<Groupable, Long, Long?>() { | ||
override suspend fun getValue(item: Groupable): Long { | ||
return item.id | ||
} | ||
|
||
override suspend fun aggregate(values: List<Long>): Long? { | ||
return values.maxOrNull()?.let { it * 2 } | ||
} | ||
} | ||
|
||
} |