Skip to content

Commit 75618d9

Browse files
committed
test(RepoItemMapper): add RepoItemMapperTest
1 parent e5abf33 commit 75618d9

File tree

3 files changed

+128
-29
lines changed

3 files changed

+128
-29
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.hoc081098.github_search_kmm.data
2+
3+
import com.hoc081098.github_search_kmm.data.remote.response.RepoItemsSearchResponse
4+
import com.hoc081098.github_search_kmm.domain.model.ArgbColor
5+
import com.hoc081098.github_search_kmm.domain.model.Owner
6+
import com.hoc081098.github_search_kmm.domain.model.RepoItem
7+
8+
internal fun RepoItemsSearchResponse.toRepoItemsList(colors: Map<String, ArgbColor>): List<RepoItem> =
9+
items
10+
?.map { it.toRepoItem(colors) }
11+
?: emptyList()
12+
13+
private fun RepoItemsSearchResponse.Item.toRepoItem(colors: Map<String, ArgbColor>): RepoItem =
14+
RepoItem(
15+
id = id,
16+
fullName = fullName,
17+
language = language,
18+
starCount = stargazersCount,
19+
name = name,
20+
repoDescription = description,
21+
languageColor = language?.let { colors[it] },
22+
htmlUrl = htmlUrl,
23+
owner = owner.toOwner(),
24+
updatedAt = updatedAt,
25+
)
26+
27+
private fun RepoItemsSearchResponse.Item.Owner.toOwner(): Owner = Owner(
28+
id = id,
29+
username = login,
30+
avatar = avatarUrl
31+
)

shared/src/commonMain/kotlin/com/hoc081098/github_search_kmm/data/RepoItemRepositoryImpl.kt

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ package com.hoc081098.github_search_kmm.data
33
import com.hoc081098.github_search_kmm.AppCoroutineDispatchers
44
import com.hoc081098.github_search_kmm.data.remote.GithubLanguageColorApi
55
import com.hoc081098.github_search_kmm.data.remote.RepoItemApi
6-
import com.hoc081098.github_search_kmm.data.remote.response.RepoItemsSearchResponse
7-
import com.hoc081098.github_search_kmm.domain.model.ArgbColor
8-
import com.hoc081098.github_search_kmm.domain.model.Owner
9-
import com.hoc081098.github_search_kmm.domain.model.RepoItem
106
import com.hoc081098.github_search_kmm.domain.repository.RepoItemRepository
117
import com.hoc081098.github_search_kmm.utils.parZipEither
128
import io.github.aakira.napier.Napier
@@ -53,28 +49,3 @@ internal open class RepoItemRepositoryImpl(
5349
repoItemsSearchResponse.toRepoItemsList(colors)
5450
}
5551
}
56-
57-
internal fun RepoItemsSearchResponse.toRepoItemsList(colors: Map<String, ArgbColor>): List<RepoItem> =
58-
items
59-
?.map { it.toRepoItem(colors) }
60-
?: emptyList()
61-
62-
private fun RepoItemsSearchResponse.Item.toRepoItem(colors: Map<String, ArgbColor>): RepoItem =
63-
RepoItem(
64-
id = id,
65-
fullName = fullName,
66-
language = language,
67-
starCount = stargazersCount,
68-
name = name,
69-
repoDescription = description,
70-
languageColor = language?.let { colors[it] },
71-
htmlUrl = htmlUrl,
72-
owner = owner.toOwner(),
73-
updatedAt = updatedAt,
74-
)
75-
76-
private fun RepoItemsSearchResponse.Item.Owner.toOwner(): Owner = Owner(
77-
id = id,
78-
username = login,
79-
avatar = avatarUrl
80-
)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.hoc081098.github_search_kmm.data
2+
3+
import arrow.core.getOrHandle
4+
import com.hoc081098.github_search_kmm.data.remote.response.RepoItemsSearchResponse
5+
import com.hoc081098.github_search_kmm.domain.model.ArgbColor
6+
import com.hoc081098.github_search_kmm.domain.model.Owner
7+
import com.hoc081098.github_search_kmm.domain.model.RepoItem
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
import kotlin.test.fail
11+
import kotlinx.datetime.Instant
12+
13+
class RepoItemMapperTest {
14+
@Test
15+
fun `test RepoItemsSearchResponse_toRepoItemsList`() {
16+
val response = RepoItemsSearchResponse(
17+
totalCount = 10,
18+
incompleteResults = false,
19+
items = listOf(
20+
RepoItemsSearchResponse.Item(
21+
id = 1,
22+
name = "name 1",
23+
fullName = "fullName 1",
24+
owner = RepoItemsSearchResponse.Item.Owner(
25+
login = "owner1",
26+
id = 0,
27+
avatarUrl = "owner1/avatar"
28+
),
29+
htmlUrl = "url/1",
30+
description = "description 1",
31+
updatedAt = Instant.fromEpochMilliseconds(1),
32+
stargazersCount = 10,
33+
language = "Kotlin"
34+
),
35+
RepoItemsSearchResponse.Item(
36+
id = 2,
37+
name = "name 2",
38+
fullName = "fullName 2",
39+
owner = RepoItemsSearchResponse.Item.Owner(
40+
login = "owner2",
41+
id = 2,
42+
avatarUrl = "owner2/avatar"
43+
),
44+
htmlUrl = "url/2",
45+
description = "description 2",
46+
updatedAt = Instant.fromEpochMilliseconds(2),
47+
stargazersCount = 20,
48+
language = "Scala"
49+
)
50+
)
51+
)
52+
53+
val colors = mapOf(
54+
"Kotlin" to ArgbColor.parse("#F18E33").getOrHandle { fail(it) },
55+
)
56+
57+
val items = response.toRepoItemsList(colors)
58+
59+
assertEquals(
60+
expected = listOf(
61+
RepoItem(
62+
id = 1,
63+
name = "name 1",
64+
fullName = "fullName 1",
65+
owner = Owner(
66+
id = 0,
67+
username = "owner1",
68+
avatar = "owner1/avatar"
69+
),
70+
htmlUrl = "url/1",
71+
repoDescription = "description 1",
72+
updatedAt = Instant.fromEpochMilliseconds(1),
73+
starCount = 10,
74+
language = "Kotlin",
75+
languageColor = colors["Kotlin"]!!
76+
),
77+
RepoItem(
78+
id = 2,
79+
name = "name 2",
80+
fullName = "fullName 2",
81+
owner = Owner(
82+
id = 2,
83+
username = "owner2",
84+
avatar = "owner2/avatar"
85+
),
86+
htmlUrl = "url/2",
87+
repoDescription = "description 2",
88+
updatedAt = Instant.fromEpochMilliseconds(2),
89+
starCount = 20,
90+
language = "Scala",
91+
languageColor = null
92+
)
93+
),
94+
actual = items
95+
)
96+
}
97+
}

0 commit comments

Comments
 (0)