Skip to content

Commit 653c200

Browse files
committed
Fix tests
1 parent a92e673 commit 653c200

File tree

4 files changed

+60
-56
lines changed

4 files changed

+60
-56
lines changed

src/main/kotlin/com/github/hannotify/classyfire/ui/statemachine/StateContext.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.github.hannotify.classyfire.data.transaction.TransactionRepository
99
import com.github.hannotify.classyfire.process.ClassificationService
1010
import com.github.hannotify.classyfire.ui.statemachine.states.RetrieveCategoriesState
1111
import kotlin.math.ceil
12+
import kotlin.math.max
1213

1314
class StateContext(val categoryRepository: CategoryRepository, val transactionRepository: TransactionRepository,
1415
val classificationService: ClassificationService) {
@@ -33,7 +34,7 @@ class StateContext(val categoryRepository: CategoryRepository, val transactionRe
3334
}
3435

3536
private fun printCategories(categories: List<Category?>) {
36-
val splitSize = categories.size / 2
37+
val splitSize = max(categories.size / 2, 1)
3738
val padding = categories.size.toString().length
3839
val categoryMap = categories.withIndex()
3940
.groupBy { it.index % splitSize }
@@ -49,8 +50,9 @@ class StateContext(val categoryRepository: CategoryRepository, val transactionRe
4950
// 2 columns:
5051
categoryMap.forEachIndexed { index, pairList ->
5152
println("%-60.60s %-60.60s".format(
52-
if (pairList[0] != null) "${index.toString().padStart(padding)} - ${pairList[0]}" else "",
53-
"${(index + splitSize).toString().padStart(padding)} - ${pairList[1]}"))
53+
pairList[0]?.let { "${index.toString().padStart(padding)} - ${pairList[0]}" },
54+
if (pairList.size > 1) "${(index + splitSize).toString().padStart(padding)} - ${pairList[1]}" else ""
55+
))
5456
}
5557

5658
println()

src/test/kotlin/com/github/hannotify/classyfire/classification/ClassificationServiceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.junit.jupiter.api.BeforeEach
1010
import org.junit.jupiter.api.Test
1111
import java.nio.file.Path
1212

13-
public class ClassificationServiceTest {
13+
class ClassificationServiceTest {
1414
private val classifier: ClassificationService = ClassificationService(Path.of(""))
1515

1616
@BeforeEach

src/test/kotlin/com/github/hannotify/classyfire/testdata/Categories.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,25 @@ import com.github.hannotify.classyfire.data.category.CategoryType
55

66
class Categories {
77
companion object {
8-
@JvmStatic val myIncomeCategory: Category = Category(CategoryType.INCOME, "My income")
9-
@JvmStatic val salarySubcategory: Category = Category(CategoryType.INCOME, "Salary", myIncomeCategory)
10-
@JvmStatic val houseCategory: Category = Category(CategoryType.EXPENSES, "House")
11-
@JvmStatic val mortgageSubcategory: Category = Category(CategoryType.EXPENSES, "Mortgage", houseCategory)
12-
@JvmStatic val powerSubcategory: Category = Category(CategoryType.EXPENSES, "Power", houseCategory)
13-
@JvmStatic val waterSubcategory: Category = Category(CategoryType.EXPENSES, "Water", houseCategory)
8+
@JvmStatic
9+
val myIncomeCategory: Category = Category(CategoryType.INCOME, "My income")
10+
@JvmStatic
11+
val salarySubcategory: Category = Category(CategoryType.INCOME, "Salary", myIncomeCategory)
12+
@JvmStatic
13+
val houseCategory: Category = Category(CategoryType.EXPENSES, "House")
14+
@JvmStatic
15+
val mortgageSubcategory: Category = Category(CategoryType.EXPENSES, "Mortgage", houseCategory)
16+
@JvmStatic
17+
val powerSubcategory: Category = Category(CategoryType.EXPENSES, "Power", houseCategory)
18+
@JvmStatic
19+
val waterSubcategory: Category = Category(CategoryType.EXPENSES, "Water", houseCategory)
20+
val allCategories = listOf(
21+
myIncomeCategory,
22+
salarySubcategory,
23+
houseCategory,
24+
mortgageSubcategory,
25+
powerSubcategory,
26+
waterSubcategory
27+
)
1428
}
1529
}
Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,67 @@
11
package com.github.hannotify.classyfire.ui.statemachine
22

33
import com.github.hannotify.classyfire.data.category.CategoryRepository
4-
import com.github.hannotify.classyfire.data.classification.ClassificationRepository
54
import com.github.hannotify.classyfire.data.transaction.TransactionRepository
65
import com.github.hannotify.classyfire.process.ClassificationService
6+
import com.github.hannotify.classyfire.testdata.Categories
77
import com.github.hannotify.classyfire.ui.statemachine.states.*
8+
import io.mockk.every
9+
import io.mockk.mockkStatic
810
import org.assertj.core.api.Assertions.*
11+
import org.junit.jupiter.api.AfterEach
912
import org.junit.jupiter.api.BeforeEach
13+
import org.junit.jupiter.api.Disabled
1014
import org.junit.jupiter.api.Test
15+
import org.junit.jupiter.params.ParameterizedTest
16+
import org.junit.jupiter.params.provider.CsvSource
17+
import java.io.File
1118
import java.nio.file.Path
1219

1320
internal class StateContextTest {
21+
private val categoryRepository = CategoryRepository(Path.of("src/test/resources/categories/categories.txt"))
1422
lateinit var stateContext: StateContext
1523

1624
@BeforeEach
1725
internal fun setup() {
26+
categoryRepository.saveAll(Categories.allCategories)
27+
categoryRepository.persist()
28+
1829
stateContext = StateContext(
1930
CategoryRepository(Path.of("src/test/resources/categories/categories.txt")),
2031
TransactionRepository(Path.of("src/test/resources/transactions/test.csv")),
2132
ClassificationService(Path.of("src/test/resources/classifications/test-output.txt")))
2233
}
2334

24-
@Test
25-
internal fun initialState_shouldBeRetrieveCategoriesState() {
26-
assertThat(stateContext.state).isInstanceOf(RetrieveCategoriesState::class.java)
27-
}
35+
@ParameterizedTest(name = "State {0} should be {1}.")
36+
@CsvSource(textBlock = """
37+
0, RetrieveCategoriesState,
38+
1, RetrieveTransactionsState,
39+
2, ProcessTrainingDataState
40+
3, ProcessIncomeTransactionsState,
41+
4, ProcessTrainingDataState
42+
5, ProcessExpensesTransactionsState,
43+
6, PersistClassificationsState""")
44+
internal fun assertStateOrder(stateNumber: Int, expectedStateName: String) {
45+
mockkStatic(::readLine)
46+
every { readLine() } returns "0"
2847

29-
@Test
30-
internal fun secondState_shouldBeRetrieveTransactionsState() {
31-
stateContext.nextState()
32-
assertThat(stateContext.state).isInstanceOf(RetrieveTransactionsState::class.java)
33-
}
48+
repeat(stateNumber) { stateContext.nextState() }
3449

35-
@Test
36-
internal fun thirdState_shouldBeProcessIncomeTransactionsState() {
37-
stateContext.nextState()
38-
stateContext.nextState()
39-
assertThat(stateContext.state).isInstanceOf(ProcessIncomeTransactionsState::class.java)
50+
assertThat(stateContext.state?.javaClass?.simpleName).isEqualTo(expectedStateName)
4051
}
4152

4253
@Test
43-
internal fun fourthState_shouldBeProcessExpensesTransactionsState() {
44-
stateContext.nextState()
45-
stateContext.nextState()
46-
stateContext.nextState()
47-
assertThat(stateContext.state).isInstanceOf(ProcessExpensesTransactionsState::class.java)
48-
}
54+
internal fun thereShouldBeNoSeventhState() {
55+
mockkStatic(::readLine)
56+
every { readLine() } returns "0"
4957

50-
@Test
51-
internal fun fifthState_shouldBeProcessExpensesTransactionsState() {
52-
stateContext.nextState()
53-
stateContext.nextState()
54-
stateContext.nextState()
55-
stateContext.nextState()
56-
assertThat(stateContext.state).isInstanceOf(ProcessExpensesTransactionsState::class.java)
57-
}
58+
repeat(7) { stateContext.nextState() }
5859

59-
@Test
60-
internal fun sixthState_shouldBePersistClassificationsState() {
61-
stateContext.nextState()
62-
stateContext.nextState()
63-
stateContext.nextState()
64-
stateContext.nextState()
65-
stateContext.nextState()
66-
assertThat(stateContext.state).isInstanceOf(PersistClassificationsState::class.java)
60+
assertThat(stateContext.state).isNull()
6761
}
6862

69-
@Test
70-
internal fun thereShouldBeNoSeventhState() {
71-
stateContext.nextState()
72-
stateContext.nextState()
73-
stateContext.nextState()
74-
stateContext.nextState()
75-
stateContext.nextState()
76-
stateContext.nextState()
77-
assertThat(stateContext.state).isNull()
63+
@AfterEach
64+
internal fun tearDown() {
65+
File(categoryRepository.storageLocation().toString()).delete()
7866
}
7967
}

0 commit comments

Comments
 (0)