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.first
|
||||||
import kotlinx.coroutines.flow.take
|
import kotlinx.coroutines.flow.take
|
||||||
import kotlinx.coroutines.flow.toList
|
import kotlinx.coroutines.flow.toList
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||||
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
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.core.storage.content.FavouriteContentLocalStorage
|
||||||
import org.fnives.test.showcase.model.content.ContentId
|
import org.fnives.test.showcase.model.content.ContentId
|
||||||
import org.fnives.test.showcase.storage.database.DatabaseInitialization
|
import org.fnives.test.showcase.storage.database.DatabaseInitialization
|
||||||
|
|
@ -31,18 +34,18 @@ internal class FavouriteContentLocalStorageImplTest {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var sut: FavouriteContentLocalStorage
|
lateinit var sut: FavouriteContentLocalStorage
|
||||||
private lateinit var testDispatcher: TestCoroutineDispatcher
|
private lateinit var testDispatcher: TestDispatcher
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
testDispatcher = TestCoroutineDispatcher()
|
testDispatcher = StandardTestDispatcher(TestCoroutineScheduler())
|
||||||
DatabaseInitialization.dispatcher = testDispatcher
|
DatabaseInitialization.dispatcher = testDispatcher
|
||||||
hiltRule.inject()
|
hiltRule.inject()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** GIVEN content_id WHEN added to Favourite THEN it can be read out */
|
/** GIVEN content_id WHEN added to Favourite THEN it can be read out */
|
||||||
@Test
|
@Test
|
||||||
fun addingContentIdToFavouriteCanBeLaterReadOut() = runBlocking {
|
fun addingContentIdToFavouriteCanBeLaterReadOut() = runTest(testDispatcher) {
|
||||||
val expected = listOf(ContentId("a"))
|
val expected = listOf(ContentId("a"))
|
||||||
|
|
||||||
sut.markAsFavourite(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 */
|
/** GIVEN content_id added WHEN removed to Favourite THEN it no longer can be read out */
|
||||||
@Test
|
@Test
|
||||||
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() =
|
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() = runTest(testDispatcher) {
|
||||||
runBlocking {
|
|
||||||
val expected = listOf<ContentId>()
|
val expected = listOf<ContentId>()
|
||||||
sut.markAsFavourite(ContentId("b"))
|
sut.markAsFavourite(ContentId("b"))
|
||||||
|
|
||||||
|
|
@ -66,35 +68,33 @@ internal class FavouriteContentLocalStorageImplTest {
|
||||||
|
|
||||||
/** GIVEN empty database WHILE observing content WHEN favourite added THEN change is emitted */
|
/** GIVEN empty database WHILE observing content WHEN favourite added THEN change is emitted */
|
||||||
@Test
|
@Test
|
||||||
fun addingFavouriteUpdatesExistingObservers() =
|
fun addingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||||
runBlocking<Unit> {
|
|
||||||
val expected = listOf(listOf(), listOf(ContentId("a")))
|
val expected = listOf(listOf(), listOf(ContentId("a")))
|
||||||
|
|
||||||
val testDispatcher = TestCoroutineDispatcher()
|
val actual = async(coroutineContext) {
|
||||||
val actual = async(testDispatcher) {
|
|
||||||
sut.observeFavourites().take(2).toList()
|
sut.observeFavourites().take(2).toList()
|
||||||
}
|
}
|
||||||
testDispatcher.advanceUntilIdle()
|
advanceUntilIdle()
|
||||||
|
|
||||||
sut.markAsFavourite(ContentId("a"))
|
sut.markAsFavourite(ContentId("a"))
|
||||||
|
advanceUntilIdle()
|
||||||
|
|
||||||
Assert.assertEquals(expected, actual.getCompleted())
|
Assert.assertEquals(expected, actual.getCompleted())
|
||||||
}
|
}
|
||||||
|
|
||||||
/** GIVEN non empty database WHILE observing content WHEN favourite removed THEN change is emitted */
|
/** GIVEN non empty database WHILE observing content WHEN favourite removed THEN change is emitted */
|
||||||
@Test
|
@Test
|
||||||
fun removingFavouriteUpdatesExistingObservers() =
|
fun removingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||||
runBlocking<Unit> {
|
|
||||||
val expected = listOf(listOf(ContentId("a")), listOf())
|
val expected = listOf(listOf(ContentId("a")), listOf())
|
||||||
sut.markAsFavourite(ContentId("a"))
|
sut.markAsFavourite(ContentId("a"))
|
||||||
|
|
||||||
val testDispatcher = TestCoroutineDispatcher()
|
val actual = async(coroutineContext) {
|
||||||
val actual = async(testDispatcher) {
|
|
||||||
sut.observeFavourites().take(2).toList()
|
sut.observeFavourites().take(2).toList()
|
||||||
}
|
}
|
||||||
testDispatcher.advanceUntilIdle()
|
advanceUntilIdle()
|
||||||
|
|
||||||
sut.deleteAsFavourite(ContentId("a"))
|
sut.deleteAsFavourite(ContentId("a"))
|
||||||
|
advanceUntilIdle()
|
||||||
|
|
||||||
Assert.assertEquals(expected, actual.getCompleted())
|
Assert.assertEquals(expected, actual.getCompleted())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,11 @@ import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.take
|
import kotlinx.coroutines.flow.take
|
||||||
import kotlinx.coroutines.flow.toList
|
import kotlinx.coroutines.flow.toList
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||||
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
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.core.storage.content.FavouriteContentLocalStorage
|
||||||
import org.fnives.test.showcase.model.content.ContentId
|
import org.fnives.test.showcase.model.content.ContentId
|
||||||
import org.fnives.test.showcase.storage.database.DatabaseInitialization
|
import org.fnives.test.showcase.storage.database.DatabaseInitialization
|
||||||
|
|
@ -26,11 +29,11 @@ import org.koin.test.inject
|
||||||
internal class FavouriteContentLocalStorageImplTest : KoinTest {
|
internal class FavouriteContentLocalStorageImplTest : KoinTest {
|
||||||
|
|
||||||
private val sut by inject<FavouriteContentLocalStorage>()
|
private val sut by inject<FavouriteContentLocalStorage>()
|
||||||
private lateinit var testDispatcher: TestCoroutineDispatcher
|
private lateinit var testDispatcher: TestDispatcher
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
testDispatcher = TestCoroutineDispatcher()
|
testDispatcher = StandardTestDispatcher(TestCoroutineScheduler())
|
||||||
DatabaseInitialization.dispatcher = testDispatcher
|
DatabaseInitialization.dispatcher = testDispatcher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,7 +44,7 @@ internal class FavouriteContentLocalStorageImplTest : KoinTest {
|
||||||
|
|
||||||
/** GIVEN content_id WHEN added to Favourite THEN it can be read out */
|
/** GIVEN content_id WHEN added to Favourite THEN it can be read out */
|
||||||
@Test
|
@Test
|
||||||
fun addingContentIdToFavouriteCanBeLaterReadOut() = runBlocking {
|
fun addingContentIdToFavouriteCanBeLaterReadOut() = runTest(testDispatcher) {
|
||||||
val expected = listOf(ContentId("a"))
|
val expected = listOf(ContentId("a"))
|
||||||
|
|
||||||
sut.markAsFavourite(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 */
|
/** GIVEN content_id added WHEN removed to Favourite THEN it no longer can be read out */
|
||||||
@Test
|
@Test
|
||||||
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() =
|
fun contentIdAddedThenRemovedCanNoLongerBeReadOut() = runTest(testDispatcher) {
|
||||||
runBlocking {
|
|
||||||
val expected = listOf<ContentId>()
|
val expected = listOf<ContentId>()
|
||||||
sut.markAsFavourite(ContentId("b"))
|
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 */
|
/** GIVEN empty database WHILE observing content WHEN favourite added THEN change is emitted */
|
||||||
@Test
|
@Test
|
||||||
fun addingFavouriteUpdatesExistingObservers() =
|
fun addingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||||
runBlocking<Unit> {
|
|
||||||
val expected = listOf(listOf(), listOf(ContentId("a")))
|
val expected = listOf(listOf(), listOf(ContentId("a")))
|
||||||
|
|
||||||
val testDispatcher = TestCoroutineDispatcher()
|
val actual = async(coroutineContext) {
|
||||||
val actual = async(testDispatcher) {
|
|
||||||
sut.observeFavourites().take(2).toList()
|
sut.observeFavourites().take(2).toList()
|
||||||
}
|
}
|
||||||
testDispatcher.advanceUntilIdle()
|
advanceUntilIdle()
|
||||||
|
|
||||||
sut.markAsFavourite(ContentId("a"))
|
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 */
|
/** GIVEN non empty database WHILE observing content WHEN favourite removed THEN change is emitted */
|
||||||
@Test
|
@Test
|
||||||
fun removingFavouriteUpdatesExistingObservers() =
|
fun removingFavouriteUpdatesExistingObservers() = runTest(testDispatcher) {
|
||||||
runBlocking<Unit> {
|
|
||||||
val expected = listOf(listOf(ContentId("a")), listOf())
|
val expected = listOf(listOf(ContentId("a")), listOf())
|
||||||
sut.markAsFavourite(ContentId("a"))
|
sut.markAsFavourite(ContentId("a"))
|
||||||
|
|
||||||
val testDispatcher = TestCoroutineDispatcher()
|
val actual = async(coroutineContext) {
|
||||||
val actual = async(testDispatcher) {
|
|
||||||
sut.observeFavourites().take(2).toList()
|
sut.observeFavourites().take(2).toList()
|
||||||
}
|
}
|
||||||
testDispatcher.advanceUntilIdle()
|
advanceUntilIdle()
|
||||||
|
|
||||||
sut.deleteAsFavourite(ContentId("a"))
|
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