Replace Shared Preferences with Data Store

This commit is contained in:
Kirill Kamakin
2022-04-03 19:55:31 +05:00
parent fd9f7e5aa1
commit b3e25db4df
17 changed files with 244 additions and 144 deletions

View File

@@ -13,8 +13,8 @@ import gq.kirmanak.mealient.test.AuthImplTestData.TEST_USERNAME
import gq.kirmanak.mealient.test.RobolectricTest
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
@@ -102,12 +102,12 @@ class AuthRepoImplTest : RobolectricTest() {
dataSource.authenticate(eq(TEST_USERNAME), eq(TEST_PASSWORD), eq(TEST_BASE_URL))
} returns TEST_TOKEN
subject.authenticate(TEST_USERNAME, TEST_PASSWORD, TEST_BASE_URL)
verify { storage.storeAuthData(TEST_AUTH_HEADER, TEST_BASE_URL) }
coVerify { storage.storeAuthData(TEST_AUTH_HEADER, TEST_BASE_URL) }
}
@Test
fun `when logout then clearAuthData is called`() = runTest {
subject.logout()
verify { storage.clearAuthData() }
coVerify { storage.clearAuthData() }
}
}

View File

@@ -0,0 +1,47 @@
package gq.kirmanak.mealient.data.storage
import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.testing.HiltAndroidTest
import gq.kirmanak.mealient.test.HiltRobolectricTest
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Test
import javax.inject.Inject
@OptIn(ExperimentalCoroutinesApi::class)
@HiltAndroidTest
class PreferencesStorageImplTest : HiltRobolectricTest() {
@Inject
lateinit var subject: PreferencesStorage
@Test
fun `when getValue without writes then null`() = runTest {
assertThat(subject.getValue(subject.authHeaderKey)).isNull()
}
@Test(expected = IllegalStateException::class)
fun `when requireValue without writes then throws IllegalStateException`() = runTest {
subject.requireValue(subject.authHeaderKey)
}
@Test
fun `when getValue after write then returns value`() = runTest {
subject.storeValues(Pair(subject.authHeaderKey, "test"))
assertThat(subject.getValue(subject.authHeaderKey)).isEqualTo("test")
}
@Test
fun `when storeValue then valueUpdates emits`() = runTest {
subject.storeValues(Pair(subject.authHeaderKey, "test"))
assertThat(subject.valueUpdates(subject.authHeaderKey).first()).isEqualTo("test")
}
@Test
fun `when remove value then getValue returns null`() = runTest {
subject.storeValues(Pair(subject.authHeaderKey, "test"))
subject.removeValues(subject.authHeaderKey)
assertThat(subject.getValue(subject.authHeaderKey)).isNull()
}
}

View File

@@ -1,6 +1,6 @@
package gq.kirmanak.mealient.extensions
import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import org.junit.Test
@@ -10,27 +10,27 @@ class RoomTypeConvertersTest {
fun `when localDateTimeToTimestamp then correctly converts`() {
val input = LocalDateTime.parse("2021-11-13T15:56:33")
val actual = RoomTypeConverters.localDateTimeToTimestamp(input)
Truth.assertThat(actual).isEqualTo(1636818993000)
assertThat(actual).isEqualTo(1636818993000)
}
@Test
fun `when timestampToLocalDateTime then correctly converts`() {
val expected = LocalDateTime.parse("2021-11-13T15:58:38")
val actual = RoomTypeConverters.timestampToLocalDateTime(1636819118000)
Truth.assertThat(actual).isEqualTo(expected)
assertThat(actual).isEqualTo(expected)
}
@Test
fun `when localDateToTimeStamp then correctly converts`() {
val input = LocalDate.parse("2021-11-13")
val actual = RoomTypeConverters.localDateToTimeStamp(input)
Truth.assertThat(actual).isEqualTo(1636761600000)
assertThat(actual).isEqualTo(1636761600000)
}
@Test
fun `when timestampToLocalDate then correctly converts`() {
val expected = LocalDate.parse("2021-11-13")
val actual = RoomTypeConverters.timestampToLocalDate(1636761600000)
Truth.assertThat(actual).isEqualTo(expected)
assertThat(actual).isEqualTo(expected)
}
}