mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
parent
7817518462
commit
9d59915459
41 changed files with 444 additions and 477 deletions
|
|
@ -4,9 +4,8 @@ import categoryItem
|
|||
import com.nhaarman.mockitokotlin2.mock
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import depictedItem
|
||||
import fr.free.nrw.commons.explore.depictions.DepictsClient
|
||||
import fr.free.nrw.commons.upload.GpsCategoryModel
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
|
@ -35,7 +34,7 @@ class CategoriesModelTest {
|
|||
|
||||
val expectedList = listOf("Test")
|
||||
whenever(categoryClient.searchCategoriesForPrefix("tes", 25))
|
||||
.thenReturn(Observable.just(expectedList))
|
||||
.thenReturn(Single.just(expectedList))
|
||||
|
||||
// Checking if both return "Test"
|
||||
val expectedItems = expectedList.map { CategoryItem(it, false) }
|
||||
|
|
@ -56,7 +55,7 @@ class CategoriesModelTest {
|
|||
whenever(gpsCategoryModel.categoriesFromLocation)
|
||||
.thenReturn(BehaviorSubject.createDefault(listOf("gpsCategory")))
|
||||
whenever(categoryClient.searchCategories("tes", 25))
|
||||
.thenReturn(Observable.just(listOf("titleSearch")))
|
||||
.thenReturn(Single.just(listOf("titleSearch")))
|
||||
whenever(categoryDao.recentCategories(25)).thenReturn(listOf("recentCategories"))
|
||||
CategoriesModel(categoryClient, categoryDao, gpsCategoryModel)
|
||||
.searchAll("", listOf("tes"), listOf(depictedItem))
|
||||
|
|
|
|||
|
|
@ -2,11 +2,10 @@ package fr.free.nrw.commons.category
|
|||
|
||||
import com.nhaarman.mockitokotlin2.mock
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers.anyInt
|
||||
import org.mockito.ArgumentMatchers.anyString
|
||||
import org.mockito.ArgumentMatchers.*
|
||||
import org.mockito.InjectMocks
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.mock
|
||||
|
|
@ -32,7 +31,7 @@ class CategoryClientTest {
|
|||
fun searchCategoriesFound() {
|
||||
val mockResponse = withMockResponse("Category:Test")
|
||||
whenever(categoryInterface.searchCategories(anyString(), anyInt(), anyInt()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.searchCategories("tes", 10)
|
||||
.test()
|
||||
.assertValues(listOf("Test"))
|
||||
|
|
@ -45,7 +44,7 @@ class CategoryClientTest {
|
|||
fun searchCategoriesNull() {
|
||||
val mockResponse = withNullPages()
|
||||
whenever(categoryInterface.searchCategories(anyString(), anyInt(), anyInt()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.searchCategories("tes", 10)
|
||||
.test()
|
||||
.assertValues(emptyList())
|
||||
|
|
@ -58,7 +57,7 @@ class CategoryClientTest {
|
|||
fun searchCategoriesForPrefixFound() {
|
||||
val mockResponse = withMockResponse("Category:Test")
|
||||
whenever(categoryInterface.searchCategoriesForPrefix(anyString(), anyInt(), anyInt()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.searchCategoriesForPrefix("tes", 10)
|
||||
.test()
|
||||
.assertValues(listOf("Test"))
|
||||
|
|
@ -71,7 +70,7 @@ class CategoryClientTest {
|
|||
fun searchCategoriesForPrefixNull() {
|
||||
val mockResponse = withNullPages()
|
||||
whenever(categoryInterface.searchCategoriesForPrefix(anyString(), anyInt(), anyInt()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.searchCategoriesForPrefix("tes", 10)
|
||||
.test()
|
||||
.assertValues(emptyList())
|
||||
|
|
@ -83,8 +82,8 @@ class CategoryClientTest {
|
|||
@Test
|
||||
fun getParentCategoryListFound() {
|
||||
val mockResponse = withMockResponse("Category:Test")
|
||||
whenever(categoryInterface.getParentCategoryList(anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
whenever(categoryInterface.getParentCategoryList(anyString(), anyMap()))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.getParentCategoryList("tes")
|
||||
.test()
|
||||
.assertValues(listOf("Test"))
|
||||
|
|
@ -93,8 +92,8 @@ class CategoryClientTest {
|
|||
@Test
|
||||
fun getParentCategoryListNull() {
|
||||
val mockResponse = withNullPages()
|
||||
whenever(categoryInterface.getParentCategoryList(anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
whenever(categoryInterface.getParentCategoryList(anyString(), anyMap()))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.getParentCategoryList("tes")
|
||||
.test()
|
||||
.assertValues(emptyList())
|
||||
|
|
@ -103,8 +102,8 @@ class CategoryClientTest {
|
|||
@Test
|
||||
fun getSubCategoryListFound() {
|
||||
val mockResponse = withMockResponse("Category:Test")
|
||||
whenever(categoryInterface.getSubCategoryList("tes"))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
whenever(categoryInterface.getSubCategoryList("tes", emptyMap()))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.getSubCategoryList("tes")
|
||||
.test()
|
||||
.assertValues(listOf("Test"))
|
||||
|
|
@ -113,8 +112,11 @@ class CategoryClientTest {
|
|||
@Test
|
||||
fun getSubCategoryListNull() {
|
||||
val mockResponse = withNullPages()
|
||||
whenever(categoryInterface.getSubCategoryList(anyString()))
|
||||
.thenReturn(Observable.just(mockResponse))
|
||||
whenever(categoryInterface.getSubCategoryList(
|
||||
anyString(),
|
||||
anyMap()
|
||||
))
|
||||
.thenReturn(Single.just(mockResponse))
|
||||
categoryClient.getSubCategoryList("tes")
|
||||
.test()
|
||||
.assertValues(emptyList())
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package fr.free.nrw.commons.contributions
|
||||
|
||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||
import com.nhaarman.mockitokotlin2.*
|
||||
import com.nhaarman.mockitokotlin2.verify
|
||||
import com.nhaarman.mockitokotlin2.verifyZeroInteractions
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
|
|
@ -58,8 +60,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<Contribution>());
|
||||
verify(mediaClient).getMediaListForUser(anyString());
|
||||
|
|
@ -73,8 +73,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 +86,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,28 +99,14 @@ 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());
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFetchContributionsForEndOfList() {
|
||||
whenever(sessionManager.userName).thenReturn("Test")
|
||||
whenever(mediaClient.doesMediaListForUserHaveMorePages(anyString()))
|
||||
.thenReturn(false)
|
||||
contributionBoundaryCallback.fetchContributions()
|
||||
verify(mediaClient, times(0)).getMediaListForUser(anyString())
|
||||
verifyNoMoreInteractions(repository)
|
||||
}
|
||||
|
||||
@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);
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package fr.free.nrw.commons.explore.categories.sub
|
||||
|
||||
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 PageableSubCategoriesDataSourceTest{
|
||||
@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 =
|
||||
PageableSubCategoriesDataSource(liveDataConverter, categoryClient)
|
||||
dataSource.onQueryUpdated("test")
|
||||
whenever(categoryClient.getSubCategoryList("test"))
|
||||
.thenReturn(Single.just(emptyList()))
|
||||
assertThat(dataSource.loadFunction(-1, 0), `is`(emptyList()))
|
||||
verify(categoryClient).resetSubCategoryContinuation("test")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `loadFunction does not call reset at any other position`() {
|
||||
val dataSource =
|
||||
PageableSubCategoriesDataSource(liveDataConverter, categoryClient)
|
||||
dataSource.onQueryUpdated("test")
|
||||
whenever(categoryClient.getSubCategoryList("test"))
|
||||
.thenReturn(Single.just(emptyList()))
|
||||
assertThat(dataSource.loadFunction(-1, 1), `is`(emptyList()))
|
||||
verify(categoryClient, never()).resetSubCategoryContinuation("test")
|
||||
}
|
||||
}
|
||||
|
|
@ -3,19 +3,20 @@ package fr.free.nrw.commons.explore.categroies
|
|||
import com.nhaarman.mockitokotlin2.mock
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import fr.free.nrw.commons.category.CategoryClient
|
||||
import fr.free.nrw.commons.explore.categories.search.PageableCategoriesDataSource
|
||||
import io.reactivex.Observable
|
||||
import fr.free.nrw.commons.explore.categories.search.PageableSearchCategoriesDataSource
|
||||
import io.reactivex.Single
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.hamcrest.Matchers
|
||||
import org.junit.Test
|
||||
|
||||
class PageableCategoriesDataSourceTest {
|
||||
class PageableSearchCategoriesDataSourceTest {
|
||||
@Test
|
||||
fun `loadFunction loads categories`() {
|
||||
val categoryClient: CategoryClient = mock()
|
||||
whenever(categoryClient.searchCategories("test", 0, 1))
|
||||
.thenReturn(Observable.just(emptyList()))
|
||||
val pageableCategoriesDataSource = PageableCategoriesDataSource(mock(), categoryClient)
|
||||
.thenReturn(Single.just(emptyList()))
|
||||
val pageableCategoriesDataSource =
|
||||
PageableSearchCategoriesDataSource(mock(), categoryClient)
|
||||
pageableCategoriesDataSource.onQueryUpdated("test")
|
||||
assertThat(pageableCategoriesDataSource.loadFunction(0, 1), Matchers.`is`(emptyList()))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue