From 184e0f36befdbac90813424fe44be355f0f4a275 Mon Sep 17 00:00:00 2001 From: bhakti Date: Tue, 20 Apr 2021 18:56:00 +0530 Subject: [PATCH] Add unit tests for HistoryDao.kt --- .../kiwixmobile/core/dao/HistoryDaoTest.kt | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 core/src/test/java/org/kiwix/kiwixmobile/core/dao/HistoryDaoTest.kt diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/dao/HistoryDaoTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/dao/HistoryDaoTest.kt new file mode 100644 index 000000000..6f784eab4 --- /dev/null +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/dao/HistoryDaoTest.kt @@ -0,0 +1,89 @@ +/* + * Kiwix Android + * Copyright (c) 2021 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.CapturingSlot +import io.mockk.clearAllMocks +import io.mockk.every +import io.mockk.mockk +import io.mockk.slot +import io.mockk.verify +import io.objectbox.Box +import io.objectbox.query.Query +import io.objectbox.query.QueryBuilder +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Test +import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity +import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity_ +import org.kiwix.kiwixmobile.core.page.adapter.Page +import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem +import java.util.concurrent.Callable + +internal class HistoryDaoTest { + + private val box: Box = mockk(relaxed = true) + private val historyDao = HistoryDao(box) + + @AfterEach + fun tearDown() { + clearAllMocks() + } + + @Test + fun deletePages() { + val historyItem: HistoryListItem.HistoryItem = mockk(relaxed = true) + val historyItemList: List = listOf(historyItem) + val pagesToDelete: List = historyItemList + historyDao.deletePages(pagesToDelete) + verify { historyDao.deleteHistory(historyItemList) } + } + + @Test + fun saveHistory() { + val historyItem: HistoryListItem.HistoryItem = mockk(relaxed = true) + val slot: CapturingSlot> = slot() + every { box.store.callInTx(capture(slot)) } returns Unit + val queryBuilder: QueryBuilder = mockk() + every { box.query() } returns queryBuilder + every { + queryBuilder.equal(HistoryEntity_.historyUrl, "") + } returns queryBuilder + every { queryBuilder.and() } returns queryBuilder + every { + queryBuilder.equal( + HistoryEntity_.dateString, + "" + ) + } returns queryBuilder + val query: Query = mockk(relaxed = true) + every { queryBuilder.build() } returns query + every { historyItem.historyUrl } returns "" + every { historyItem.dateString } returns "" + historyDao.saveHistory(historyItem) + slot.captured.call() + verify { query.remove() } + verify { box.put(HistoryEntity(historyItem)) } + } + + @Test + fun deleteAllHistory() { + historyDao.deleteAllHistory() + verify { box.removeAll() } + } +}