Skip to content

Commit ccbeb80

Browse files
committed
test: add tests for AccountRepository
1 parent 090f988 commit ccbeb80

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package dev.dimension.flare.data.repository
2+
3+
import androidx.room.Room
4+
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
5+
import dev.dimension.flare.data.database.app.AppDatabase
6+
import dev.dimension.flare.data.database.cache.CacheDatabase
7+
import dev.dimension.flare.data.datastore.AppDataStore
8+
import dev.dimension.flare.data.io.PlatformPathProducer
9+
import dev.dimension.flare.model.MicroBlogKey
10+
import dev.dimension.flare.ui.model.UiAccount
11+
import dev.dimension.flare.ui.model.UiState
12+
import kotlinx.coroutines.Dispatchers
13+
import kotlinx.coroutines.flow.first
14+
import kotlinx.coroutines.test.StandardTestDispatcher
15+
import kotlinx.coroutines.test.TestScope
16+
import kotlinx.coroutines.test.runTest
17+
import kotlin.test.AfterTest
18+
import kotlin.test.BeforeTest
19+
import kotlin.test.Test
20+
import kotlin.test.assertEquals
21+
import kotlin.test.assertIs
22+
23+
internal class AccountRepositoryTest {
24+
25+
private lateinit var appDatabase: AppDatabase
26+
private lateinit var cacheDatabase: CacheDatabase
27+
private lateinit var appDataStore: AppDataStore
28+
private val testDispatcher = StandardTestDispatcher()
29+
private val testScope = TestScope(testDispatcher)
30+
31+
val accountKey = MicroBlogKey("test-id", "test-host.com")
32+
val credential = UiAccount.Mastodon.Credential(
33+
instance = "test-host.com",
34+
accessToken = "test-token",
35+
)
36+
val account = UiAccount.Mastodon(
37+
accountKey = accountKey,
38+
instance = "test-host.com"
39+
)
40+
41+
@BeforeTest
42+
fun setUp() {
43+
44+
appDatabase = Room
45+
.inMemoryDatabaseBuilder<AppDatabase>()
46+
.setDriver(BundledSQLiteDriver())
47+
.setQueryCoroutineContext(Dispatchers.Unconfined)
48+
.build()
49+
50+
cacheDatabase = Room
51+
.inMemoryDatabaseBuilder<CacheDatabase>()
52+
.setDriver(BundledSQLiteDriver())
53+
.setQueryCoroutineContext(Dispatchers.Unconfined)
54+
.build()
55+
56+
57+
val platformPathProducer = PlatformPathProducer()
58+
appDataStore = AppDataStore(platformPathProducer)
59+
}
60+
61+
@AfterTest
62+
fun tearDown() {
63+
appDatabase.close()
64+
cacheDatabase.close()
65+
}
66+
67+
68+
69+
@Test
70+
fun `should insert account on add account`() = runTest(testDispatcher) {
71+
72+
val repo = createRepository()
73+
74+
repo.addAccount(
75+
account = account,
76+
credential = credential
77+
).join()
78+
79+
val addedAccount = repo.onAdded.first()
80+
assertEquals(accountKey, addedAccount.accountKey)
81+
82+
}
83+
84+
85+
@Test
86+
fun `should set active account on add account`() = runTest(testDispatcher) {
87+
88+
val accountKey = MicroBlogKey("test-id", "test-host.com")
89+
90+
val repo = createRepository()
91+
92+
repo.addAccount(
93+
account = account,
94+
credential = credential
95+
).join()
96+
97+
repo.setActiveAccount(accountKey).join()
98+
99+
100+
val activeAccount = repo.activeAccount.first()
101+
assertIs<UiState.Success<UiAccount>>(activeAccount)
102+
103+
assertEquals(accountKey, activeAccount.data.accountKey)
104+
}
105+
106+
@Test
107+
fun `should delete account on delete account`() = runTest(testDispatcher) {
108+
109+
val accountKey = MicroBlogKey("test-id", "test-host.com")
110+
111+
val repo = createRepository()
112+
113+
repo.addAccount(
114+
account = account,
115+
credential = credential
116+
).join()
117+
118+
val currentlyAddedAccount = repo.allAccounts.first()
119+
assertEquals(1, currentlyAddedAccount.size)
120+
121+
repo.delete(accountKey).join()
122+
123+
val recentlyRemovedKey = repo.onRemoved.first()
124+
assertEquals(accountKey, recentlyRemovedKey)
125+
126+
val deletedAccount = repo.allAccounts.first()
127+
assertEquals(0, deletedAccount.size)
128+
}
129+
130+
131+
132+
133+
134+
private fun createRepository(): AccountRepository {
135+
return AccountRepository(
136+
appDatabase = appDatabase,
137+
coroutineScope = testScope,
138+
appDataStore = appDataStore,
139+
cacheDatabase = cacheDatabase
140+
)
141+
}
142+
143+
}

0 commit comments

Comments
 (0)