Skip to content

Commit 7891aee

Browse files
committed
test: update
1 parent 9c2e416 commit 7891aee

File tree

10 files changed

+130
-70
lines changed

10 files changed

+130
-70
lines changed

shared/src/commonMain/kotlin/com/hoc081098/github_search_kmm/data/remote/KtorGithubLanguageColorApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal open class KtorGithubLanguageColorApi(
3333
.traverse { (k, v) ->
3434
ArgbColor
3535
.parse(v)
36-
.mapLeft { IllegalStateException(it) }
36+
.mapLeft(::IllegalStateException)
3737
.map { k to it }
3838
}
3939
}

shared/src/commonTest/kotlin/com/hoc081098/github_search_kmm/data/RepoItemMapperTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.hoc081098.github_search_kmm.data
22

3-
import arrow.core.getOrHandle
43
import com.hoc081098.github_search_kmm.data.remote.response.RepoItemsSearchResponse
54
import com.hoc081098.github_search_kmm.domain.model.ArgbColor
65
import com.hoc081098.github_search_kmm.domain.model.Owner
76
import com.hoc081098.github_search_kmm.domain.model.RepoItem
7+
import com.hoc081098.github_search_kmm.getOrThrow
88
import kotlin.test.Test
99
import kotlin.test.assertEquals
10-
import kotlin.test.fail
1110
import kotlinx.datetime.Instant
1211

1312
class RepoItemMapperTest {
@@ -51,7 +50,7 @@ class RepoItemMapperTest {
5150
)
5251

5352
val colors = mapOf(
54-
"Kotlin" to ArgbColor.parse("#F18E33").getOrHandle { fail(it) },
53+
"Kotlin" to ArgbColor.parse("#F18E33").getOrThrow,
5554
)
5655

5756
val items = response.toRepoItemsList(colors)

shared/src/commonTest/kotlin/com/hoc081098/github_search_kmm/data/RepoItemRepositoryImplTest.kt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.hoc081098.github_search_kmm.data
22

3-
import arrow.core.getOrHandle
43
import arrow.core.left
54
import arrow.core.right
65
import com.hoc081098.github_search_kmm.TestAntilog
@@ -10,6 +9,8 @@ import com.hoc081098.github_search_kmm.data.remote.RepoItemApi
109
import com.hoc081098.github_search_kmm.data.remote.response.RepoItemsSearchResponse
1110
import com.hoc081098.github_search_kmm.domain.model.AppError
1211
import com.hoc081098.github_search_kmm.domain.model.ArgbColor
12+
import com.hoc081098.github_search_kmm.getOrThrow
13+
import com.hoc081098.github_search_kmm.leftValueOrThrow
1314
import io.github.aakira.napier.Napier
1415
import io.mockative.Mock
1516
import io.mockative.given
@@ -22,7 +23,6 @@ import kotlin.test.BeforeTest
2223
import kotlin.test.Test
2324
import kotlin.test.assertEquals
2425
import kotlin.test.assertIs
25-
import kotlin.test.fail
2626
import kotlinx.coroutines.Dispatchers
2727
import kotlinx.coroutines.test.resetMain
2828
import kotlinx.coroutines.test.runTest
@@ -88,7 +88,7 @@ class RepoItemRepositoryImplTest {
8888
val either = repoItemRepositoryImpl.searchRepoItems(term, page)
8989
assertEquals(
9090
FAKE_REPO_ITEMS,
91-
either.getOrHandle { throw it }
91+
either.getOrThrow
9292
)
9393

9494
verify(repoItemApi)
@@ -117,12 +117,7 @@ class RepoItemRepositoryImplTest {
117117
.then { AppError.ApiException.NetworkException(error) }
118118

119119
val either = repoItemRepositoryImpl.searchRepoItems(term, page)
120-
assertIs<AppError.ApiException.NetworkException>(
121-
either.fold(
122-
ifLeft = { it },
123-
ifRight = { fail("Expected Left but got Right") }
124-
)
125-
)
120+
assertIs<AppError.ApiException.NetworkException>(either.leftValueOrThrow)
126121

127122
verify(repoItemApi)
128123
.coroutine { searchRepoItems(term, page) }
@@ -153,12 +148,7 @@ class RepoItemRepositoryImplTest {
153148
.then { AppError.ApiException.NetworkException(error) }
154149

155150
val either = repoItemRepositoryImpl.searchRepoItems(term, page)
156-
assertIs<AppError.ApiException.NetworkException>(
157-
either.fold(
158-
ifLeft = { it },
159-
ifRight = { fail("Expected Left but got Right") }
160-
)
161-
)
151+
assertIs<AppError.ApiException.NetworkException>(either.leftValueOrThrow)
162152

163153
verify(repoItemApi)
164154
.coroutine { searchRepoItems(term, page) }
@@ -175,13 +165,13 @@ class RepoItemRepositoryImplTest {
175165
val FAKE_GITHUB_LANGUAGE_COLORS: Map<String, ArgbColor> = mapOf(
176166
"Kotlin" to ArgbColor
177167
.parse("#814CCC")
178-
.getOrHandle { error(it) },
168+
.getOrThrow,
179169
"Java" to ArgbColor
180170
.parse("#B07219")
181-
.getOrHandle { error(it) },
171+
.getOrThrow,
182172
"JavaScript" to ArgbColor
183173
.parse("#F1E05A")
184-
.getOrHandle { error(it) },
174+
.getOrThrow,
185175
)
186176

187177
val FAKE_REPO_ITEMS_SEARCH_RESPONSE = RepoItemsSearchResponse(

shared/src/commonTest/kotlin/com/hoc081098/github_search_kmm/data/remote/KtorGithubLanguageColorApiTest.kt

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.hoc081098.github_search_kmm.data.remote
22

3-
import arrow.core.getOrHandle
43
import com.hoc081098.github_search_kmm.TestAntilog
54
import com.hoc081098.github_search_kmm.TestAppCoroutineDispatchers
65
import com.hoc081098.github_search_kmm.domain.model.ArgbColor
6+
import com.hoc081098.github_search_kmm.getOrThrow
7+
import com.hoc081098.github_search_kmm.leftValueOrThrow
78
import io.github.aakira.napier.Napier
89
import io.ktor.client.HttpClient
910
import io.ktor.client.engine.mock.MockEngine
1011
import io.ktor.client.engine.mock.MockRequestHandler
1112
import io.ktor.client.engine.mock.respond
13+
import io.ktor.client.engine.mock.respondError
1214
import io.ktor.http.ContentType
1315
import io.ktor.http.HttpHeaders
1416
import io.ktor.http.HttpMethod
@@ -19,7 +21,7 @@ import kotlin.test.AfterTest
1921
import kotlin.test.BeforeTest
2022
import kotlin.test.Test
2123
import kotlin.test.assertEquals
22-
import kotlin.test.fail
24+
import kotlin.test.assertIs
2325
import kotlinx.coroutines.channels.Channel
2426
import kotlinx.coroutines.test.runTest
2527

@@ -95,14 +97,73 @@ class KtorGithubLanguageColorApiTest {
9597
}
9698

9799
val either = ktorGithubLanguageColorApi.getColors()
98-
val colors = either.getOrHandle { throw it }
100+
val colors = either.getOrThrow
99101

100102
assertEquals(
101103
mapOf(
102-
"ABAP CDS" to ArgbColor.parse("#555e25").getOrHandle { fail(it) },
103-
"Agda" to ArgbColor.parse("#315665").getOrHandle { fail(it) },
104+
"ABAP CDS" to ArgbColor.parse("#555e25").getOrThrow,
105+
"Agda" to ArgbColor.parse("#315665").getOrThrow,
104106
),
105107
colors,
106108
)
107109
}
110+
111+
@Test
112+
fun `getColors returns a Right WHEN httpClient returns a successful response and it is invalid`() =
113+
runTest(testAppCoroutineDispatchers.testCoroutineDispatcher) {
114+
val colorsResponse = """
115+
|{
116+
| "ABAP CDS": {
117+
| "color": "#qwerty",
118+
| "url": "https://github.com/trending?l=ABAP-CDS"
119+
| },
120+
| "Ada": {
121+
| "url": "https://github.com/trending?l=Ada"
122+
| },
123+
| "Agda": {
124+
| "color": "#315665",
125+
| "url": "https://github.com/trending?l=Agda"
126+
| }
127+
|}
128+
""".trimMargin("|")
129+
130+
handlerChannel.trySend { request ->
131+
when (request.url.encodedPath) {
132+
"" -> {
133+
check(request.method == HttpMethod.Get)
134+
135+
respond(
136+
content = colorsResponse,
137+
status = HttpStatusCode.OK,
138+
headers = headersOf(
139+
HttpHeaders.ContentType,
140+
ContentType.Application.Json.toString()
141+
)
142+
)
143+
}
144+
else -> error("Unhandled request ${request.url}")
145+
}
146+
}
147+
148+
val either = ktorGithubLanguageColorApi.getColors()
149+
assertIs<IllegalStateException>(either.leftValueOrThrow)
150+
}
151+
152+
@Test
153+
fun `getColors returns a Left WHEN httpClient returns a failure response`() =
154+
runTest(testAppCoroutineDispatchers.testCoroutineDispatcher) {
155+
handlerChannel.trySend { request ->
156+
when (request.url.encodedPath) {
157+
"" -> {
158+
check(request.method == HttpMethod.Get)
159+
160+
respondError(HttpStatusCode.InternalServerError)
161+
}
162+
else -> error("Unhandled request ${request.url}")
163+
}
164+
}
165+
166+
val either = ktorGithubLanguageColorApi.getColors()
167+
either.leftValueOrThrow
168+
}
108169
}

shared/src/commonTest/kotlin/com/hoc081098/github_search_kmm/data/remote/KtorRepoItemApiTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.hoc081098.github_search_kmm.data.remote
22

3-
import arrow.core.getOrHandle
4-
import arrow.core.identity
53
import com.hoc081098.github_search_kmm.TestAntilog
64
import com.hoc081098.github_search_kmm.TestAppCoroutineDispatchers
5+
import com.hoc081098.github_search_kmm.getOrThrow
6+
import com.hoc081098.github_search_kmm.leftValueOrThrow
77
import com.hoc081098.github_search_kmm.readTextResource
88
import io.github.aakira.napier.Napier
99
import io.ktor.client.HttpClient
@@ -20,7 +20,6 @@ import kotlin.test.AfterTest
2020
import kotlin.test.BeforeTest
2121
import kotlin.test.Test
2222
import kotlin.test.assertEquals
23-
import kotlin.test.fail
2423
import kotlinx.coroutines.channels.Channel
2524
import kotlinx.coroutines.test.runTest
2625
import kotlinx.serialization.decodeFromString
@@ -91,7 +90,7 @@ class KtorRepoItemApiTest {
9190

9291
assertEquals(
9392
json.decodeFromString(responseString),
94-
either.getOrHandle { throw it }
93+
either.getOrThrow
9594
)
9695
}
9796

@@ -123,6 +122,6 @@ class KtorRepoItemApiTest {
123122
page = 1
124123
)
125124

126-
either.fold(ifLeft = ::identity, ifRight = { fail("Expected Left, but got Right") })
125+
either.leftValueOrThrow
127126
}
128127
}

shared/src/commonTest/kotlin/com/hoc081098/github_search_kmm/domain/model/ArgbColorTest.kt

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.hoc081098.github_search_kmm.domain.model
22

3-
import arrow.core.getOrHandle
43
import arrow.core.left
4+
import com.hoc081098.github_search_kmm.getOrThrow
55
import com.hoc081098.github_search_kmm.readTextResource
66
import kotlin.test.Test
77
import kotlin.test.assertEquals
88
import kotlin.test.assertNotEquals
99
import kotlin.test.assertTrue
10-
import kotlin.test.fail
1110
import kotlinx.serialization.decodeFromString
1211
import kotlinx.serialization.json.Json
1312

@@ -62,48 +61,48 @@ class ArgbColorTest {
6261
@Test
6362
fun `ArgbColor ==`() {
6463
assertEquals(
65-
ArgbColor.parse("#000000").getOrHandle { fail(it) },
66-
ArgbColor.parse("#000000").getOrHandle { fail(it) }
64+
ArgbColor.parse("#000000").getOrThrow,
65+
ArgbColor.parse("#000000").getOrThrow
6766
)
6867
assertEquals(
69-
ArgbColor.parse("#112233").getOrHandle { fail(it) },
70-
ArgbColor.parse("#123").getOrHandle { fail(it) }
68+
ArgbColor.parse("#112233").getOrThrow,
69+
ArgbColor.parse("#123").getOrThrow
7170
)
7271
assertEquals(
73-
ArgbColor.parse("#FF112233").getOrHandle { fail(it) },
74-
ArgbColor.parse("#123").getOrHandle { fail(it) }
72+
ArgbColor.parse("#FF112233").getOrThrow,
73+
ArgbColor.parse("#123").getOrThrow
7574
)
7675
assertEquals(
77-
ArgbColor.parse("#ff112233").getOrHandle { fail(it) },
78-
ArgbColor.parse("#123").getOrHandle { fail(it) }
76+
ArgbColor.parse("#ff112233").getOrThrow,
77+
ArgbColor.parse("#123").getOrThrow
7978
)
8079
assertNotEquals(
81-
ArgbColor.parse("#FF112233").getOrHandle { fail(it) },
82-
ArgbColor.parse("#12112233").getOrHandle { fail(it) }
80+
ArgbColor.parse("#FF112233").getOrThrow,
81+
ArgbColor.parse("#12112233").getOrThrow
8382
)
8483
}
8584

8685
@Test
8786
fun `ArgbColor hashCode`() {
8887
assertEquals(
89-
ArgbColor.parse("#000000").getOrHandle { fail(it) }.hashCode(),
90-
ArgbColor.parse("#000000").getOrHandle { fail(it) }.hashCode()
88+
ArgbColor.parse("#000000").getOrThrow.hashCode(),
89+
ArgbColor.parse("#000000").getOrThrow.hashCode()
9190
)
9291
assertEquals(
93-
ArgbColor.parse("#112233").getOrHandle { fail(it) }.hashCode(),
94-
ArgbColor.parse("#123").getOrHandle { fail(it) }.hashCode()
92+
ArgbColor.parse("#112233").getOrThrow.hashCode(),
93+
ArgbColor.parse("#123").getOrThrow.hashCode()
9594
)
9695
assertEquals(
97-
ArgbColor.parse("#FF112233").getOrHandle { fail(it) }.hashCode(),
98-
ArgbColor.parse("#123").getOrHandle { fail(it) }.hashCode()
96+
ArgbColor.parse("#FF112233").getOrThrow.hashCode(),
97+
ArgbColor.parse("#123").getOrThrow.hashCode()
9998
)
10099
assertEquals(
101-
ArgbColor.parse("#ff112233").getOrHandle { fail(it) }.hashCode(),
102-
ArgbColor.parse("#123").getOrHandle { fail(it) }.hashCode()
100+
ArgbColor.parse("#ff112233").getOrThrow.hashCode(),
101+
ArgbColor.parse("#123").getOrThrow.hashCode()
103102
)
104103
assertNotEquals(
105-
ArgbColor.parse("#FF112233").getOrHandle { fail(it) }.hashCode(),
106-
ArgbColor.parse("#12112233").getOrHandle { fail(it) }.hashCode()
104+
ArgbColor.parse("#FF112233").getOrThrow.hashCode(),
105+
ArgbColor.parse("#12112233").getOrThrow.hashCode()
107106
)
108107
}
109108

@@ -131,7 +130,7 @@ class ArgbColorTest {
131130

132131
private fun assertArgbColor(hex: String, a: Float, r: Float, g: Float, b: Float) {
133132
ArgbColor.parse(hex)
134-
.getOrHandle { error(it) }
133+
.getOrThrow
135134
.argb
136135
.run {
137136
assertEquals(

shared/src/commonTest/kotlin/com/hoc081098/github_search_kmm/domain/usecase/SearchRepoItemsUseCaseTest.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.hoc081098.github_search_kmm.domain.usecase
22

3-
import arrow.core.getOrHandle
43
import arrow.core.left
54
import arrow.core.right
65
import com.hoc081098.github_search_kmm.domain.model.AppError
76
import com.hoc081098.github_search_kmm.domain.repository.RepoItemRepository
87
import com.hoc081098.github_search_kmm.genRepoItems
8+
import com.hoc081098.github_search_kmm.getOrThrow
9+
import com.hoc081098.github_search_kmm.leftValueOrThrow
910
import io.mockative.Mock
1011
import io.mockative.given
1112
import io.mockative.mock
@@ -15,7 +16,6 @@ import kotlin.test.AfterTest
1516
import kotlin.test.BeforeTest
1617
import kotlin.test.Test
1718
import kotlin.test.assertEquals
18-
import kotlin.test.fail
1919
import kotlinx.coroutines.test.runTest
2020

2121
class SearchRepoItemsUseCaseTest {
@@ -49,7 +49,7 @@ class SearchRepoItemsUseCaseTest {
4949

5050
assertEquals(
5151
items,
52-
either.getOrHandle { throw it }
52+
either.getOrThrow
5353
)
5454
verify(repoItemRepository)
5555
.coroutine { searchRepoItems(term, page) }
@@ -68,10 +68,7 @@ class SearchRepoItemsUseCaseTest {
6868

6969
val either = searchRepoItemsUseCase(term, page)
7070

71-
assertEquals(
72-
error,
73-
either.fold(ifLeft = { it }, ifRight = { fail("Expected Left but got Right") })
74-
)
71+
assertEquals(error, either.leftValueOrThrow)
7572
verify(repoItemRepository)
7673
.coroutine { searchRepoItems(term, page) }
7774
.wasInvoked(exactly = once)

0 commit comments

Comments
 (0)