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 package org.kiwix.kiwixmobile.core.dao
// * Kiwix Android
import io.mockk.every // * Copyright (c) 2020 Kiwix <android.kiwix.org>
import io.mockk.mockk // * This program is free software: you can redistribute it and/or modify
import io.mockk.verify // * it under the terms of the GNU General Public License as published by
import io.objectbox.Box // * the Free Software Foundation, either version 3 of the License, or
import io.objectbox.query.Query // * (at your option) any later version.
import io.objectbox.query.QueryBuilder // *
import kotlinx.coroutines.flow.flowOf // * This program is distributed in the hope that it will be useful,
import kotlinx.coroutines.test.runBlockingTest // * but WITHOUT ANY WARRANTY; without even the implied warranty of
import org.junit.jupiter.api.Nested // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
import org.junit.jupiter.api.Test // * GNU General Public License for more details.
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity // *
import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity_ // * You should have received a copy of the GNU General Public License
import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch // * along with this program. If not, see <http://www.gnu.org/licenses/>.
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem // *
import org.kiwix.kiwixmobile.core.search.viewmodel.test // */
import org.kiwix.sharedFunctions.recentSearchEntity //
// package org.kiwix.kiwixmobile.core.dao
internal class NewRecentSearchDaoTest { //
// import io.mockk.every
private val box: Box<RecentSearchEntity> = mockk(relaxed = true) // import io.mockk.mockk
private val flowBuilder: FlowBuilder = mockk() // import io.mockk.verify
private val newRecentSearchDao = NewRecentSearchDao(box, flowBuilder) // import io.objectbox.Box
// import io.objectbox.query.Query
@Nested // import io.objectbox.query.QueryBuilder
inner class RecentSearchTests { // import kotlinx.coroutines.flow.flowOf
@Test // import kotlinx.coroutines.test.runBlockingTest
fun `recentSearches searches by Id passed`() = runBlockingTest { // import org.junit.jupiter.api.Nested
val zimId = "id" // import org.junit.jupiter.api.Test
val queryResult = listOf<RecentSearchEntity>(recentSearchEntity()) // import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity
expectFromRecentSearches(queryResult, zimId) // import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity_
newRecentSearchDao.recentSearches(zimId) // import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch
.test(this) // import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
.assertValues( // import org.kiwix.kiwixmobile.core.search.viewmodel.test
queryResult.map { RecentSearchListItem(it.searchTerm) } // import org.kiwix.sharedFunctions.recentSearchEntity
) //
.finish() // internal class NewRecentSearchDaoTest {
} //
// private val box: Box<RecentSearchEntity> = mockk(relaxed = true)
@Test // private val flowBuilder: FlowBuilder = mockk()
fun `recentSearches searches with blank Id if null passed`() = runBlockingTest { // private val newRecentSearchDao = NewRecentSearchDao(box, flowBuilder)
val queryResult = listOf<RecentSearchEntity>(recentSearchEntity()) //
expectFromRecentSearches(queryResult, "") // @Nested
newRecentSearchDao.recentSearches(null) // inner class RecentSearchTests {
.test(this) // @Test
.assertValues( // fun `recentSearches searches by Id passed`() = runBlockingTest {
queryResult.map { RecentSearchListItem(it.searchTerm) } // val zimId = "id"
) // val queryResult = listOf<RecentSearchEntity>(recentSearchEntity())
.finish() // expectFromRecentSearches(queryResult, zimId)
} // newRecentSearchDao.recentSearches(zimId)
// .test(this)
@Test // .assertValues(
fun `recentSearches searches returns distinct entities by searchTerm`() = runBlockingTest { // queryResult.map { RecentSearchListItem(it.searchTerm) }
val queryResult = listOf<RecentSearchEntity>(recentSearchEntity(), recentSearchEntity()) // )
expectFromRecentSearches(queryResult, "") // .finish()
newRecentSearchDao.recentSearches("") // }
.test(this) //
.assertValues( // @Test
queryResult.take(1).map { RecentSearchListItem(it.searchTerm) } // fun `recentSearches searches with blank Id if null passed`() = runBlockingTest {
) // val queryResult = listOf<RecentSearchEntity>(recentSearchEntity())
.finish() // expectFromRecentSearches(queryResult, "")
} // newRecentSearchDao.recentSearches(null)
// .test(this)
@Test // .assertValues(
fun `recentSearches searches returns a limitedNumber of entities`() = runBlockingTest { // queryResult.map { RecentSearchListItem(it.searchTerm) }
val searchResults: List<RecentSearchEntity> = // )
(0..200).map { recentSearchEntity(searchTerm = "$it") } // .finish()
expectFromRecentSearches(searchResults, "") // }
newRecentSearchDao.recentSearches("") //
.test(this) // @Test
.assertValue { it.size == 100 } // fun `recentSearches searches returns distinct entities by searchTerm`() = runBlockingTest {
.finish() // val queryResult = listOf<RecentSearchEntity>(recentSearchEntity(), recentSearchEntity())
} // expectFromRecentSearches(queryResult, "")
// newRecentSearchDao.recentSearches("")
private fun expectFromRecentSearches(queryResult: List<RecentSearchEntity>, zimId: String) { // .test(this)
val queryBuilder = mockk<QueryBuilder<RecentSearchEntity>>() // .assertValues(
every { box.query() } returns queryBuilder // queryResult.take(1).map { RecentSearchListItem(it.searchTerm) }
every { // )
queryBuilder.equal( // .finish()
RecentSearchEntity_.zimId, // }
zimId, //
QueryBuilder.StringOrder.CASE_INSENSITIVE // @Test
) // fun `recentSearches searches returns a limitedNumber of entities`() = runBlockingTest {
} returns queryBuilder // val searchResults: List<RecentSearchEntity> =
every { queryBuilder.orderDesc(RecentSearchEntity_.id) } returns queryBuilder // (0..200).map { recentSearchEntity(searchTerm = "$it") }
val query = mockk<Query<RecentSearchEntity>>() // expectFromRecentSearches(searchResults, "")
every { queryBuilder.build() } returns query // newRecentSearchDao.recentSearches("")
every { flowBuilder.buildCallbackFlow(query) } returns flowOf(queryResult) // .test(this)
} // .assertValue { it.size == 100 }
} // .finish()
// }
@Test //
fun `saveSearch puts RecentSearchEntity into box`() { // private fun expectFromRecentSearches(queryResult: List<RecentSearchEntity>, zimId: String) {
newRecentSearchDao.saveSearch("title", "id") // val queryBuilder = mockk<QueryBuilder<RecentSearchEntity>>()
verify { box.put(recentSearchEntity(searchTerm = "title", zimId = "id")) } // every { box.query() } returns queryBuilder
} // every { queryBuilder.equal(RecentSearchEntity_.zimId, zimId) } returns queryBuilder
// every { queryBuilder.orderDesc(RecentSearchEntity_.id) } returns queryBuilder
@Test // val query = mockk<Query<RecentSearchEntity>>()
fun `deleteSearchString removes query results for the term`() { // every { queryBuilder.build() } returns query
val searchTerm = "searchTerm" // every { flowBuilder.buildCallbackFlow(query) } returns flowOf(queryResult)
val queryBuilder: QueryBuilder<RecentSearchEntity> = mockk() // }
every { box.query() } returns queryBuilder // }
every { //
queryBuilder.equal( // @Test
RecentSearchEntity_.searchTerm, // fun `saveSearch puts RecentSearchEntity into box`() {
searchTerm, // newRecentSearchDao.saveSearch("title", "id")
QueryBuilder.StringOrder.CASE_INSENSITIVE // verify { box.put(recentSearchEntity(searchTerm = "title", zimId = "id")) }
) // }
} returns queryBuilder //
val query: Query<RecentSearchEntity> = mockk(relaxed = true) // @Test
every { queryBuilder.build() } returns query // fun `deleteSearchString removes query results for the term`() {
newRecentSearchDao.deleteSearchString(searchTerm) // val searchTerm = "searchTerm"
verify { query.remove() } // val queryBuilder: QueryBuilder<RecentSearchEntity> = mockk()
} // every { box.query() } returns queryBuilder
// every { queryBuilder.equal(RecentSearchEntity_.searchTerm, searchTerm) } returns queryBuilder
@Test // val query: Query<RecentSearchEntity> = mockk(relaxed = true)
fun `deleteSearchHistory deletes everything`() { // every { queryBuilder.build() } returns query
newRecentSearchDao.deleteSearchHistory() // newRecentSearchDao.deleteSearchString(searchTerm)
verify { box.removeAll() } // verify { query.remove() }
} // }
//
@Test // @Test
fun `migrationInsert adds old items to box`() { // fun `deleteSearchHistory deletes everything`() {
val id = "zimId" // newRecentSearchDao.deleteSearchHistory()
val term = "searchString" // verify { box.removeAll() }
val recentSearch: RecentSearch = mockk() // }
every { recentSearch.searchString } returns term //
every { recentSearch.zimID } returns id // @Test
newRecentSearchDao.migrationInsert(mutableListOf(recentSearch)) // fun `migrationInsert adds old items to box`() {
verify { box.put(listOf(recentSearchEntity(searchTerm = term, zimId = id))) } // 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 @@
/* package org.kiwix.kiwixmobile.core.search.viewmodel.effects// /*
* Kiwix Android // * Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org> // * Copyright (c) 2020 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify // * 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 // * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or // * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. // * (at your option) any later version.
* // *
* This program is distributed in the hope that it will be useful, // * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of // * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. // * GNU General Public License for more details.
* // *
* You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. // * along with this program. If not, see <http://www.gnu.org/licenses/>.
* // *
*/ // */
//
package org.kiwix.kiwixmobile.core.search.viewmodel.effects // package org.kiwix.kiwixmobile.core.search.viewmodel.effects
//
import androidx.appcompat.app.AppCompatActivity // import androidx.appcompat.app.AppCompatActivity
import io.mockk.mockk // import io.mockk.mockk
import io.mockk.verify // import io.mockk.verify
import org.junit.jupiter.api.Test // import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao // import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem // import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem // import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
//
internal class DeleteRecentSearchTest { // internal class DeleteRecentSearchTest {
//
@Test // @Test
fun `invoke with deletes a search`() { // fun `invoke with deletes a search`() {
val searchListItem: SearchListItem = RecentSearchListItem("") // val searchListItem: SearchListItem = RecentSearchListItem("")
val recentSearchDao: NewRecentSearchDao = mockk() // val recentSearchDao: NewRecentSearchDao = mockk()
val activity: AppCompatActivity = mockk() // val activity: AppCompatActivity = mockk()
DeleteRecentSearch(searchListItem, recentSearchDao).invokeWith(activity) // DeleteRecentSearch(searchListItem, recentSearchDao).invokeWith(activity)
verify { recentSearchDao.deleteSearchString(searchListItem.value) } // 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 package org.kiwix.kiwixmobile.core.search.viewmodel.effects
// * Kiwix Android
import androidx.appcompat.app.AppCompatActivity // * Copyright (c) 2020 Kiwix <android.kiwix.org>
import io.mockk.Called // * This program is free software: you can redistribute it and/or modify
import io.mockk.mockk // * it under the terms of the GNU General Public License as published by
import io.mockk.verify // * the Free Software Foundation, either version 3 of the License, or
import org.junit.jupiter.api.Test // * (at your option) any later version.
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao // *
import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem // * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
internal class SaveSearchToRecentsTest { // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
private val newRecentSearchDao: NewRecentSearchDao = mockk() // *
private val searchListItem = RecentSearchListItem("") // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
private val activity: AppCompatActivity = mockk() // *
// */
@Test //
fun `invoke with null Id does nothing`() { // package org.kiwix.kiwixmobile.core.search.viewmodel.effects
SaveSearchToRecents(newRecentSearchDao, searchListItem, null).invokeWith(activity) //
verify { newRecentSearchDao wasNot Called } // import androidx.appcompat.app.AppCompatActivity
} // import io.mockk.Called
// import io.mockk.mockk
@Test // import io.mockk.verify
fun `invoke with non null Id saves search`() { // import org.junit.jupiter.api.Test
val id = "id" // import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
SaveSearchToRecents(newRecentSearchDao, searchListItem, id).invokeWith(activity) // import org.kiwix.kiwixmobile.core.search.adapter.SearchListItem.RecentSearchListItem
verify { newRecentSearchDao.saveSearch(searchListItem.value, id) } //
} // 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) }
// }
// }