diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt index 5563c186f..85ba019df 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDao.kt @@ -25,9 +25,7 @@ import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem import javax.inject.Inject -class NewRecentSearchDao @Inject constructor( - val box: Box -) { +class NewRecentSearchDao @Inject constructor(private val box: Box) { fun recentSearches(zimId: String?) = box.asFlowable( box.query { equal(RecentSearchEntity_.zimId, zimId ?: "") @@ -60,6 +58,6 @@ class NewRecentSearchDao @Inject constructor( } companion object { - const val NUM_RECENT_RESULTS = 100 + private const val NUM_RECENT_RESULTS = 100 } } diff --git a/core/src/sharedTestFunctions/java/org/kiwix/sharedFunctions/TestModelFunctions.kt b/core/src/sharedTestFunctions/java/org/kiwix/sharedFunctions/TestModelFunctions.kt index 2831b5191..94691d5fe 100644 --- a/core/src/sharedTestFunctions/java/org/kiwix/sharedFunctions/TestModelFunctions.kt +++ b/core/src/sharedTestFunctions/java/org/kiwix/sharedFunctions/TestModelFunctions.kt @@ -27,6 +27,7 @@ import org.kiwix.kiwixmobile.core.downloader.model.DownloadState import org.kiwix.kiwixmobile.core.downloader.model.DownloadState.Pending import org.kiwix.kiwixmobile.core.downloader.model.Seconds import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity +import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.core.entity.MetaLinkNetworkEntity import org.kiwix.kiwixmobile.core.entity.MetaLinkNetworkEntity.FileElement @@ -160,3 +161,6 @@ fun book( fun libraryNetworkEntity(books: List = emptyList()) = LibraryNetworkEntity().apply { book = LinkedList(books) } + +fun recentSearchEntity(id: Long = 0L, searchTerm: String = "", zimId: String = "") = + RecentSearchEntity(id, searchTerm, zimId) diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDaoTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDaoTest.kt new file mode 100644 index 000000000..b66e21c64 --- /dev/null +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDaoTest.kt @@ -0,0 +1,125 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.dao + +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.verify +import io.objectbox.Box +import io.objectbox.query.Query +import io.objectbox.query.QueryBuilder +import io.objectbox.rx.RxQuery +import io.reactivex.Observable +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity +import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity_ +import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch +import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem +import org.kiwix.sharedFunctions.recentSearchEntity + +internal class NewRecentSearchDaoTest { + + private val box: Box = mockk(relaxed = true) + private val newRecentSearchDao = NewRecentSearchDao(box) + + @Nested + inner class RecentSearchTests { + @Test + fun `recentSearches searches by Id passed`() { + val zimId = "id" + val queryResult = listOf(recentSearchEntity()) + expectFromRecentSearches(queryResult, zimId) + newRecentSearchDao.recentSearches(zimId).test() + .assertValues(queryResult.map { RecentSearchListItem(it.searchTerm) }) + } + + @Test + fun `recentSearches searches with blank Id if null passed`() { + val queryResult = listOf(recentSearchEntity()) + expectFromRecentSearches(queryResult, "") + newRecentSearchDao.recentSearches(null).test() + .assertValues(queryResult.map { RecentSearchListItem(it.searchTerm) }) + } + + @Test + fun `recentSearches searches returns distinct entities by searchTerm`() { + val queryResult = listOf(recentSearchEntity(), recentSearchEntity()) + expectFromRecentSearches(queryResult, "") + newRecentSearchDao.recentSearches("").test() + .assertValues(queryResult.take(1).map { RecentSearchListItem(it.searchTerm) }) + } + + @Test + fun `recentSearches searches returns a limitedNumber of entities`() { + val searchResults: List = + (0..200).map { recentSearchEntity(searchTerm = "$it") } + expectFromRecentSearches(searchResults, "") + newRecentSearchDao.recentSearches("").test() + .assertValue { it.size == 100 } + } + + private fun expectFromRecentSearches(queryResult: List, zimId: String) { + val queryBuilder = mockk>() + every { box.query() } returns queryBuilder + every { queryBuilder.equal(RecentSearchEntity_.zimId, zimId) } returns queryBuilder + every { queryBuilder.orderDesc(RecentSearchEntity_.id) } returns queryBuilder + val query = mockk>() + every { queryBuilder.build() } returns query + mockkStatic(RxQuery::class) + every { RxQuery.observable(query) } returns Observable.just(queryResult) + } + } + + @Test + fun `saveSearch puts RecentSearchEntity into box`() { + newRecentSearchDao.saveSearch("title", "id") + verify { box.put(recentSearchEntity(searchTerm = "title", zimId = "id")) } + } + + @Test + fun `deleteSearchString removes query results for the term`() { + val searchTerm = "searchTerm" + val queryBuilder: QueryBuilder = mockk() + every { box.query() } returns queryBuilder + every { queryBuilder.equal(RecentSearchEntity_.searchTerm, searchTerm) } returns queryBuilder + val query: Query = mockk(relaxed = true) + every { queryBuilder.build() } returns query + newRecentSearchDao.deleteSearchString(searchTerm) + verify { query.remove() } + } + + @Test + fun `deleteSearchHistory deletes everything`() { + newRecentSearchDao.deleteSearchHistory() + verify { box.removeAll() } + } + + @Test + fun `migrationInsert adds old items to box`() { + val id = "zimId" + val term = "searchString" + val recentSearch: RecentSearch = mockk() + every { recentSearch.searchString } returns term + every { recentSearch.zimID } returns id + newRecentSearchDao.migrationInsert(mutableListOf(recentSearch)) + verify { box.put(listOf(recentSearchEntity(searchTerm = term, zimId = id))) } + } +}