Skip to content

Commit

Permalink
test: add datastore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Divinelink committed Jul 10, 2024
1 parent d780ce0 commit 10b9780
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 40 deletions.
6 changes: 0 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ dependencies {
testImplementation(libs.ui.automator)
debugImplementation(libs.androidx.test.ktx)

testImplementation(libs.datastore)
testImplementation(libs.datastore.core)
testImplementation(libs.datastore.preferences)
testImplementation(libs.datastore.preferences.core)
testImplementation(libs.encrypted.preferences)

testImplementation(libs.kotlin.test.junit)

androidTestImplementation(libs.androidx.compose.ui.test)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
package com.andreolas.movierama.base.storage

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStoreFile
import androidx.test.platform.app.InstrumentationRegistry
import com.divinelink.core.datastore.DataStorePreferenceStorage
import com.divinelink.core.designsystem.theme.Theme
import com.divinelink.core.testing.MainDispatcherRule
import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod
import com.divinelink.core.testing.datastore.TestDatastoreFactory
import com.divinelink.core.testing.factories.model.jellyseerr.JellyseerrAccountDetailsFactory
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

private const val TEST_DATASTORE_NAME: String = "test_datastore"

@RunWith(RobolectricTestRunner::class)
class DataStorePreferenceStorageTest {

private lateinit var storage: DataStorePreferenceStorage

@get:Rule
val mainDispatcherRule = MainDispatcherRule()
private val testDispatcher = mainDispatcherRule.testDispatcher

private val testContext: Context = InstrumentationRegistry.getInstrumentation().targetContext

private val testCoroutineScope = TestScope(testDispatcher)

private val fakeDataStore: DataStore<Preferences> =
PreferenceDataStoreFactory.create(
scope = testCoroutineScope,
produceFile = { testContext.preferencesDataStoreFile(TEST_DATASTORE_NAME) },
)
private val fakeDataStore = TestDatastoreFactory.create()

@Test
fun `test selectTheme sets selectedTheme`() = runTest {
Expand Down Expand Up @@ -148,4 +128,72 @@ class DataStorePreferenceStorageTest {

assertThat(storage.accountId.first()).isNull()
}

@Test
fun `test setJellyseerrAddress`() = runTest {
storage = DataStorePreferenceStorage(fakeDataStore)

assertThat(storage.jellyseerrAddress.first()).isEqualTo(null)

storage.setJellyseerrAddress("http://localhost:5050")
assertThat(storage.jellyseerrAddress.first()).isEqualTo("http://localhost:5050")
}

@Test
fun `test clearJellyseerrAddress`() = runTest {
storage = DataStorePreferenceStorage(fakeDataStore)

storage.setJellyseerrAddress("http://localhost:5050")
assertThat(storage.jellyseerrAddress.first()).isEqualTo("http://localhost:5050")

storage.clearJellyseerrAddress()
assertThat(storage.jellyseerrAddress.first()).isEqualTo(null)
}

@Test
fun `test setJellyseerrAccount`() = runTest {
storage = DataStorePreferenceStorage(fakeDataStore)
assertThat(storage.jellyseerrAccount.first()).isEqualTo(null)

val displayName = JellyseerrAccountDetailsFactory.jellyseerr().displayName

storage.setJellyseerrAccount(displayName)
assertThat(storage.jellyseerrAccount.first()).isEqualTo(displayName)
}

@Test
fun `test clearJellyseerrAccount`() = runTest {
storage = DataStorePreferenceStorage(fakeDataStore)
val displayName = JellyseerrAccountDetailsFactory.jellyseerr().displayName

storage.setJellyseerrAccount(displayName)
assertThat(storage.jellyseerrAccount.first()).isEqualTo(displayName)

storage.clearJellyseerrAccount()
assertThat(storage.jellyseerrAccount.first()).isEqualTo(null)
}

@Test
fun `test testJellyseerrSignInMethod`() = runTest {
storage = DataStorePreferenceStorage(fakeDataStore)
assertThat(storage.jellyseerrSignInMethod.first()).isEqualTo(null)

storage.setJellyseerrSignInMethod(JellyseerrLoginMethod.JELLYSEERR.name)
assertThat(
storage.jellyseerrSignInMethod.first(),
).isEqualTo(JellyseerrLoginMethod.JELLYSEERR.name)
}

@Test
fun `test clearJellyseerrSignInMethod`() = runTest {
storage = DataStorePreferenceStorage(fakeDataStore)

storage.setJellyseerrSignInMethod(JellyseerrLoginMethod.JELLYSEERR.name)
assertThat(
storage.jellyseerrSignInMethod.first(),
).isEqualTo(JellyseerrLoginMethod.JELLYSEERR.name)

storage.clearJellyseerrSignInMethod()
assertThat(storage.jellyseerrSignInMethod.first()).isEqualTo(null)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.andreolas.movierama.session

import com.divinelink.core.datastore.SessionStorage
import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod
import com.divinelink.core.testing.storage.FakeEncryptedPreferenceStorage
import com.divinelink.core.testing.storage.FakePreferenceStorage
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Test
Expand Down Expand Up @@ -125,4 +127,35 @@ class SessionStorageTest {

assertThat(preferenceStorage.accountId.value).isEqualTo("account_id")
}

@Test
fun `test clearJellyseerrSession clears jellyseerr data`() = runTest {
val preferenceStorage = FakePreferenceStorage(
jellyseerrAccount = "Zabaob",
jellyseerrAddress = "http://localhost:5050",
jellyseerrSignInMethod = JellyseerrLoginMethod.JELLYSEERR.name,
)
val encryptedPreferenceStorage = FakeEncryptedPreferenceStorage(
jellyseerrAuthCookie = "123456789qwertyuiop",
)

val sessionStorage = SessionStorage(
preferenceStorage,
encryptedPreferenceStorage,
)

assertThat(preferenceStorage.jellyseerrAccount.first()).isEqualTo("Zabaob")
assertThat(preferenceStorage.jellyseerrAddress.first()).isEqualTo("http://localhost:5050")
assertThat(preferenceStorage.jellyseerrSignInMethod.first()).isEqualTo(
JellyseerrLoginMethod.JELLYSEERR.name,
)
assertThat(encryptedPreferenceStorage.jellyseerrAuthCookie).isEqualTo("123456789qwertyuiop")

sessionStorage.clearJellyseerrSession()

assertThat(preferenceStorage.jellyseerrAccount.first()).isNull()
assertThat(preferenceStorage.jellyseerrAddress.first()).isNull()
assertThat(preferenceStorage.jellyseerrSignInMethod.first()).isNull()
assertThat(encryptedPreferenceStorage.jellyseerrAuthCookie).isNull()
}
}
6 changes: 6 additions & 0 deletions core/testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ dependencies {
implementation(libs.robolectric)

implementation(libs.sqldelight.driver)

implementation(libs.datastore)
implementation(libs.datastore.core)
implementation(libs.datastore.preferences)
implementation(libs.datastore.preferences.core)
implementation(libs.encrypted.preferences)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.divinelink.core.testing.datastore

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStoreFile
import androidx.test.platform.app.InstrumentationRegistry
import com.divinelink.core.testing.MainDispatcherRule
import kotlinx.coroutines.test.TestScope
import org.junit.Rule

class TestDatastoreFactory {

@get:Rule
val mainDispatcherRule = MainDispatcherRule()
private val testDispatcher = mainDispatcherRule.testDispatcher

private val testCoroutineScope = TestScope(testDispatcher)

private val testContext: Context = InstrumentationRegistry.getInstrumentation().targetContext

val datastore: DataStore<Preferences> = PreferenceDataStoreFactory.create(
scope = testCoroutineScope,
produceFile = { testContext.preferencesDataStoreFile(TEST_DATASTORE_NAME) },
)

companion object {
private const val TEST_DATASTORE_NAME: String = "test_datastore"

fun create(): DataStore<Preferences> = TestDatastoreFactory().datastore
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.divinelink.core.testing.factories.storage

import com.divinelink.core.datastore.SessionStorage
import com.divinelink.core.model.jellyseerr.JellyseerrLoginMethod
import com.divinelink.core.testing.storage.FakeEncryptedPreferenceStorage
import com.divinelink.core.testing.storage.FakePreferenceStorage

Expand All @@ -23,7 +24,13 @@ object SessionStorageFactory {
fun full() = SessionStorage(
storage = FakePreferenceStorage(
accountId = "123456789",
jellyseerrAccount = "Zabaob",
jellyseerrAddress = "http://localhost:5050",
jellyseerrSignInMethod = JellyseerrLoginMethod.JELLYSEERR.name,
),
encryptedStorage = FakeEncryptedPreferenceStorage(
sessionId = "123456789",
jellyseerrAuthCookie = "123456789qwertyuiop",
),
encryptedStorage = FakeEncryptedPreferenceStorage(sessionId = "123456789"),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FakeGetJellyseerrDetailsUseCase {
)
}

fun mockSuccess(response: Result<JellyseerrAccountDetails>) {
fun mockSuccess(response: Result<JellyseerrAccountDetails?>) {
whenever(mock.invoke(any())).thenReturn(flowOf(response))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ class JellyseerrSettingsViewModelTest {
)
}

@Test
fun `test getJellyseerrAccount with null clear logged in data`() = runTest {
testRobot
.mockJellyseerrAccountDetailsResponse(Result.success(null))
.buildViewModel()
.assertUiState(
createUiState(
jellyseerrState = JellyseerrState.Initial(
isLoading = false,
address = "",
),
),
)
}

private fun createUiState(
snackbarMessage: SnackbarMessage? = null,
jellyseerrState: JellyseerrState = JellyseerrState.Initial(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class JellyseerrSettingsViewModelTestRobot : ViewModelTestRobot<JellyseerrSettin
loginJellyseerrUseCase.mockSuccess(flowOf(response))
}

fun mockJellyseerrAccountDetailsResponse(response: Result<JellyseerrAccountDetails?>) = apply {
getJellyseerrDetailsUseCase.mockSuccess(response)
}

fun onUserAddressChange(address: String) = apply {
viewModel.onJellyseerrInteraction(JellyseerrInteraction.OnAddressChange(address))
}
Expand Down

0 comments on commit 10b9780

Please sign in to comment.