Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search history: Data layer #758

Merged
merged 7 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class AppDatabaseTest {
AppDatabase.MIGRATION_69_70,
AppDatabase.MIGRATION_70_71,
AppDatabase.MIGRATION_71_72,
AppDatabase.MIGRATION_72_73
AppDatabase.MIGRATION_72_73,
AppDatabase.MIGRATION_73_74
)
.build()
// close the database and release any stream resources when the test finishes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
package au.com.shiftyjelly.pocketcasts.models.db

import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import au.com.shiftyjelly.pocketcasts.models.db.dao.SearchHistoryDao
import au.com.shiftyjelly.pocketcasts.models.entity.SearchHistoryItem
import au.com.shiftyjelly.pocketcasts.models.entity.SearchHistoryItem.Folder
import au.com.shiftyjelly.pocketcasts.models.entity.SearchHistoryItem.Podcast
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Date
import java.util.UUID

private const val SEARCH_TERM_TEST1 = "test1"
private const val SEARCH_TERM_TEST2 = "test2"

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class SearchHistoryDaoTest {
ashiagr marked this conversation as resolved.
Show resolved Hide resolved
lateinit var searchHistoryDao: SearchHistoryDao
lateinit var testDb: AppDatabase

@Before
fun setupDb() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
testDb = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
searchHistoryDao = testDb.searchHistoryDao()
}

@After
fun closeDb() {
testDb.close()
}

/* INSERT */
@Test
fun testInsertSearchTerm() = runTest {
searchHistoryDao.insert(createTermSearchHistoryItem(SEARCH_TERM_TEST1))

assertTrue(searchHistoryDao.findAll().first().term == SEARCH_TERM_TEST1)
}

@Test
fun testInsertPodcastSearchHistory() {
val uuid = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createPodcastSearchHistoryItem(uuid))

assertTrue(searchHistoryDao.findAll().first().podcast?.uuid == uuid)
}
}

@Test
fun testInsertFolderSearchHistory() {
val uuid = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createFolderSearchHistoryItem(uuid))

assertTrue(searchHistoryDao.findAll().first().folder?.uuid == uuid)
}
}

@Test
fun testInsertEpisodeSearchHistory() {
val uuid = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createEpisodeSearchHistoryItem(uuid))

assertTrue(searchHistoryDao.findAll().first().episode?.uuid == uuid)
}
}

/* MULTIPLE INSERT OR REPLACE */
@Test
fun testMultipleInsertSameSearchTerms() {
runTest {
searchHistoryDao.insert(createTermSearchHistoryItem(SEARCH_TERM_TEST1))
val modifiedPrevious = searchHistoryDao.findAll().first().modified
searchHistoryDao.insert(createTermSearchHistoryItem(SEARCH_TERM_TEST1))

val result = searchHistoryDao.findAll()
assertEquals("Insert should replace, count should be 1", 1, result.size)
assertEquals(
"Replaced search term should be on top",
true,
result.first().modified > modifiedPrevious
)
}
}

@Test
fun testMultipleInsertUniqueSearchTerms() {
runTest {
searchHistoryDao.insert(createTermSearchHistoryItem(SEARCH_TERM_TEST1))
searchHistoryDao.insert(createTermSearchHistoryItem(SEARCH_TERM_TEST2))

val result = searchHistoryDao.findAll()
assertEquals("Unique search terms should be inserted, count should be 2", 2, result.size)
assertEquals(
"Last search term inserted should be on top",
SEARCH_TERM_TEST2,
result.first().term
)
}
}

@Test
fun testMultipleInsertSamePodcastSearchHistory() {
val uuid = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createPodcastSearchHistoryItem(uuid = uuid))
val modifiedPrevious = searchHistoryDao.findAll().first().modified
searchHistoryDao.insert(createPodcastSearchHistoryItem(uuid = uuid))

val result = searchHistoryDao.findAll()
assertEquals("Same podcast search insert should replace, count should be 1", 1, result.size)
assertEquals(
"Replaced podcast search history item should be on top",
true,
result.first().modified > modifiedPrevious
)
ashiagr marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Test
fun testMultipleInsertUniquePodcastSearchHistory() {
val uuid1 = UUID.randomUUID().toString()
val uuid2 = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createPodcastSearchHistoryItem(uuid1))
searchHistoryDao.insert(createPodcastSearchHistoryItem(uuid2))

val result = searchHistoryDao.findAll()
assertEquals("Unique podcast search history should be inserted, count should be 2", 2, result.size)
assertEquals(
"Last podcast search history inserted should be on top",
uuid2,
result.first().podcast?.uuid
)
}
}

@Test
fun testMultipleInsertSameFolderSearchHistory() {
val uuid = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createFolderSearchHistoryItem(uuid))
val modifiedPrevious = searchHistoryDao.findAll().first().modified
searchHistoryDao.insert(createFolderSearchHistoryItem(uuid))

val result = searchHistoryDao.findAll()
assertEquals("Same folder search insert should replace, count should be 1", 1, result.size)
assertEquals(
"Replaced folder search should be on top",
true,
result.first().modified > modifiedPrevious
)
}
}

@Test
fun testMultipleInsertUniqueFolderSearchHistory() {
val uuid1 = UUID.randomUUID().toString()
val uuid2 = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createFolderSearchHistoryItem(uuid1))
searchHistoryDao.insert(createFolderSearchHistoryItem(uuid2))

val result = searchHistoryDao.findAll()
assertEquals("Unique folder search history should be inserted, count should be 2", 2, result.size)
assertEquals(
"Last folder search history inserted should be on top",
uuid2,
result.first().folder?.uuid
)
}
}

@Test
fun testMultipleInsertSameEpisodeSearchHistory() {
val uuid = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createEpisodeSearchHistoryItem(uuid))
val modifiedPrevious = searchHistoryDao.findAll().first().modified
searchHistoryDao.insert(createEpisodeSearchHistoryItem(uuid))

val result = searchHistoryDao.findAll()
assertEquals("Same episode insert should replace, count should be 1", 1, result.size)
assertEquals(
"Replaced episode search should be on top",
true,
result.first().modified > modifiedPrevious
)
}
}

@Test
fun testMultipleInsertUniqueEpisodeSearchHistory() {
val uuid1 = UUID.randomUUID().toString()
val uuid2 = UUID.randomUUID().toString()
runTest {
searchHistoryDao.insert(createEpisodeSearchHistoryItem(uuid1))
searchHistoryDao.insert(createEpisodeSearchHistoryItem(uuid2))

val result = searchHistoryDao.findAll()
assertEquals("Unique episode search history should be inserted, count should be 2", 2, result.size)
assertEquals(
"Last episode search history inserted should be on top",
uuid2,
result.first().episode?.uuid
)
}
}

/* DELETE */
@Test
fun testDeleteSearchHistoryItem() {
runTest {
searchHistoryDao.insert(createTermSearchHistoryItem(SEARCH_TERM_TEST1))

searchHistoryDao.delete(searchHistoryDao.findAll().first())

assertTrue(searchHistoryDao.findAll().isEmpty())
}
}

@Test
fun testDeleteAllSearchHistory() {
runTest {
val uuid = UUID.randomUUID().toString()
searchHistoryDao.insert(createTermSearchHistoryItem(SEARCH_TERM_TEST1))
searchHistoryDao.insert(createPodcastSearchHistoryItem(uuid))
searchHistoryDao.insert(createFolderSearchHistoryItem(uuid))
searchHistoryDao.insert(createEpisodeSearchHistoryItem(uuid))

searchHistoryDao.deleteAll()

assertTrue(searchHistoryDao.findAll().isEmpty())
}
}

/* HELPER FUNCTIONS */
private fun createTermSearchHistoryItem(term: String) =
SearchHistoryItem(term = term)

private fun createPodcastSearchHistoryItem(uuid: String) =
SearchHistoryItem(
podcast = Podcast(
uuid = uuid,
title = "",
author = "",
)
)

private fun createFolderSearchHistoryItem(uuid: String) =
SearchHistoryItem(folder = Folder(uuid = uuid, title = "", color = 0, podcastIds = ""))

private fun createEpisodeSearchHistoryItem(uuid: String) =
SearchHistoryItem(
episode = SearchHistoryItem.Episode(
uuid = uuid,
title = "",
publishedDate = Date(),
duration = 0.0,
)
)
}
Loading