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