mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 18:56:44 -04:00
Improved search functionality
* Fixed search unit test cases. * Improved the search functionality to perform the unit test case for search.
This commit is contained in:
parent
731b0a5843
commit
ca285abab6
@ -32,7 +32,7 @@ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
|
||||
import org.kiwix.kiwixmobile.core.main.UNINITIALISER_ADDRESS
|
||||
import org.kiwix.kiwixmobile.core.main.UNINITIALISE_HTML
|
||||
import org.kiwix.kiwixmobile.core.reader.ZimFileReader.Companion.CONTENT_PREFIX
|
||||
import org.kiwix.kiwixmobile.core.search.SearchSuggestion
|
||||
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.ZimSearchResultListItem
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
|
||||
import org.kiwix.libkiwix.JNIKiwixException
|
||||
import org.kiwix.libzim.Archive
|
||||
@ -42,7 +42,6 @@ import org.kiwix.libzim.Item
|
||||
import org.kiwix.libzim.Query
|
||||
import org.kiwix.libzim.Search
|
||||
import org.kiwix.libzim.Searcher
|
||||
import org.kiwix.libzim.SuggestionSearch
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.IOException
|
||||
@ -144,14 +143,14 @@ class ZimFileReader constructor(
|
||||
null
|
||||
}
|
||||
|
||||
fun getNextSuggestion(suggestionSearch: SuggestionSearch?): List<SearchSuggestion> {
|
||||
val suggestionList = mutableListOf<SearchSuggestion>()
|
||||
fun getSearchResultList(search: Search?): List<ZimSearchResultListItem> {
|
||||
val suggestionList = mutableListOf<ZimSearchResultListItem>()
|
||||
val suggestionIterator =
|
||||
suggestionSearch?.getResults(0, suggestionSearch.estimatedMatches.toInt())
|
||||
if (suggestionIterator != null) {
|
||||
while (suggestionIterator.hasNext()) {
|
||||
val suggestionItem = suggestionIterator.next()
|
||||
suggestionList.add(SearchSuggestion(suggestionItem.title, suggestionItem.path))
|
||||
search?.getResults(0, search.estimatedMatches.toInt())
|
||||
suggestionIterator?.let {
|
||||
while (it.hasNext()) {
|
||||
val entry = it.next()
|
||||
suggestionList.add(ZimSearchResultListItem(entry.title))
|
||||
}
|
||||
}
|
||||
return suggestionList
|
||||
|
@ -23,7 +23,6 @@ import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.yield
|
||||
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
|
||||
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
|
||||
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.ZimSearchResultListItem
|
||||
import javax.inject.Inject
|
||||
|
||||
interface SearchResultGenerator {
|
||||
@ -49,15 +48,6 @@ class ZimSearchResultGenerator @Inject constructor() : SearchResultGenerator {
|
||||
?.searchSuggestions(searchTerm)
|
||||
.also { yield() }
|
||||
.run {
|
||||
val suggestionList = mutableListOf<ZimSearchResultListItem>()
|
||||
val suggestionIterator =
|
||||
this?.getResults(0, this.estimatedMatches.toInt())
|
||||
suggestionIterator?.let {
|
||||
while (it.hasNext()) {
|
||||
val entry = it.next()
|
||||
suggestionList.add(ZimSearchResultListItem(entry.title))
|
||||
}
|
||||
}
|
||||
return@run suggestionList
|
||||
reader?.getSearchResultList(this) ?: emptyList()
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,8 @@ import kotlinx.coroutines.runBlocking
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
|
||||
import org.kiwix.kiwixmobile.core.search.SearchSuggestion
|
||||
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.ZimSearchResultListItem
|
||||
import org.kiwix.libzim.Search
|
||||
|
||||
internal class ZimSearchResultGeneratorTest {
|
||||
|
||||
@ -47,10 +47,10 @@ internal class ZimSearchResultGeneratorTest {
|
||||
internal fun `suggestion results are distinct`() {
|
||||
val validTitle = "title"
|
||||
val searchTerm = " "
|
||||
val item = mockk<SearchSuggestion>()
|
||||
every { zimFileReader.searchSuggestions(" ") } returns true
|
||||
every { zimFileReader.getNextSuggestion(suggestionSearch) } returnsMany listOf(item, item, null)
|
||||
every { item.title } returns validTitle
|
||||
val item = ZimSearchResultListItem(validTitle)
|
||||
val search: Search = mockk()
|
||||
every { zimFileReader.searchSuggestions(searchTerm) } returns search
|
||||
every { zimFileReader.getSearchResultList(search) } returns listOf(item)
|
||||
runBlocking {
|
||||
assertThat(zimSearchResultGenerator.generateSearchResults(searchTerm, zimFileReader))
|
||||
.isEqualTo(listOf(ZimSearchResultListItem(validTitle)))
|
||||
|
Loading…
x
Reference in New Issue
Block a user