Skip to content

Commit 4109d23

Browse files
Bugfix/categories search (commons-app#3913)
* Fixes commons-app#3734 * Donot ignore case while searching categories * Fixed test-cases to ensure search terms are passed as it is to the CategoryClient * Used a First_Char_Caps title list term just to ensure test case tests as intended * Fixed searchAll with empty term test case
1 parent 7f90361 commit 4109d23

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

app/src/main/java/fr/free/nrw/commons/category/CategoriesModel.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CategoriesModel @Inject constructor(
9191
Function4(::combine)
9292
)
9393
else
94-
categoryClient.searchCategoriesForPrefix(term.toLowerCase(), SEARCH_CATS_LIMIT)
94+
categoryClient.searchCategoriesForPrefix(term, SEARCH_CATS_LIMIT)
9595
.map { it.sortedWith(StringSortingUtils.sortBySimilarity(term)) }
9696
.toObservable()
9797
}
@@ -122,12 +122,11 @@ class CategoriesModel @Inject constructor(
122122

123123
/**
124124
* Return category for single title
125-
* title is converted to lower case to make search case-insensitive
126125
* @param title
127126
* @return
128127
*/
129128
private fun getTitleCategories(title: String): Observable<List<String>> {
130-
return categoryClient.searchCategories(title.toLowerCase(), SEARCH_CATS_LIMIT).toObservable()
129+
return categoryClient.searchCategories(title, SEARCH_CATS_LIMIT).toObservable()
131130
}
132131

133132

@@ -160,6 +159,6 @@ class CategoriesModel @Inject constructor(
160159
}
161160

162161
companion object {
163-
private const val SEARCH_CATS_LIMIT = 25
162+
const val SEARCH_CATS_LIMIT = 25
164163
}
165164
}

app/src/test/kotlin/fr/free/nrw/commons/category/CategoriesModelTest.kt

+31-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package fr.free.nrw.commons.category
22

33
import categoryItem
44
import com.nhaarman.mockitokotlin2.mock
5+
import com.nhaarman.mockitokotlin2.verify
56
import com.nhaarman.mockitokotlin2.whenever
67
import depictedItem
78
import fr.free.nrw.commons.upload.GpsCategoryModel
89
import io.reactivex.Single
910
import io.reactivex.subjects.BehaviorSubject
1011
import org.junit.Before
1112
import org.junit.Test
13+
import org.mockito.ArgumentMatchers
1214
import org.mockito.Mock
1315
import org.mockito.MockitoAnnotations
1416

@@ -27,38 +29,58 @@ class CategoriesModelTest {
2729
MockitoAnnotations.initMocks(this)
2830
}
2931

30-
// Test Case for verifying that Categories search (MW api calls) are case-insensitive
32+
// Test Case for verifying that Categories search (MW api calls)
3133
@Test
3234
fun searchAllFoundCaseTest() {
3335
val categoriesModel = CategoriesModel(categoryClient, mock(), mock())
3436

3537
val expectedList = listOf("Test")
36-
whenever(categoryClient.searchCategoriesForPrefix("tes", 25))
38+
whenever(
39+
categoryClient.searchCategoriesForPrefix(
40+
ArgumentMatchers.anyString(),
41+
ArgumentMatchers.anyInt(),
42+
ArgumentMatchers.anyInt()
43+
)
44+
)
3745
.thenReturn(Single.just(expectedList))
3846

3947
// Checking if both return "Test"
4048
val expectedItems = expectedList.map { CategoryItem(it, false) }
41-
categoriesModel.searchAll("tes", emptyList(), emptyList())
49+
var categoryTerm = "Test"
50+
categoriesModel.searchAll(categoryTerm, emptyList(), emptyList())
4251
.test()
4352
.assertValues(expectedItems)
4453

45-
categoriesModel.searchAll("Tes", emptyList(), emptyList())
54+
verify(categoryClient).searchCategoriesForPrefix(
55+
categoryTerm,
56+
CategoriesModel.SEARCH_CATS_LIMIT
57+
)
58+
59+
categoriesModel.searchAll(categoryTerm, emptyList(), emptyList())
4660
.test()
4761
.assertValues(expectedItems)
4862
}
4963

64+
5065
@Test
5166
fun `searchAll with empty search terms creates results from gps, title search & recents`() {
5267
val gpsCategoryModel: GpsCategoryModel = mock()
5368
val depictedItem = depictedItem(commonsCategories = listOf("depictionCategory"))
5469

5570
whenever(gpsCategoryModel.categoriesFromLocation)
5671
.thenReturn(BehaviorSubject.createDefault(listOf("gpsCategory")))
57-
whenever(categoryClient.searchCategories("tes", 25))
72+
whenever(
73+
categoryClient.searchCategories(
74+
ArgumentMatchers.anyString(),
75+
ArgumentMatchers.anyInt(),
76+
ArgumentMatchers.anyInt()
77+
)
78+
)
5879
.thenReturn(Single.just(listOf("titleSearch")))
5980
whenever(categoryDao.recentCategories(25)).thenReturn(listOf("recentCategories"))
81+
val imageTitleList = listOf("Test")
6082
CategoriesModel(categoryClient, categoryDao, gpsCategoryModel)
61-
.searchAll("", listOf("tes"), listOf(depictedItem))
83+
.searchAll("", imageTitleList, listOf(depictedItem))
6284
.test()
6385
.assertValue(
6486
listOf(
@@ -68,5 +90,8 @@ class CategoriesModelTest {
6890
categoryItem("recentCategories")
6991
)
7092
)
93+
imageTitleList.forEach {
94+
verify(categoryClient).searchCategories(it, CategoriesModel.SEARCH_CATS_LIMIT)
95+
}
7196
}
7297
}

0 commit comments

Comments
 (0)