How to test Proto datastore? The Medium article by Simona Stojanovic doesn't work for proto datastore
@simona-anomis đang làm issue này rồi.
Từ ngày 12/12/2022.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
I tried the approach in Simona Stojanovic @simona-anomis Medium article but it doesn't work for proto datastore. The issue is that testCoroutineScope.runBlockingTest gives me an error.
The entire code is the same as in the Medium article except for the part where the datastore is created:
DataStoreFactory.create(
serializer = UserPreferencesSerializer,
produceFile = { testContext.dataStoreFile(DATA_STORE_FILE_NAME) },
scope = testCoroutineScope
)
This is the full test file I run as an Instrumented Test:
package com.xkcd.comiclist_data.repository
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.dataStoreFile
import androidx.test.platform.app.InstrumentationRegistry
import app.cash.turbine.test
import com.xkcd.core.XkcdSettings
import com.xkcd.core.settings.XkcdSettingsSerializer
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.test.*
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@HiltAndroidTest
class XkcdSettingsRepositoryImplTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
private val testContext: Context =
InstrumentationRegistry.getInstrumentation().targetContext
private val testCoroutineDispatcher: TestCoroutineDispatcher =
TestCoroutineDispatcher()
@OptIn(ExperimentalCoroutinesApi::class)
private val testCoroutineScope =
TestCoroutineScope(testCoroutineDispatcher + Job())
private val testDataStore: DataStore<XkcdSettings> = DataStoreFactory.create(
serializer = XkcdSettingsSerializer,
produceFile = { testContext.dataStoreFile(XkcdSettingsSerializer.testDataStoreName) },
scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
)
private val repository: XkcdSettingsRepositoryImpl = XkcdSettingsRepositoryImpl(testDataStore)
@OptIn(ExperimentalCoroutinesApi::class)
@Before
fun setup() {
Dispatchers.setMain(testCoroutineDispatcher)
}
@OptIn(ExperimentalCoroutinesApi::class)
@After
fun cleanUp() {
Dispatchers.resetMain()
testCoroutineDispatcher.cleanupTestCoroutines()
testCoroutineScope.cancel()
}
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun dummyTest() {
testCoroutineScope.runBlockingTest {
repository.getXkcdSettings().test {
println(awaitItem())
awaitComplete()
}
}
}
}
The error I get states that This job has not completed yet:
java.lang.IllegalStateException: This job has not completed yet
at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1190)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersDeprecatedKt.runBlockingTest(TestBuildersDeprecated.kt:67)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(Unknown Source:1)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersDeprecatedKt.runBlockingTest(TestBuildersDeprecated.kt:126)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(Unknown Source:1)
at com.xkcd.comiclist_data.repository.XkcdSettingsRepositoryImplTest.dummyTest(XkcdSettingsRepositoryImplTest.kt:60)
Here is the full XkcdSettingsRepositoryImpl:
package com.xkcd.comiclist_data.repository
import androidx.datastore.core.DataStore
import com.xkcd.comiclist_domain.model.Comic
import com.xkcd.comiclist_domain.model.CurrentXkcdSettings
import com.xkcd.comiclist_domain.repository.XkcdSettingsRepository
import com.xkcd.core.XkcdSettings
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
import kotlin.math.max
class XkcdSettingsRepositoryImpl @Inject constructor(
private val xkcdSettings: DataStore<XkcdSettings>
): XkcdSettingsRepository {
override suspend fun updateUserName(userName: String) {
xkcdSettings.updateData { settings ->
settings.toBuilder().setUserName(userName).build()
}
}
override suspend fun updateLatestComic(comic: Comic) {
xkcdSettings.updateData { settings ->
settings.toBuilder().setLatestComicNum(max(comic.num, settings.latestComicNum.toInt()).toLong()).build()
}
}
override suspend fun updateSelectedTab(selectedTab: XkcdSettings.SelectedTab) {
xkcdSettings.updateData { settings ->
settings.toBuilder().setSelectedComicTab(selectedTab).build()
}
}
override suspend fun updateSelectedComicSort(selectedComicSort: XkcdSettings.SelectedComicSort) {
xkcdSettings.updateData { settings ->
settings.toBuilder().setSelectedComicSort(selectedComicSort).build()
}
}
override suspend fun getXkcdSettings(): Flow<CurrentXkcdSettings> = flow {
xkcdSettings.data.collectLatest {
emit(CurrentXkcdSettings(
userName = it.userName,
latestComicNum = it.latestComicNum.toInt(),
selectedTab = it.selectedComicTab,
selectedComicSort = it.selectedComicSort
))
}
}
}
- Ngôn ngữ chính
- Kotlin
- Star
- 242
- Fork
- 115
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của android/codelab-android-datastore
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
android/codelab-android-datastore#83 · 2 bình luận · 2 reaction ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 55/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
Tất cả issue của android/codelab-android-datastore
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
SimonHalvdansson/Harmonic-HN#399 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
[Request] 李跳跳Đang mởrequest-adaption
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 78/100
BetterAndroid/android-notification-icon-project#799 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
6.4.0快进问题Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
open-ani/animeko#3664 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100