From 936e50d327e8918681c21bdc6aca3df8e29f1c61 Mon Sep 17 00:00:00 2001 From: savsch Date: Wed, 15 Jan 2025 00:03:07 +0530 Subject: [PATCH] Add tests --- .../fr/free/nrw/commons/ModelFunctions.kt | 4 +- .../commons/category/CategoriesModelTest.kt | 52 +++++++++++++--- .../commons/category/CategoryClientTest.kt | 62 +++++++++++++++++++ 3 files changed, 108 insertions(+), 10 deletions(-) diff --git a/app/src/test/kotlin/fr/free/nrw/commons/ModelFunctions.kt b/app/src/test/kotlin/fr/free/nrw/commons/ModelFunctions.kt index 76f9c21d0..816eb53c1 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/ModelFunctions.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/ModelFunctions.kt @@ -18,7 +18,7 @@ import java.util.Date fun depictedItem( name: String = "label", description: String = "desc", - imageUrl: String = "", + primaryImage: String = "", instanceOfs: List = listOf(), commonsCategories: List = listOf(), isSelected: Boolean = false, @@ -26,7 +26,7 @@ fun depictedItem( ) = DepictedItem( name = name, description = description, - imageUrl = imageUrl, + primaryImage = primaryImage, instanceOfs = instanceOfs, commonsCategories = commonsCategories, isSelected = isSelected, diff --git a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt index cc5340fbb..69d5ee7a3 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt @@ -1,7 +1,9 @@ package fr.free.nrw.commons.category import categoryItem +import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.times import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever import depictedItem @@ -90,14 +92,18 @@ class CategoriesModelTest { val depictedItem = depictedItem( commonsCategories = - listOf( - CategoryItem( - "depictionCategory", - "", - "", - false, - ), + listOf( + CategoryItem( + "depictionCategory", + "", + "", + false, ), + ), + ) + val depictedItemWithoutCategories = + depictedItem( + primaryImage = "P18" ) whenever(gpsCategoryModel.categoriesFromLocation) @@ -159,6 +165,23 @@ class CategoriesModelTest { ), ), ) + whenever( + categoryClient.getCategoriesOfImage( + "P18", + 25, + ), + ).thenReturn( + Single.just( + listOf( + CategoryItem( + "categoriesOfP18", + "", + "", + false, + ), + ), + ), + ) val imageTitleList = listOf("Test") CategoriesModel(categoryClient, categoryDao, gpsCategoryModel) .searchAll("", imageTitleList, listOf(depictedItem)) @@ -171,8 +194,21 @@ class CategoriesModelTest { categoryItem("recentCategories"), ), ) + CategoriesModel(categoryClient, categoryDao, gpsCategoryModel) + .searchAll("", imageTitleList, listOf(depictedItemWithoutCategories)) + .test() + .assertValue( + listOf( + categoryItem("categoriesOfP18"), + categoryItem("gpsCategory"), + categoryItem("titleSearch"), + categoryItem("recentCategories"), + ), + ) imageTitleList.forEach { - verify(categoryClient).searchCategories(it, CategoriesModel.SEARCH_CATS_LIMIT) + verify(categoryClient, times(2)).searchCategories(it, CategoriesModel.SEARCH_CATS_LIMIT) + verify(categoryClient).getCategoriesByName(any(), any(), any(), any()) + verify(categoryClient).getCategoriesOfImage(any(), any()) } } 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 5c95215a8..5edf55f6e 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 @@ -132,6 +132,45 @@ class CategoryClientTest { ) } + @Test + fun getCategoriesByTitlesFound() { + val mockResponse = withMockResponse("Category:Test") + whenever( + categoryInterface.getCategoriesByTitles( + anyString(), + anyInt(), + ), + ).thenReturn(Single.just(mockResponse)) + categoryClient + .getCategoriesOfImage("tes", 10) + .test() + .assertValues( + listOf( + CategoryItem( + "Test", + "", + "", + false, + ), + ), + ) + categoryClient + .getCategoriesOfImage( + "tes", + 10, + ).test() + .assertValues( + listOf( + CategoryItem( + "Test", + "", + "", + false, + ), + ), + ) + } + @Test fun getCategoriesByNameNull() { val mockResponse = withNullPages() @@ -160,6 +199,29 @@ class CategoryClientTest { .assertValues(emptyList()) } + @Test + fun getCategoriesByTitlesNull() { + val mockResponse = withNullPages() + whenever( + categoryInterface.getCategoriesByTitles( + anyString(), + anyInt(), + ), + ).thenReturn(Single.just(mockResponse)) + categoryClient + .getCategoriesOfImage( + "tes", + 10, + ).test() + .assertValues(emptyList()) + categoryClient + .getCategoriesOfImage( + "tes", + 10, + ).test() + .assertValues(emptyList()) + } + @Test fun getParentCategoryListFound() { val mockResponse = withMockResponse("Category:Test")