Skip to content

Commit f8bd4a6

Browse files
committed
Refactor test specs to use articleFixture and testDependencies; simplify test setup and improve consistency across ArticlesRouteSpec, ArticleServiceSpec, and others.
1 parent faf13d5 commit f8bd4a6

6 files changed

Lines changed: 242 additions & 296 deletions

File tree

src/test/kotlin/io/github/nomisrev/TestBalloon.kt

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.github.nefilim.kjwt.JWSHMAC512Algorithm
1515
import io.github.nefilim.kjwt.JWT
1616
import io.github.nefilim.kjwt.KJWTVerificationError
1717
import io.github.nomisrev.articles.Article
18+
import io.github.nomisrev.articles.ArticleService
1819
import io.github.nomisrev.articles.CreateArticle
1920
import io.github.nomisrev.env.Dependencies
2021
import io.github.nomisrev.env.kotlinXSerializersModule
@@ -29,84 +30,104 @@ import kotlinx.serialization.json.Json
2930

3031
@TestRegistering
3132
fun TestSuite.testService(
32-
@TestElementName name: String,
33-
testConfig: TestConfig = TestConfig,
34-
test: suspend context(Dependencies) Test.ExecutionScope.() -> Unit,
33+
@TestElementName name: String,
34+
testConfig: TestConfig = TestConfig,
35+
test: suspend context(Dependencies) Test.ExecutionScope.() -> Unit,
3536
) =
36-
test(name, testConfig) {
37-
withTestDependencies { dependencies ->
38-
test.invoke(dependencies, this@test)
39-
}
37+
test(name, testConfig) {
38+
withTestDependencies { dependencies ->
39+
test.invoke(dependencies, this@test)
4040
}
41+
}
42+
43+
@TestRegistering
44+
fun TestSuite.testDependencies(
45+
@TestElementName name: String,
46+
testConfig: TestConfig = TestConfig,
47+
test:
48+
suspend context(Dependencies, Raise<DomainError>) Test.ExecutionScope.(
49+
) -> Unit,
50+
) = test(name, testConfig) {
51+
withTestDependencies { dependencies ->
52+
either<DomainError, Unit> {
53+
test.invoke(
54+
dependencies,
55+
contextOf<Raise<DomainError>>(),
56+
this@test,
57+
)
58+
}
59+
.shouldBeRight()
60+
}
61+
}
4162

4263
@TestRegistering
4364
fun TestSuite.testServer(
44-
@TestElementName name: String,
45-
testConfig: TestConfig = TestConfig,
46-
test:
47-
suspend context(Dependencies, HttpClient, Raise<DomainError>) Test.ExecutionScope.(
48-
) -> Unit,
65+
@TestElementName name: String,
66+
testConfig: TestConfig = TestConfig,
67+
test:
68+
suspend context(Dependencies, HttpClient, Raise<DomainError>) Test.ExecutionScope.(
69+
) -> Unit,
4970
) =
50-
test(name, testConfig) {
51-
withTestDependencies { dependencies ->
52-
testApplication {
53-
application { app(dependencies) }
54-
createClient {
55-
expectSuccess = false
56-
install(ContentNegotiation) {
57-
json(Json { serializersModule = kotlinXSerializersModule })
58-
}
59-
}
60-
.use { client ->
61-
either<DomainError, Unit> {
62-
test.invoke(
63-
dependencies,
64-
client,
65-
contextOf<Raise<DomainError>>(),
66-
this@test,
67-
)
68-
}
69-
.shouldBeRight()
70-
}
71-
}
71+
test(name, testConfig) {
72+
withTestDependencies { dependencies ->
73+
testApplication {
74+
application { app(dependencies) }
75+
createClient {
76+
expectSuccess = false
77+
install(ContentNegotiation) {
78+
json(Json { serializersModule = kotlinXSerializersModule })
79+
}
7280
}
81+
.use { client ->
82+
either<DomainError, Unit> {
83+
test.invoke(
84+
dependencies,
85+
client,
86+
contextOf<Raise<DomainError>>(),
87+
this@test,
88+
)
89+
}
90+
.shouldBeRight()
91+
}
92+
}
7393
}
94+
}
7495

7596
context(client: HttpClient)
7697
val client: HttpClient
77-
get() = client
98+
get() = client
7899

79100
context(dependencies: Dependencies)
80101
val dependencies: Dependencies
81-
get() = dependencies
102+
get() = dependencies
82103

83-
context(dependencies: Dependencies, _: Raise<DomainError>)
84-
suspend fun createArticle(userId: UserId, article: ArticleFixture = articleFixture()): Article =
85-
dependencies.articleService.createArticle(
86-
CreateArticle(
87-
userId,
88-
article.title,
89-
article.description,
90-
article.body,
91-
article.tags,
92-
)
104+
context(_: Raise<DomainError>)
105+
suspend fun ArticleService.createArticle(userId: UserId, article: ArticleFixture = articleFixture()): Article =
106+
createArticle(
107+
CreateArticle(
108+
userId,
109+
article.title,
110+
article.description,
111+
article.body,
112+
article.tags,
93113
)
114+
)
94115

95116
context(dependencies: Dependencies, _: Raise<DomainError>)
96117
fun registerUser(fixture: UserFixture = userFixture()): RegisteredUser {
97-
val token =
98-
dependencies.userService.register(
99-
RegisterUser(fixture.username, fixture.email, fixture.password)
100-
)
101-
val jwt =
102-
withError({ JwtInvalid(it.toString()) }) {
103-
JWT.decodeT(token.value, JWSHMAC512Algorithm)
104-
.bind<KJWTVerificationError, DecodedJWT<JWSHMAC512Algorithm>>()
105-
}
106-
val id =
107-
ensureNotNull(jwt.claimValueAsLong("id").getOrNull()) {
108-
JwtInvalid("id missing from JWT Token")
109-
}
118+
val token =
119+
dependencies.userService.register(
120+
RegisterUser(fixture.username, fixture.email, fixture.password)
121+
)
122+
val jwt =
123+
withError({ JwtInvalid(it.toString()) }) {
124+
JWT.decodeT(token.value, JWSHMAC512Algorithm)
125+
.bind<KJWTVerificationError, DecodedJWT<JWSHMAC512Algorithm>>()
126+
}
127+
val id =
128+
ensureNotNull(jwt.claimValueAsLong("id").getOrNull()) {
129+
JwtInvalid("id missing from JWT Token")
130+
}
110131

111-
return RegisteredUser(fixture, token, UserId(id))
132+
return RegisteredUser(fixture, token, UserId(id))
112133
}

src/test/kotlin/io/github/nomisrev/TestSupport.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.github.nomisrev
22

3+
import arrow.core.raise.context.Raise
34
import arrow.core.raise.context.bind
45
import arrow.core.raise.context.ensureNotNull
56
import arrow.core.raise.context.withError
67
import arrow.core.raise.either
8+
import arrow.core.raise.recover
79
import arrow.fx.coroutines.resourceScope
810
import io.github.nefilim.kjwt.DecodedJWT
911
import io.github.nefilim.kjwt.JWSHMAC512Algorithm
@@ -126,3 +128,9 @@ fun Dependencies.registerUser(fixture: UserFixture = userFixture()): RegisteredU
126128

127129
fun HttpMessageBuilder.tokenAuth(token: String): Unit =
128130
header(HttpHeaders.Authorization, "Token $token")
131+
132+
fun <E> assertRaised(block: Raise<E>.() -> Unit): E =
133+
recover({
134+
block()
135+
throw AssertionError("Expected erro to be raised")
136+
}) { e -> e }

src/test/kotlin/io/github/nomisrev/articles/ArticleRouteSpec.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ val ArticleRouteSuite by testSuite {
198198
val _ =
199199
dependencies.userPersistence.followProfile(followed.user.username, reader.userId)
200200

201-
val createdFollowedArticle = createArticle(followed.userId)
202-
val _ = createArticle(unrelated.userId)
201+
val createdFollowedArticle =
202+
dependencies.articleService.createArticle(followed.userId, articleFixture())
203+
val _ = dependencies.articleService.createArticle(unrelated.userId, articleFixture())
203204

204205
val response =
205206
client.request(

0 commit comments

Comments
 (0)