Merge pull request #2001 from kiwix/macgills/feature/1865-test-recent-search-dao

#1865 Add unit tests for NewRecentSearchDao
This commit is contained in:
Seán Mac Gillicuddy 2020-04-06 15:47:25 +01:00 committed by GitHub
commit 77df01da65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 131 additions and 4 deletions

View File

@ -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<RecentSearchEntity>
) {
class NewRecentSearchDao @Inject constructor(private val box: Box<RecentSearchEntity>) {
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
}
}

View File

@ -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<Book> = emptyList()) = LibraryNetworkEntity().apply {
book = LinkedList(books)
}
fun recentSearchEntity(id: Long = 0L, searchTerm: String = "", zimId: String = "") =
RecentSearchEntity(id, searchTerm, zimId)

View File

@ -0,0 +1,125 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
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<RecentSearchEntity> = 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>(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>(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(), 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<RecentSearchEntity> =
(0..200).map { recentSearchEntity(searchTerm = "$it") }
expectFromRecentSearches(searchResults, "")
newRecentSearchDao.recentSearches("").test()
.assertValue { it.size == 100 }
}
private fun expectFromRecentSearches(queryResult: List<RecentSearchEntity>, zimId: String) {
val queryBuilder = mockk<QueryBuilder<RecentSearchEntity>>()
every { box.query() } returns queryBuilder
every { queryBuilder.equal(RecentSearchEntity_.zimId, zimId) } returns queryBuilder
every { queryBuilder.orderDesc(RecentSearchEntity_.id) } returns queryBuilder
val query = mockk<Query<RecentSearchEntity>>()
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<RecentSearchEntity> = mockk()
every { box.query() } returns queryBuilder
every { queryBuilder.equal(RecentSearchEntity_.searchTerm, searchTerm) } returns queryBuilder
val query: Query<RecentSearchEntity> = 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))) }
}
}