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.common.encodeJson
6+ import dev.dimension.flare.data.database.app.AppDatabase
7+ import dev.dimension.flare.data.network.mastodon.api.model.CreateApplicationResponse
8+ import dev.dimension.flare.model.PlatformType
9+ import dev.dimension.flare.ui.model.UiApplication
10+ import kotlinx.coroutines.Dispatchers
11+ import kotlinx.coroutines.test.StandardTestDispatcher
12+ import kotlinx.coroutines.test.runTest
13+ import kotlin.test.AfterTest
14+ import kotlin.test.BeforeTest
15+ import kotlin.test.Test
16+ import kotlin.test.assertEquals
17+
18+ internal class ApplicationRepositoryTest {
19+
20+ private lateinit var appDatabase: AppDatabase
21+ private val testDispatcher = StandardTestDispatcher ()
22+
23+ val host = " test-mstdn-host.com"
24+ val application = UiApplication .Mastodon (
25+ host = host,
26+ application = CreateApplicationResponse (
27+ clientID = " test-client-id" ,
28+ clientSecret = " test-client-secret" ,
29+ redirectURI = " test-redirect-mstdn-uri" ,
30+ )
31+ )
32+
33+ @BeforeTest
34+ fun setUp () {
35+ appDatabase = Room
36+ .inMemoryDatabaseBuilder<AppDatabase >()
37+ .setDriver(BundledSQLiteDriver ())
38+ .setQueryCoroutineContext(Dispatchers .Unconfined )
39+ .build()
40+ }
41+
42+ @AfterTest
43+ fun tearDown () {
44+ appDatabase.close()
45+ }
46+
47+ @Test
48+ fun `should fun host by host when findByHost` () = runTest(testDispatcher) {
49+
50+ val repo = createRepository()
51+
52+ repo.addApplication(
53+ host = application.host,
54+ credentialJson = application.application.encodeJson(),
55+ platformType = PlatformType .Mastodon
56+ )
57+
58+ val resultApplication = repo.findByHost(host)
59+ assertEquals(host, resultApplication?.host)
60+ }
61+
62+ @Test
63+ fun `should have correct values for pendingOAuth for get and set and clear the same` () =
64+ runTest(testDispatcher) {
65+
66+ val repo = createRepository()
67+
68+ repo.addApplication(
69+ host = application.host,
70+ credentialJson = application.application.encodeJson(),
71+ platformType = PlatformType .Mastodon
72+ )
73+
74+ repo.setPendingOAuth(
75+ host = host,
76+ pendingOAuth = true
77+ )
78+
79+ val resultApplication = repo.getPendingOAuth()
80+ assertEquals(host, resultApplication?.host)
81+
82+ repo.clearPendingOAuth()
83+ val resultApplicationAfterClear = repo.getPendingOAuth()
84+ assertEquals(null , resultApplicationAfterClear)
85+
86+ }
87+
88+
89+
90+
91+
92+ private fun createRepository (): ApplicationRepository {
93+ return ApplicationRepository (appDatabase)
94+ }
95+ }
0 commit comments