Added new migration test in database and deleted old objectbox database test code

This commit is contained in:
Gouri Panda 2022-12-11 00:42:16 +05:30 committed by Kelson
parent a48c185d2c
commit 4dc5f189d5
4 changed files with 296 additions and 236 deletions

View File

@ -0,0 +1,72 @@
/*
* Kiwix Android
* Copyright (c) 2022 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.data.local
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.mockk
import io.objectbox.Box
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.*
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchRoomDao
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity
import java.io.IOException
@RunWith(AndroidJUnit4::class)
class KiwixRoomDatabaseTest {
private val box: Box<RecentSearchEntity> = mockk(relaxed = true)
private lateinit var newRecentSearchRoomDao: NewRecentSearchRoomDao
private lateinit var db: KiwixRoomDatabase
// @Before
// fun createDb() {
// }
@Test
@Throws(IOException::class)
fun testMigrationTest() = runBlocking {
val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(
context, KiwixRoomDatabase::class.java
).build()
newRecentSearchRoomDao = db.newRecentSearchRoomDao()
val searchTerm = "title"
val zimId = "zimId"
box.put(RecentSearchEntity(searchTerm = searchTerm, zimId = zimId))
newRecentSearchRoomDao.migrationToRoomInsert(box)
newRecentSearchRoomDao.search("zimId").collect { recentSearchEntites ->
val entity = recentSearchEntites.find { it.zimId == zimId }
if (entity != null) {
Assertions.assertEquals(searchTerm, entity.searchTerm)
}
}
}
@After
@Throws(IOException::class)
fun closeDb() {
db.close()
}
}

View File

@ -1,151 +1,139 @@
/*
* 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.verify
import io.objectbox.Box
import io.objectbox.query.Query
import io.objectbox.query.QueryBuilder
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runBlockingTest
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.kiwixmobile.core.search.viewmodel.test
import org.kiwix.sharedFunctions.recentSearchEntity
internal class NewRecentSearchDaoTest {
private val box: Box<RecentSearchEntity> = mockk(relaxed = true)
private val flowBuilder: FlowBuilder = mockk()
private val newRecentSearchDao = NewRecentSearchDao(box, flowBuilder)
@Nested
inner class RecentSearchTests {
@Test
fun `recentSearches searches by Id passed`() = runBlockingTest {
val zimId = "id"
val queryResult = listOf<RecentSearchEntity>(recentSearchEntity())
expectFromRecentSearches(queryResult, zimId)
newRecentSearchDao.recentSearches(zimId)
.test(this)
.assertValues(
queryResult.map { RecentSearchListItem(it.searchTerm) }
)
.finish()
}
@Test
fun `recentSearches searches with blank Id if null passed`() = runBlockingTest {
val queryResult = listOf<RecentSearchEntity>(recentSearchEntity())
expectFromRecentSearches(queryResult, "")
newRecentSearchDao.recentSearches(null)
.test(this)
.assertValues(
queryResult.map { RecentSearchListItem(it.searchTerm) }
)
.finish()
}
@Test
fun `recentSearches searches returns distinct entities by searchTerm`() = runBlockingTest {
val queryResult = listOf<RecentSearchEntity>(recentSearchEntity(), recentSearchEntity())
expectFromRecentSearches(queryResult, "")
newRecentSearchDao.recentSearches("")
.test(this)
.assertValues(
queryResult.take(1).map { RecentSearchListItem(it.searchTerm) }
)
.finish()
}
@Test
fun `recentSearches searches returns a limitedNumber of entities`() = runBlockingTest {
val searchResults: List<RecentSearchEntity> =
(0..200).map { recentSearchEntity(searchTerm = "$it") }
expectFromRecentSearches(searchResults, "")
newRecentSearchDao.recentSearches("")
.test(this)
.assertValue { it.size == 100 }
.finish()
}
private fun expectFromRecentSearches(queryResult: List<RecentSearchEntity>, zimId: String) {
val queryBuilder = mockk<QueryBuilder<RecentSearchEntity>>()
every { box.query() } returns queryBuilder
every {
queryBuilder.equal(
RecentSearchEntity_.zimId,
zimId,
QueryBuilder.StringOrder.CASE_INSENSITIVE
)
} returns queryBuilder
every { queryBuilder.orderDesc(RecentSearchEntity_.id) } returns queryBuilder
val query = mockk<Query<RecentSearchEntity>>()
every { queryBuilder.build() } returns query
every { flowBuilder.buildCallbackFlow(query) } returns flowOf(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,
QueryBuilder.StringOrder.CASE_INSENSITIVE
)
} 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))) }
}
}
// * 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.verify
// import io.objectbox.Box
// import io.objectbox.query.Query
// import io.objectbox.query.QueryBuilder
// import kotlinx.coroutines.flow.flowOf
// import kotlinx.coroutines.test.runBlockingTest
// 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.kiwixmobile.core.search.viewmodel.test
// import org.kiwix.sharedFunctions.recentSearchEntity
//
// internal class NewRecentSearchDaoTest {
//
// private val box: Box<RecentSearchEntity> = mockk(relaxed = true)
// private val flowBuilder: FlowBuilder = mockk()
// private val newRecentSearchDao = NewRecentSearchDao(box, flowBuilder)
//
// @Nested
// inner class RecentSearchTests {
// @Test
// fun `recentSearches searches by Id passed`() = runBlockingTest {
// val zimId = "id"
// val queryResult = listOf<RecentSearchEntity>(recentSearchEntity())
// expectFromRecentSearches(queryResult, zimId)
// newRecentSearchDao.recentSearches(zimId)
// .test(this)
// .assertValues(
// queryResult.map { RecentSearchListItem(it.searchTerm) }
// )
// .finish()
// }
//
// @Test
// fun `recentSearches searches with blank Id if null passed`() = runBlockingTest {
// val queryResult = listOf<RecentSearchEntity>(recentSearchEntity())
// expectFromRecentSearches(queryResult, "")
// newRecentSearchDao.recentSearches(null)
// .test(this)
// .assertValues(
// queryResult.map { RecentSearchListItem(it.searchTerm) }
// )
// .finish()
// }
//
// @Test
// fun `recentSearches searches returns distinct entities by searchTerm`() = runBlockingTest {
// val queryResult = listOf<RecentSearchEntity>(recentSearchEntity(), recentSearchEntity())
// expectFromRecentSearches(queryResult, "")
// newRecentSearchDao.recentSearches("")
// .test(this)
// .assertValues(
// queryResult.take(1).map { RecentSearchListItem(it.searchTerm) }
// )
// .finish()
// }
//
// @Test
// fun `recentSearches searches returns a limitedNumber of entities`() = runBlockingTest {
// val searchResults: List<RecentSearchEntity> =
// (0..200).map { recentSearchEntity(searchTerm = "$it") }
// expectFromRecentSearches(searchResults, "")
// newRecentSearchDao.recentSearches("")
// .test(this)
// .assertValue { it.size == 100 }
// .finish()
// }
//
// 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
// every { flowBuilder.buildCallbackFlow(query) } returns flowOf(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))) }
// }
// }

View File

@ -1,39 +1,39 @@
/*
* 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.search.viewmodel.effects
import androidx.appcompat.app.AppCompatActivity
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
internal class DeleteRecentSearchTest {
@Test
fun `invoke with deletes a search`() {
val searchListItem: SearchListItem = RecentSearchListItem("")
val recentSearchDao: NewRecentSearchDao = mockk()
val activity: AppCompatActivity = mockk()
DeleteRecentSearch(searchListItem, recentSearchDao).invokeWith(activity)
verify { recentSearchDao.deleteSearchString(searchListItem.value) }
}
}
package org.kiwix.kiwixmobile.core.search.viewmodel.effects// /*
// * 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.search.viewmodel.effects
//
// import androidx.appcompat.app.AppCompatActivity
// import io.mockk.mockk
// import io.mockk.verify
// import org.junit.jupiter.api.Test
// import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
// import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
// import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
//
// internal class DeleteRecentSearchTest {
//
// @Test
// fun `invoke with deletes a search`() {
// val searchListItem: SearchListItem = RecentSearchListItem("")
// val recentSearchDao: NewRecentSearchDao = mockk()
// val activity: AppCompatActivity = mockk()
// DeleteRecentSearch(searchListItem, recentSearchDao).invokeWith(activity)
// verify { recentSearchDao.deleteSearchString(searchListItem.value) }
// }
// }

View File

@ -1,48 +1,48 @@
/*
* 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.search.viewmodel.effects
import androidx.appcompat.app.AppCompatActivity
import io.mockk.Called
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
internal class SaveSearchToRecentsTest {
private val newRecentSearchDao: NewRecentSearchDao = mockk()
private val searchListItem = RecentSearchListItem("")
private val activity: AppCompatActivity = mockk()
@Test
fun `invoke with null Id does nothing`() {
SaveSearchToRecents(newRecentSearchDao, searchListItem, null).invokeWith(activity)
verify { newRecentSearchDao wasNot Called }
}
@Test
fun `invoke with non null Id saves search`() {
val id = "id"
SaveSearchToRecents(newRecentSearchDao, searchListItem, id).invokeWith(activity)
verify { newRecentSearchDao.saveSearch(searchListItem.value, id) }
}
}
// * 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.search.viewmodel.effects
//
// import androidx.appcompat.app.AppCompatActivity
// import io.mockk.Called
// import io.mockk.mockk
// import io.mockk.verify
// import org.junit.jupiter.api.Test
// import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
// import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
//
// internal class SaveSearchToRecentsTest {
//
// private val newRecentSearchDao: NewRecentSearchDao = mockk()
// private val searchListItem = RecentSearchListItem("")
//
// private val activity: AppCompatActivity = mockk()
//
// @Test
// fun `invoke with null Id does nothing`() {
// SaveSearchToRecents(newRecentSearchDao, searchListItem, null).invokeWith(activity)
// verify { newRecentSearchDao wasNot Called }
// }
//
// @Test
// fun `invoke with non null Id saves search`() {
// val id = "id"
// SaveSearchToRecents(newRecentSearchDao, searchListItem, id).invokeWith(activity)
// verify { newRecentSearchDao.saveSearch(searchListItem.value, id) }
// }
// }