@@ -8,6 +8,9 @@ import org.mockito.*
88import org.wikipedia.dataclient.mwapi.MwQueryPage
99import org.wikipedia.dataclient.mwapi.MwQueryResponse
1010import org.wikipedia.dataclient.mwapi.MwQueryResult
11+ import android.content.Context
12+ import com.google.gson.Gson
13+ import fr.free.nrw.commons.kvstore.JsonKvStore
1114
1215// class for testing CategoriesModel class
1316class CategoriesModelTest {
@@ -17,12 +20,26 @@ class CategoriesModelTest {
1720 @Mock
1821 internal var categoryItem: CategoryItem ? = null
1922
23+ @Spy
24+ internal lateinit var gson: Gson
25+
26+ @Spy
27+ internal lateinit var categoryItemForSubstringSearch: CategoryItem
28+
29+ @Mock
30+ internal var categoryDao: CategoryDao ? = null
31+
32+ @Mock
33+ internal var context: Context ? = null
34+
2035 @InjectMocks
2136 var categoryClient: CategoryClient ? = null
2237
2338 @Before
2439 @Throws(Exception ::class )
2540 fun setUp () {
41+ gson = Gson ()
42+ categoryItemForSubstringSearch = CategoryItem (" " ,false )
2643 MockitoAnnotations .initMocks(this )
2744 }
2845
@@ -47,4 +64,42 @@ class CategoriesModelTest {
4764 val actualCategoryNameCaps = categoriesModel!! .searchAll(" Tes" ,null ).blockingFirst()
4865 assertEquals(" Test" , actualCategoryNameCaps.getName())
4966 }
67+
68+ /* *
69+ * For testing the substring search algorithm for Categories search
70+ * To be more precise it tests the In Between substring( ex: searching `atte`
71+ * will give search suggestions: `Latte`, `Iced latte` e.t.c) which has been described
72+ * on github repo wiki:
73+ * https://github.com/commons-app/apps-android-commons/wiki/Category-suggestions-(readme)#user-content-3-category-search-when-typing-in-the-search-field-has-been-made-more-flexible
74+ */
75+ @Test
76+ fun searchAllFoundCaseTestForSubstringSearch () {
77+ val mockDaoList = mutableListOf<String >(" " )
78+ val mwQueryPage = Mockito .mock(MwQueryPage ::class .java)
79+ Mockito .`when `(mwQueryPage.title()).thenReturn(" Category:Test" )
80+ val mwQueryResult = Mockito .mock(MwQueryResult ::class .java)
81+ Mockito .`when `(mwQueryResult.pages()).thenReturn(listOf (mwQueryPage))
82+ val mockResponse = Mockito .mock(MwQueryResponse ::class .java)
83+ Mockito .`when `(mockResponse.query()).thenReturn(mwQueryResult)
84+ Mockito .`when `(context!! .getSharedPreferences(" " ,0 ))
85+ .thenReturn(null )
86+ val directKvStore = Mockito .spy(JsonKvStore (context," " ,gson))
87+ val categoriesModelForSubstringSearch = Mockito .spy(CategoriesModel (categoryClient,categoryDao,directKvStore))
88+ Mockito .doReturn(Observable .just(categoryItemForSubstringSearch)).`when `(categoriesModelForSubstringSearch).gpsCategories()
89+ Mockito .`when `(context!! .getSharedPreferences(" " ,0 ))
90+ .thenReturn(null )
91+ Mockito .`when `(categoryInterface!! .searchCategories(ArgumentMatchers .anyString(), ArgumentMatchers .anyInt(), ArgumentMatchers .anyInt()))
92+ .thenReturn(Observable .just(mockResponse))
93+ Mockito .doReturn(mockDaoList).`when `(categoryDao)?.recentCategories(25 )
94+ Mockito .doReturn(" Random Value" ).`when `(directKvStore).getString(" Category" ," " )
95+ Mockito .`when `(categoryInterface!! .searchCategories(ArgumentMatchers .anyString(), ArgumentMatchers .anyInt(), ArgumentMatchers .anyInt()))
96+ .thenReturn(Observable .just(mockResponse))
97+
98+ // Checking if both return "Test"
99+ val actualCategoryName = categoriesModelForSubstringSearch!! .searchAll(null , listOf<String >(" tes" )).blockingLast()
100+ assertEquals(" Test" ,actualCategoryName.getName())
101+
102+ val actualCategoryNameCaps = categoriesModelForSubstringSearch!! .searchAll(null , listOf<String >(" Tes" )).blockingLast()
103+ assertEquals(" Test" ,actualCategoryNameCaps.getName())
104+ }
50105}
0 commit comments