From f384a688a8c541a038deaf24fd408acb4c1bec25 Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Fri, 19 Jun 2020 09:42:23 +0100 Subject: [PATCH] #3822 Convert SubCategoryImagesListFragment to use Pagination - add tests for ParentCategoriesDataSource --- .../commons/category/CategoryClientTest.kt | 4 +- .../ContributionBoundaryCallbackTest.kt | 12 ----- .../PageableParentCategoriesDataSourceTest.kt | 48 +++++++++++++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSourceTest.kt diff --git a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt index f3620a46b..18298a04d 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoryClientTest.kt @@ -82,7 +82,7 @@ class CategoryClientTest { @Test fun getParentCategoryListFound() { val mockResponse = withMockResponse("Category:Test") - whenever(categoryInterface.getParentCategoryList(anyString())) + whenever(categoryInterface.getParentCategoryList(anyString(), anyMap())) .thenReturn(Single.just(mockResponse)) categoryClient.getParentCategoryList("tes") .test() @@ -92,7 +92,7 @@ class CategoryClientTest { @Test fun getParentCategoryListNull() { val mockResponse = withNullPages() - whenever(categoryInterface.getParentCategoryList(anyString())) + whenever(categoryInterface.getParentCategoryList(anyString(), anyMap())) .thenReturn(Single.just(mockResponse)) categoryClient.getParentCategoryList("tes") .test() diff --git a/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionBoundaryCallbackTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionBoundaryCallbackTest.kt index 365c0ae7f..2c8ca067f 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionBoundaryCallbackTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionBoundaryCallbackTest.kt @@ -58,8 +58,6 @@ class ContributionBoundaryCallbackTest { whenever(mediaClient.getMediaListForUser(anyString())).thenReturn( Single.just(listOf(mock(Media::class.java))) ) - whenever(mediaClient.doesMediaListForUserHaveMorePages(anyString())) - .thenReturn(true) contributionBoundaryCallback.onZeroItemsLoaded() verify(repository).save(anyList()); verify(mediaClient).getMediaListForUser(anyString()); @@ -73,8 +71,6 @@ class ContributionBoundaryCallbackTest { whenever(mediaClient.getMediaListForUser(anyString())).thenReturn( Single.just(listOf(mock(Media::class.java))) ) - whenever(mediaClient.doesMediaListForUserHaveMorePages(anyString())) - .thenReturn(true) contributionBoundaryCallback.onItemAtEndLoaded(mock(Contribution::class.java)) verify(repository).save(anyList()); verify(mediaClient).getMediaListForUser(anyString()); @@ -88,8 +84,6 @@ class ContributionBoundaryCallbackTest { whenever(mediaClient.getMediaListForUser(anyString())).thenReturn( Single.just(listOf(mock(Media::class.java))) ) - whenever(mediaClient.doesMediaListForUserHaveMorePages(anyString())) - .thenReturn(true) contributionBoundaryCallback.onItemAtFrontLoaded(mock(Contribution::class.java)) verify(repository).save(anyList()); verify(mediaClient).getMediaListForUser(anyString()); @@ -103,8 +97,6 @@ class ContributionBoundaryCallbackTest { whenever(mediaClient.getMediaListForUser(anyString())).thenReturn( Single.just(listOf(mock(Media::class.java))) ) - whenever(mediaClient.doesMediaListForUserHaveMorePages(anyString())) - .thenReturn(true) contributionBoundaryCallback.fetchContributions() verify(repository).save(anyList()); verify(mediaClient).getMediaListForUser(anyString()); @@ -113,8 +105,6 @@ class ContributionBoundaryCallbackTest { @Test fun testFetchContributionsForEndOfList() { whenever(sessionManager.userName).thenReturn("Test") - whenever(mediaClient.doesMediaListForUserHaveMorePages(anyString())) - .thenReturn(false) contributionBoundaryCallback.fetchContributions() verify(mediaClient, times(0)).getMediaListForUser(anyString()) verifyNoMoreInteractions(repository) @@ -123,8 +113,6 @@ class ContributionBoundaryCallbackTest { @Test fun testFetchContributionsFailed() { whenever(sessionManager.userName).thenReturn("Test") - whenever(mediaClient.doesMediaListForUserHaveMorePages(anyString())) - .thenReturn(true) whenever(mediaClient.getMediaListForUser(anyString())).thenReturn(Single.error(Exception("Error"))) contributionBoundaryCallback.fetchContributions() verifyZeroInteractions(repository); diff --git a/app/src/test/kotlin/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSourceTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSourceTest.kt new file mode 100644 index 000000000..ca7b85de6 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/explore/categories/parent/PageableParentCategoriesDataSourceTest.kt @@ -0,0 +1,48 @@ +package fr.free.nrw.commons.explore.categories.parent + +import com.nhaarman.mockitokotlin2.never +import com.nhaarman.mockitokotlin2.verify +import com.nhaarman.mockitokotlin2.whenever +import fr.free.nrw.commons.category.CategoryClient +import fr.free.nrw.commons.explore.paging.LiveDataConverter +import io.reactivex.Single +import org.hamcrest.CoreMatchers.`is` +import org.hamcrest.MatcherAssert.assertThat +import org.junit.Before +import org.junit.Test +import org.mockito.Mock +import org.mockito.MockitoAnnotations + +class PageableParentCategoriesDataSourceTest{ + @Mock + lateinit var categoryClient: CategoryClient + @Mock + lateinit var liveDataConverter: LiveDataConverter + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + } + + @Test + fun `loadFunction calls reset at position 0`() { + val dataSource = + PageableParentCategoriesDataSource(liveDataConverter, categoryClient) + dataSource.onQueryUpdated("test") + whenever(categoryClient.getParentCategoryList("test")) + .thenReturn(Single.just(emptyList())) + assertThat(dataSource.loadFunction(-1, 0), `is`(emptyList())) + verify(categoryClient).resetParentCategoryContinuation("test") + } + + @Test + fun `loadFunction does not call reset at any other position`() { + val dataSource = + PageableParentCategoriesDataSource(liveDataConverter, categoryClient) + dataSource.onQueryUpdated("test") + whenever(categoryClient.getParentCategoryList("test")) + .thenReturn(Single.just(emptyList())) + assertThat(dataSource.loadFunction(-1, 1), `is`(emptyList())) + verify(categoryClient, never()).resetParentCategoryContinuation("test") + } +}