-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add Unit tests PixabayImagePagingSourceTest.
- Loading branch information
1 parent
a3eab0a
commit 9f28ab4
Showing
14 changed files
with
1,181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"total": 7, | ||
"totalHits": 7, | ||
"hits": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"total": 7, | ||
"totalHits": 7, | ||
"hits": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import androidx.annotation.VisibleForTesting | ||
import com.wei.picquest.core.network.fake.FakeAssetManager | ||
import java.io.File | ||
import java.io.InputStream | ||
import java.util.Properties | ||
|
||
/** | ||
* This class helps with loading Android `/assets` files, especially when running JVM unit tests. | ||
* It must remain on the root package for an easier [Class.getResource] with relative paths. | ||
* @see <a href="https://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/UnitTestOptions">UnitTestOptions</a> | ||
*/ | ||
@VisibleForTesting | ||
internal object JvmUnitTestFakeAssetManager : FakeAssetManager { | ||
private val config = | ||
requireNotNull(javaClass.getResource("com/android/tools/test_config.properties")) { | ||
""" | ||
Missing Android resources properties file. | ||
Did you forget to enable the feature in the gradle build file? | ||
android.testOptions.unitTests.isIncludeAndroidResources = true | ||
""".trimIndent() | ||
} | ||
private val properties = Properties().apply { config.openStream().use(::load) } | ||
private val assets = File(properties["android_merged_assets"].toString()) | ||
|
||
override fun open(fileName: String): InputStream = File(assets, fileName).inputStream() | ||
} |
7 changes: 7 additions & 0 deletions
7
core/network/src/main/java/com/wei/picquest/core/network/fake/FakeAssetManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.wei.picquest.core.network.fake | ||
|
||
import java.io.InputStream | ||
|
||
fun interface FakeAssetManager { | ||
fun open(fileName: String): InputStream | ||
} |
56 changes: 56 additions & 0 deletions
56
core/network/src/main/java/com/wei/picquest/core/network/fake/FakePqNetworkDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.wei.picquest.core.network.fake | ||
|
||
import JvmUnitTestFakeAssetManager | ||
import com.wei.picquest.core.network.Dispatcher | ||
import com.wei.picquest.core.network.PqDispatchers | ||
import com.wei.picquest.core.network.PqNetworkDataSource | ||
import com.wei.picquest.core.network.model.NetworkSearchImages | ||
import com.wei.picquest.core.network.model.NetworkSearchVideos | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromStream | ||
import org.jetbrains.annotations.VisibleForTesting | ||
|
||
class FakePqNetworkDataSource( | ||
@Dispatcher(PqDispatchers.IO) private val ioDispatcher: CoroutineDispatcher, | ||
private val networkJson: Json, | ||
private val assets: FakeAssetManager = JvmUnitTestFakeAssetManager, | ||
) : PqNetworkDataSource { | ||
|
||
private var shouldReturnErrorForImages = false | ||
|
||
@VisibleForTesting | ||
fun setReturnErrorForImages(shouldReturnError: Boolean) { | ||
shouldReturnErrorForImages = shouldReturnError | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
override suspend fun searchImages(query: String, page: Int, perPage: Int): NetworkSearchImages = | ||
withContext(ioDispatcher) { | ||
if (shouldReturnErrorForImages) { | ||
throw Exception("Fake exception for images") | ||
} | ||
when (page) { | ||
0 -> assets.open(IMAGES_PAGE1_ASSET).use(networkJson::decodeFromStream) | ||
1 -> assets.open(IMAGES_PAGE2_ASSET).use(networkJson::decodeFromStream) | ||
else -> { | ||
assets.open(IMAGES_PAGE_END_ASSET).use(networkJson::decodeFromStream) | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
override suspend fun searchVideos(query: String, page: Int, perPage: Int): NetworkSearchVideos = | ||
withContext(ioDispatcher) { | ||
assets.open(VIDEOS_PAGE_END_ASSET).use(networkJson::decodeFromStream) | ||
} | ||
|
||
companion object { | ||
private const val IMAGES_PAGE1_ASSET = "images_page1.json" | ||
private const val IMAGES_PAGE2_ASSET = "images_page2.json" | ||
private const val IMAGES_PAGE_END_ASSET = "images_page_end.json" | ||
private const val VIDEOS_PAGE_END_ASSET = "videos_page_end.json" | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../pagingsource/PixabayImagePagingSource.kt → .../pagingsource/PixabayImagePagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../pagingsource/PixabayVideoPagingSource.kt → .../pagingsource/PixabayVideoPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
.../src/test/java/com/wei/picquest/core/network/pagingsource/PixabayImagePagingSourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.wei.picquest.core.network.pagingsource | ||
|
||
import JvmUnitTestFakeAssetManager | ||
import androidx.paging.PagingSource | ||
import com.google.common.truth.Truth.assertThat | ||
import com.wei.picquest.core.network.fake.FakePqNetworkDataSource | ||
import kotlinx.coroutines.test.StandardTestDispatcher | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.serialization.json.Json | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
/** | ||
* Unit tests for [PixabayImagePagingSource]. | ||
* | ||
* 遵循此模型,安排、操作、斷言: | ||
* {Arrange}{Act}{Assert} | ||
*/ | ||
class PixabayImagePagingSourceTest { | ||
|
||
private lateinit var fakePqNetworkDataSource: FakePqNetworkDataSource | ||
private lateinit var pixabayImagePagingSource: PixabayImagePagingSource | ||
|
||
private val testDispatcher = StandardTestDispatcher() | ||
|
||
@Before | ||
fun setUp() { | ||
fakePqNetworkDataSource = FakePqNetworkDataSource( | ||
ioDispatcher = testDispatcher, | ||
networkJson = Json { ignoreUnknownKeys = true }, | ||
assets = JvmUnitTestFakeAssetManager, | ||
) | ||
pixabayImagePagingSource = PixabayImagePagingSource(fakePqNetworkDataSource, "testQuery") | ||
} | ||
|
||
@Test | ||
fun `load should return non-empty page for first and second page`() = runTest(testDispatcher) { | ||
// Arrange | ||
val firstPageKey = 0 | ||
val secondPageKey = 1 | ||
|
||
// Act | ||
val firstPageResult = pixabayImagePagingSource.load( | ||
PagingSource.LoadParams.Refresh( | ||
key = firstPageKey, | ||
loadSize = 20, | ||
placeholdersEnabled = false, | ||
), | ||
) | ||
val secondPageResult = pixabayImagePagingSource.load( | ||
PagingSource.LoadParams.Refresh( | ||
key = secondPageKey, | ||
loadSize = 20, | ||
placeholdersEnabled = false, | ||
), | ||
) | ||
|
||
// Assert | ||
assertThat(firstPageResult).isInstanceOf(PagingSource.LoadResult.Page::class.java) | ||
assertThat((firstPageResult as PagingSource.LoadResult.Page).data).isNotEmpty() | ||
assertThat(secondPageResult).isInstanceOf(PagingSource.LoadResult.Page::class.java) | ||
assertThat((secondPageResult as PagingSource.LoadResult.Page).data).isNotEmpty() | ||
} | ||
|
||
@Test | ||
fun `load should return empty page at end of pagination`() = runTest(testDispatcher) { | ||
// Arrange | ||
val endPageKey = 2 // Assuming this is the end | ||
|
||
// Act | ||
val endPageResult = pixabayImagePagingSource.load( | ||
PagingSource.LoadParams.Refresh( | ||
key = endPageKey, | ||
loadSize = 20, | ||
placeholdersEnabled = false, | ||
), | ||
) | ||
|
||
// Assert | ||
assertThat(endPageResult).isInstanceOf(PagingSource.LoadResult.Page::class.java) | ||
assertThat((endPageResult as PagingSource.LoadResult.Page).data).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `load should return error when data source throws exception`() = runTest { | ||
// Arrange | ||
fakePqNetworkDataSource.setReturnErrorForImages(true) | ||
|
||
// Act | ||
val result = pixabayImagePagingSource.load( | ||
PagingSource.LoadParams.Refresh( | ||
key = 0, | ||
loadSize = 20, | ||
placeholdersEnabled = false, | ||
), | ||
) | ||
|
||
// Assert | ||
assertThat(result).isInstanceOf(PagingSource.LoadResult.Error::class.java) | ||
} | ||
} |