From 4dc5f189d5fd97ff057c9004c1cac03541dfcee1 Mon Sep 17 00:00:00 2001 From: Gouri Panda Date: Sun, 11 Dec 2022 00:42:16 +0530 Subject: [PATCH] Added new migration test in database and deleted old objectbox database test code --- .../core/data/local/KiwixRoomDatabaseTest.kt | 72 +++++ .../core/dao/NewRecentSearchDaoTest.kt | 288 +++++++++--------- .../effects/DeleteRecentSearchTest.kt | 78 ++--- .../effects/SaveSearchToRecentsTest.kt | 94 +++--- 4 files changed, 296 insertions(+), 236 deletions(-) create mode 100644 app/src/androidTest/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabaseTest.kt diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabaseTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabaseTest.kt new file mode 100644 index 000000000..5b62ebed8 --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/core/data/local/KiwixRoomDatabaseTest.kt @@ -0,0 +1,72 @@ +/* + * Kiwix Android + * Copyright (c) 2022 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.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 = 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() + 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() + } +} 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 index c424f6dcc..5564bc872 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDaoTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewRecentSearchDaoTest.kt @@ -1,151 +1,139 @@ -/* - * 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.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 = 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()) - 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()) - 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()) - 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 = - (0..200).map { recentSearchEntity(searchTerm = "$it") } - expectFromRecentSearches(searchResults, "") - newRecentSearchDao.recentSearches("") - .test(this) - .assertValue { it.size == 100 } - .finish() - } - - private fun expectFromRecentSearches(queryResult: List, zimId: String) { - val queryBuilder = mockk>() - 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>() - 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 = mockk() - every { box.query() } returns queryBuilder - every { - queryBuilder.equal( - RecentSearchEntity_.searchTerm, - searchTerm, - QueryBuilder.StringOrder.CASE_INSENSITIVE - ) - } 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))) } - } -} +// * 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.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 = 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()) +// 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()) +// 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()) +// 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 = +// (0..200).map { recentSearchEntity(searchTerm = "$it") } +// expectFromRecentSearches(searchResults, "") +// newRecentSearchDao.recentSearches("") +// .test(this) +// .assertValue { it.size == 100 } +// .finish() +// } +// +// 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 +// 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 = 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))) } +// } +// } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/DeleteRecentSearchTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/DeleteRecentSearchTest.kt index f2e9dafa1..761e174d3 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/DeleteRecentSearchTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/DeleteRecentSearchTest.kt @@ -1,39 +1,39 @@ -/* - * 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.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 +// * 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.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) } +// } +// } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/SaveSearchToRecentsTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/SaveSearchToRecentsTest.kt index a6e8d62a8..e936134ce 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/SaveSearchToRecentsTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/SaveSearchToRecentsTest.kt @@ -1,48 +1,48 @@ -/* - * 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.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 +// * 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.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) } +// } +// }