Skip to content

Commit 6368068

Browse files
committed
Refactor test suites to replace either with simpler assertions, improve formatting consistency, and streamline test setups across ArticleServiceSpec, ArticleRouteSpec, and others.
1 parent f8bd4a6 commit 6368068

11 files changed

Lines changed: 775 additions & 975 deletions

File tree

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

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,104 +30,106 @@ import kotlinx.serialization.json.Json
3030

3131
@TestRegistering
3232
fun TestSuite.testService(
33-
@TestElementName name: String,
34-
testConfig: TestConfig = TestConfig,
35-
test: suspend context(Dependencies) Test.ExecutionScope.() -> Unit,
33+
@TestElementName name: String,
34+
testConfig: TestConfig = TestConfig,
35+
test: suspend context(Dependencies) Test.ExecutionScope.() -> Unit,
3636
) =
37-
test(name, testConfig) {
38-
withTestDependencies { dependencies ->
39-
test.invoke(dependencies, this@test)
37+
test(name, testConfig) {
38+
withTestDependencies { dependencies ->
39+
test.invoke(dependencies, this@test)
40+
}
4041
}
41-
}
4242

4343
@TestRegistering
4444
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-
)
45+
@TestElementName name: String,
46+
testConfig: TestConfig = TestConfig,
47+
test: suspend context(Dependencies, Raise<DomainError>) Test.ExecutionScope.() -> Unit,
48+
) =
49+
test(name, testConfig) {
50+
withTestDependencies { dependencies ->
51+
either<DomainError, Unit> {
52+
test.invoke(
53+
dependencies,
54+
contextOf<Raise<DomainError>>(),
55+
this@test,
56+
)
57+
}
58+
.shouldBeRight()
59+
}
5860
}
59-
.shouldBeRight()
60-
}
61-
}
6261

6362
@TestRegistering
6463
fun TestSuite.testServer(
65-
@TestElementName name: String,
66-
testConfig: TestConfig = TestConfig,
67-
test:
68-
suspend context(Dependencies, HttpClient, Raise<DomainError>) Test.ExecutionScope.(
69-
) -> Unit,
64+
@TestElementName name: String,
65+
testConfig: TestConfig = TestConfig,
66+
test:
67+
suspend context(Dependencies, HttpClient, Raise<DomainError>) Test.ExecutionScope.(
68+
) -> Unit,
7069
) =
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-
}
80-
}
81-
.use { client ->
82-
either<DomainError, Unit> {
83-
test.invoke(
84-
dependencies,
85-
client,
86-
contextOf<Raise<DomainError>>(),
87-
this@test,
88-
)
70+
test(name, testConfig) {
71+
withTestDependencies { dependencies ->
72+
testApplication {
73+
application { app(dependencies) }
74+
createClient {
75+
expectSuccess = false
76+
install(ContentNegotiation) {
77+
json(Json { serializersModule = kotlinXSerializersModule })
78+
}
79+
}
80+
.use { client ->
81+
either<DomainError, Unit> {
82+
test.invoke(
83+
dependencies,
84+
client,
85+
contextOf<Raise<DomainError>>(),
86+
this@test,
87+
)
88+
}
89+
.shouldBeRight()
90+
}
8991
}
90-
.shouldBeRight()
91-
}
92-
}
92+
}
9393
}
94-
}
9594

9695
context(client: HttpClient)
9796
val client: HttpClient
98-
get() = client
97+
get() = client
9998

10099
context(dependencies: Dependencies)
101100
val dependencies: Dependencies
102-
get() = dependencies
101+
get() = dependencies
103102

104103
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,
104+
suspend fun ArticleService.createArticle(
105+
userId: UserId,
106+
article: ArticleFixture = articleFixture(),
107+
): Article =
108+
createArticle(
109+
CreateArticle(
110+
userId,
111+
article.title,
112+
article.description,
113+
article.body,
114+
article.tags,
115+
)
113116
)
114-
)
115117

116118
context(dependencies: Dependencies, _: Raise<DomainError>)
117119
fun registerUser(fixture: UserFixture = userFixture()): RegisteredUser {
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-
}
120+
val token =
121+
dependencies.userService.register(
122+
RegisterUser(fixture.username, fixture.email, fixture.password)
123+
)
124+
val jwt =
125+
withError({ JwtInvalid(it.toString()) }) {
126+
JWT.decodeT(token.value, JWSHMAC512Algorithm)
127+
.bind<KJWTVerificationError, DecodedJWT<JWSHMAC512Algorithm>>()
128+
}
129+
val id =
130+
ensureNotNull(jwt.claimValueAsLong("id").getOrNull()) {
131+
JwtInvalid("id missing from JWT Token")
132+
}
131133

132-
return RegisteredUser(fixture, token, UserId(id))
134+
return RegisteredUser(fixture, token, UserId(id))
133135
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,6 @@ fun <E> assertRaised(block: Raise<E>.() -> Unit): E =
133133
recover({
134134
block()
135135
throw AssertionError("Expected erro to be raised")
136-
}) { e -> e }
136+
}) { e ->
137+
e
138+
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import io.github.nomisrev.users.Login
1515
import io.github.nomisrev.users.RegisterUser
1616
import io.github.nomisrev.users.Update
1717
import io.github.nomisrev.users.UserId
18-
import io.kotest.assertions.arrow.core.shouldBeLeft
1918
import io.kotest.assertions.arrow.core.shouldBeRight
19+
import io.kotest.matchers.shouldBe
2020

2121
fun incorrectInput(head: InvalidField, vararg tail: InvalidField) =
2222
IncorrectInput(nonEmptyListOf(head, *tail))
@@ -26,7 +26,7 @@ val Validation by testSuite {
2626
test("accumulates all invalid fields and all errors per field") {
2727
val input = RegisterUser(username = "", email = "not-an-email", password = "")
2828

29-
either { input.validate() } shouldBeLeft
29+
assertRaised { input.validate() } shouldBe
3030
incorrectInput(
3131
InvalidUsername(
3232
nonEmptyListOf(
@@ -47,7 +47,7 @@ val Validation by testSuite {
4747
test("accumulates email and password validation errors") {
4848
val input = Login(email = "", password = "")
4949

50-
either { input.validate() } shouldBeLeft
50+
assertRaised { input.validate() } shouldBe
5151
incorrectInput(
5252
InvalidEmail(nonEmptyListOf("Cannot be blank", "'' is invalid email")),
5353
InvalidPassword(
@@ -70,7 +70,7 @@ val Validation by testSuite {
7070
image = null,
7171
)
7272

73-
either { input.validate() } shouldBeLeft
73+
assertRaised { input.validate() } shouldBe
7474
incorrectInput(
7575
InvalidUsername(
7676
nonEmptyListOf(
@@ -106,7 +106,7 @@ val Validation by testSuite {
106106
tagList = listOf("", "ok", " "),
107107
)
108108

109-
either { input.validate() } shouldBeLeft
109+
assertRaised { input.validate() } shouldBe
110110
incorrectInput(
111111
InvalidTitle(nonEmptyListOf("Cannot be blank")),
112112
InvalidDescription(nonEmptyListOf("Cannot be blank")),
@@ -116,7 +116,7 @@ val Validation by testSuite {
116116
}
117117

118118
test("validates body") {
119-
either { NewComment(body = " ").validate() } shouldBeLeft
119+
assertRaised { NewComment(body = " ").validate() } shouldBe
120120
incorrectInput(InvalidBody(nonEmptyListOf("Cannot be blank")))
121121
}
122122

@@ -130,7 +130,7 @@ val Validation by testSuite {
130130
)
131131
)
132132

133-
either { input.validate(userId) } shouldBeLeft
133+
assertRaised { input.validate(userId) } shouldBe
134134
incorrectInput(
135135
InvalidFeedOffset(nonEmptyListOf("too small, minimum is 0, and found -1")),
136136
InvalidFeedLimit(nonEmptyListOf("too small, minimum is 1, and found 0")),
@@ -146,7 +146,7 @@ val Validation by testSuite {
146146
)
147147
)
148148

149-
either { input.validate(currentUserId = null) } shouldBeLeft
149+
assertRaised { input.validate(currentUserId = null) } shouldBe
150150
incorrectInput(
151151
InvalidFeedOffset(nonEmptyListOf("too small, minimum is 0, and found -1")),
152152
InvalidFeedLimit(nonEmptyListOf("too small, minimum is 1, and found 0")),

0 commit comments

Comments
 (0)