/
ObserveDailyReadArticleTest.kt
66 lines (56 loc) · 2.13 KB
/
ObserveDailyReadArticleTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (C) 2022, Kasem S.M
* All rights reserved.
*/
package kasem.sm.article.domain.interactors
import app.cash.turbine.test
import io.mockk.coEvery
import io.mockk.mockk
import java.sql.SQLException
import kasem.sm.article.datasource.cache.ArticleDatabaseService
import kasem.sm.article.domain.interactors.utils.ArticleFakes.defaultQuadDataWithOneFalse
import kasem.sm.article.domain.interactors.utils.ArticleFakes.getMockEntity
import kasem.sm.article.domain.observers.ObserveDailyReadArticle
import kasem.sm.common_test_utils.ThreadExceptionTestRule
import kasem.sm.common_test_utils.shouldBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
@ExperimentalCoroutinesApi
class ObserveDailyReadArticleTest {
@get:Rule
val uncaughtExceptionHandler = ThreadExceptionTestRule()
private val databaseMock: ArticleDatabaseService = mockk()
private val observer = ObserveDailyReadArticle(
cache = databaseMock
)
@Test
fun assertFlowEmitsProperValue() = runTest {
coEvery { databaseMock.getActiveArticleFlow() } returns flow {
emit(getMockEntity(defaultQuadDataWithOneFalse))
}
observer.joinAndCollect(
params = Unit,
coroutineScope = TestScope()
).test {
awaitItem()?.isActiveInDailyRead shouldBe defaultQuadDataWithOneFalse.second.isActiveInDailyRead
cancelAndIgnoreRemainingEvents()
}
}
@Test
fun assertExceptionIsCaught() = runBlocking {
val list = mutableListOf<String>()
coEvery { databaseMock.getActiveArticleFlow() } throws SQLException("Table doesn't exists")
observer.joinAndCollect(
params = Unit,
coroutineScope = TestScope(UnconfinedTestDispatcher()),
onError = { list.add(it) },
).test { cancelAndIgnoreRemainingEvents() }
list.first() shouldBe "Table doesn't exists"
}
}