Issue#11 Adjust FavouriteContentLocalStorage by using runTest instead of deprecated TestDispatchers
This commit is contained in:
parent
968ccb647d
commit
3c85431d96
2 changed files with 63 additions and 63 deletions
|
|
@ -8,8 +8,11 @@ import kotlinx.coroutines.async
|
|||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.take
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.TestCoroutineScheduler
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import kotlinx.coroutines.test.advanceUntilIdle
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.fnives.test.showcase.core.storage.content.FavouriteContentLocalStorage
|
||||
import org.fnives.test.showcase.model.content.ContentId
|
||||
import org.fnives.test.showcase.storage.database.DatabaseInitialization
|
||||
|
|
@ -31,18 +34,18 @@ internal class FavouriteContentLocalStorageImplTest {
|
|||
|
||||
@Inject
|
||||
lateinit var sut: FavouriteContentLocalStorage
|
||||
private lateinit var testDispatcher: TestCoroutineDispatcher
|
||||
private lateinit var testDispatcher: TestDispatcher
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
testDispatcher = TestCoroutineDispatcher()
|
||||
testDispatcher = StandardTestDispatcher(TestCoroutineScheduler())
|
||||
DatabaseInitialization.dispatcher = testDispatcher
|
||||
hiltRule.inject()
|
||||
}
|
||||
|
||||
/** GIVEN content_id WHEN added to Favourite THEN it can be read out */
|
||||
@Test
|
||||
fun addingContentIdToFavouriteCanBeLaterReadOut() = runBlocking {
|
||||
fun addingContentIdToFavouriteCanBeLaterReadOut() = runTest(testDispatcher) {
|
||||
val expected = listOf(ContentId("a"))
|
||||
|
||||
sut.markAsFavourite(ContentId("a"))
|
||||
|
|
@ -53,8 +56,7 @@ internal class FavouriteContentLocalStorageImplTest {
|
|||
|
||||
/** GIVEN content_id added WHEN removed to Favourite THEN it no longer can be read out */
|
||||
@Test
|
||||
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() =
|
||||
runBlocking {
|
||||
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() = runTest(testDispatcher) {
|
||||
val expected = listOf<ContentId>()
|
||||
sut.markAsFavourite(ContentId("b"))
|
||||
|
||||
|
|
@ -66,35 +68,33 @@ internal class FavouriteContentLocalStorageImplTest {
|
|||
|
||||
/** GIVEN empty database WHILE observing content WHEN favourite added THEN change is emitted */
|
||||
@Test
|
||||
fun addingFavouriteUpdatesExistingObservers() =
|
||||
runBlocking<Unit> {
|
||||
fun addingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||
val expected = listOf(listOf(), listOf(ContentId("a")))
|
||||
|
||||
val testDispatcher = TestCoroutineDispatcher()
|
||||
val actual = async(testDispatcher) {
|
||||
val actual = async(coroutineContext) {
|
||||
sut.observeFavourites().take(2).toList()
|
||||
}
|
||||
testDispatcher.advanceUntilIdle()
|
||||
advanceUntilIdle()
|
||||
|
||||
sut.markAsFavourite(ContentId("a"))
|
||||
advanceUntilIdle()
|
||||
|
||||
Assert.assertEquals(expected, actual.getCompleted())
|
||||
}
|
||||
|
||||
/** GIVEN non empty database WHILE observing content WHEN favourite removed THEN change is emitted */
|
||||
@Test
|
||||
fun removingFavouriteUpdatesExistingObservers() =
|
||||
runBlocking<Unit> {
|
||||
fun removingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||
val expected = listOf(listOf(ContentId("a")), listOf())
|
||||
sut.markAsFavourite(ContentId("a"))
|
||||
|
||||
val testDispatcher = TestCoroutineDispatcher()
|
||||
val actual = async(testDispatcher) {
|
||||
val actual = async(coroutineContext) {
|
||||
sut.observeFavourites().take(2).toList()
|
||||
}
|
||||
testDispatcher.advanceUntilIdle()
|
||||
advanceUntilIdle()
|
||||
|
||||
sut.deleteAsFavourite(ContentId("a"))
|
||||
advanceUntilIdle()
|
||||
|
||||
Assert.assertEquals(expected, actual.getCompleted())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,11 @@ import kotlinx.coroutines.async
|
|||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.take
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.TestCoroutineScheduler
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import kotlinx.coroutines.test.advanceUntilIdle
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.fnives.test.showcase.core.storage.content.FavouriteContentLocalStorage
|
||||
import org.fnives.test.showcase.model.content.ContentId
|
||||
import org.fnives.test.showcase.storage.database.DatabaseInitialization
|
||||
|
|
@ -26,11 +29,11 @@ import org.koin.test.inject
|
|||
internal class FavouriteContentLocalStorageImplTest : KoinTest {
|
||||
|
||||
private val sut by inject<FavouriteContentLocalStorage>()
|
||||
private lateinit var testDispatcher: TestCoroutineDispatcher
|
||||
private lateinit var testDispatcher: TestDispatcher
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
testDispatcher = TestCoroutineDispatcher()
|
||||
testDispatcher = StandardTestDispatcher(TestCoroutineScheduler())
|
||||
DatabaseInitialization.dispatcher = testDispatcher
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +44,7 @@ internal class FavouriteContentLocalStorageImplTest : KoinTest {
|
|||
|
||||
/** GIVEN content_id WHEN added to Favourite THEN it can be read out */
|
||||
@Test
|
||||
fun addingContentIdToFavouriteCanBeLaterReadOut() = runBlocking {
|
||||
fun addingContentIdToFavouriteCanBeLaterReadOut() = runTest(testDispatcher) {
|
||||
val expected = listOf(ContentId("a"))
|
||||
|
||||
sut.markAsFavourite(ContentId("a"))
|
||||
|
|
@ -52,8 +55,7 @@ internal class FavouriteContentLocalStorageImplTest : KoinTest {
|
|||
|
||||
/** GIVEN content_id added WHEN removed to Favourite THEN it no longer can be read out */
|
||||
@Test
|
||||
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() =
|
||||
runBlocking {
|
||||
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() = runTest(testDispatcher) {
|
||||
val expected = listOf<ContentId>()
|
||||
sut.markAsFavourite(ContentId("b"))
|
||||
|
||||
|
|
@ -65,36 +67,34 @@ internal class FavouriteContentLocalStorageImplTest : KoinTest {
|
|||
|
||||
/** GIVEN empty database WHILE observing content WHEN favourite added THEN change is emitted */
|
||||
@Test
|
||||
fun addingFavouriteUpdatesExistingObservers() =
|
||||
runBlocking<Unit> {
|
||||
fun addingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||
val expected = listOf(listOf(), listOf(ContentId("a")))
|
||||
|
||||
val testDispatcher = TestCoroutineDispatcher()
|
||||
val actual = async(testDispatcher) {
|
||||
val actual = async(coroutineContext) {
|
||||
sut.observeFavourites().take(2).toList()
|
||||
}
|
||||
testDispatcher.advanceUntilIdle()
|
||||
advanceUntilIdle()
|
||||
|
||||
sut.markAsFavourite(ContentId("a"))
|
||||
advanceUntilIdle()
|
||||
|
||||
Assert.assertEquals(expected, actual.await())
|
||||
Assert.assertEquals(expected, actual.getCompleted())
|
||||
}
|
||||
|
||||
/** GIVEN non empty database WHILE observing content WHEN favourite removed THEN change is emitted */
|
||||
@Test
|
||||
fun removingFavouriteUpdatesExistingObservers() =
|
||||
runBlocking<Unit> {
|
||||
fun removingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||
val expected = listOf(listOf(ContentId("a")), listOf())
|
||||
sut.markAsFavourite(ContentId("a"))
|
||||
|
||||
val testDispatcher = TestCoroutineDispatcher()
|
||||
val actual = async(testDispatcher) {
|
||||
val actual = async(coroutineContext) {
|
||||
sut.observeFavourites().take(2).toList()
|
||||
}
|
||||
testDispatcher.advanceUntilIdle()
|
||||
advanceUntilIdle()
|
||||
|
||||
sut.deleteAsFavourite(ContentId("a"))
|
||||
advanceUntilIdle()
|
||||
|
||||
Assert.assertEquals(expected, actual.await())
|
||||
Assert.assertEquals(expected, actual.getCompleted())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue