Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dev.dimension.flare.data.repository

import androidx.room.Room
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import dev.dimension.flare.common.encodeJson
import dev.dimension.flare.data.database.app.AppDatabase
import dev.dimension.flare.data.network.mastodon.api.model.CreateApplicationResponse
import dev.dimension.flare.model.PlatformType
import dev.dimension.flare.ui.model.UiApplication
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

internal class ApplicationRepositoryTest {

private lateinit var appDatabase: AppDatabase
private val testDispatcher = StandardTestDispatcher()

val host = "test-mstdn-host.com"
val application = UiApplication.Mastodon(
host = host,
application = CreateApplicationResponse(
clientID = "test-client-id",
clientSecret = "test-client-secret",
redirectURI = "test-redirect-mstdn-uri",
)
)

@BeforeTest
fun setUp() {
appDatabase = Room
.inMemoryDatabaseBuilder<AppDatabase>()
.setDriver(BundledSQLiteDriver())
.setQueryCoroutineContext(Dispatchers.Unconfined)
.build()
}

@AfterTest
fun tearDown() {
appDatabase.close()
}

@Test
fun `should fun host by host when findByHost`() = runTest(testDispatcher) {

val repo = createRepository()

repo.addApplication(
host = application.host,
credentialJson = application.application.encodeJson(),
platformType = PlatformType.Mastodon
)

val resultApplication = repo.findByHost(host)
assertEquals(host, resultApplication?.host)
}

@Test
fun `should have correct values for pendingOAuth for get and set and clear the same`() =
runTest(testDispatcher) {

val repo = createRepository()

repo.addApplication(
host = application.host,
credentialJson = application.application.encodeJson(),
platformType = PlatformType.Mastodon
)

repo.setPendingOAuth(
host = host,
pendingOAuth = true
)

val resultApplication = repo.getPendingOAuth()
assertEquals(host, resultApplication?.host)

repo.clearPendingOAuth()
val resultApplicationAfterClear = repo.getPendingOAuth()
assertEquals(null, resultApplicationAfterClear)

}





private fun createRepository(): ApplicationRepository {
return ApplicationRepository(appDatabase)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package dev.dimension.flare.data.repository

import androidx.room.Room
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import dev.dimension.flare.data.database.app.AppDatabase
import dev.dimension.flare.ui.model.UiKeywordFilter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

internal class LocalFilterRepositoryTest {

private lateinit var appDatabase: AppDatabase
private val testDispatcher = StandardTestDispatcher()
private val testScope = TestScope(testDispatcher)

@BeforeTest
fun setUp() {
appDatabase = Room
.inMemoryDatabaseBuilder<AppDatabase>()
.setDriver(BundledSQLiteDriver())
.setQueryCoroutineContext(Dispatchers.Unconfined)
.build()
}

@AfterTest
fun tearDown() {
appDatabase.close()
}


@Test
fun `add keyword filter on add`() = runTest(testDispatcher) {

val keywordFilter = UiKeywordFilter(
keyword = "test",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

val repo = createRepository()

repo.add(
keyword = "test",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

val resultKeywordFilter = repo.getAllFlow().first().first()
assertEquals(keywordFilter, resultKeywordFilter)
}

@Test
fun `update keyword filter on update`() = runTest(testDispatcher) {
val keywordFilter = UiKeywordFilter(
keyword = "test",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

val repo = createRepository()

repo.add(
keyword = "test",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

val resultKeywordFilter = repo.getAllFlow().first().first()
assertEquals(keywordFilter, resultKeywordFilter)

repo.update(
keyword = "test",
forTimeline = false,
forNotification = true,
forSearch = true,
expiredAt = null
)

val updatedResult = repo.getAllFlow().first().first()
assertFalse(updatedResult.forTimeline)

}

@Test
fun `delete keyword filter on delete and delete all on clear`() = runTest(testDispatcher) {
val keywordFilter = UiKeywordFilter(
keyword = "test",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

val repo = createRepository()

repo.add(
keyword = "test",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

val resultKeywordFilter = repo.getAllFlow().first().first()
assertEquals(keywordFilter, resultKeywordFilter)

repo.delete("test")

val newResult = repo.getAllFlow().first()
assertEquals(emptyList(), newResult)

repo.add(
keyword = "test-1",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

repo.add(
keyword = "test-2",
forTimeline = true,
forNotification = true,
forSearch = true,
expiredAt = null
)

repo.clear()

val clearAllResult = repo.getAllFlow().first()
assertEquals(emptyList(), clearAllResult)
}

private fun createRepository(): LocalFilterRepository {
return LocalFilterRepository(
database = appDatabase,
coroutineScope = testScope
)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.dimension.flare.data.repository

import androidx.room.Room
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import dev.dimension.flare.data.database.app.AppDatabase
import dev.dimension.flare.ui.model.UiSearchHistory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.Clock

class SearchHistoryRepositoryTest {

private lateinit var appDatabase: AppDatabase
private val testDispatcher = StandardTestDispatcher()
private val testScope = TestScope(testDispatcher)

@BeforeTest
fun setUp() {
appDatabase = Room
.inMemoryDatabaseBuilder<AppDatabase>()
.setDriver(BundledSQLiteDriver())
.setQueryCoroutineContext(Dispatchers.Unconfined)
.build()
}

@AfterTest
fun tearDown() {
appDatabase.close()
}


@Test
fun `should reflect correct data when add, delete search history`() = runTest(testDispatcher) {
val keyword = "test-keyword"

val repo = createRepository()

repo.addSearchHistory(keyword).join()

val addedResult = repo.allSearchHistory.first().toImmutableList().first()
assertEquals(keyword, addedResult.keyword)

repo.deleteSearchHistory(keyword)
val deletedResult = repo.allSearchHistory.first().toImmutableList()
assertEquals(emptyList(), deletedResult)

repo.addSearchHistory(keyword)
repo.addSearchHistory(keyword + "2")

val addedTwiceResult = repo.allSearchHistory.first().toImmutableList()
assertEquals(2, addedTwiceResult.size)

repo.deleteAllSearchHistory()
val deletedAllResult = repo.allSearchHistory.first().toImmutableList()
assertEquals(emptyList(), deletedAllResult)
}

private fun createRepository(): SearchHistoryRepository {
return SearchHistoryRepository(
appDatabase,
testScope
)
}
}
Loading