From 1fab43a4ab2f312eb95b6e0b09ea052da0508419 Mon Sep 17 00:00:00 2001 From: Stypox Date: Mon, 24 Jan 2022 23:36:51 +0100 Subject: [PATCH 1/2] Improve HistoryRecordManager tests --- .../local/history/HistoryRecordManagerTest.kt | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt b/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt index c32a43b2a2f..b4d4f336f6a 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt +++ b/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt @@ -14,6 +14,7 @@ import org.schabi.newpipe.testUtil.TrampolineSchedulerRule import java.time.LocalDateTime import java.time.OffsetDateTime import java.time.ZoneOffset +import kotlin.collections.ArrayList class HistoryRecordManagerTest { @@ -97,14 +98,24 @@ class HistoryRecordManagerTest { assertThat(database.searchHistoryDAO().all.blockingFirst()).isEmpty() } - @Test - fun getRelatedSearches_emptyQuery() { + private fun insertShuffledRelatedSearches(relatedSearches: Collection) { + + // shuffle to make sure the order of items returned by queries depends only on + // SearchHistoryEntry.creationDate, not on the actual insertion time + val copiedRelatedSearchesEntries = ArrayList(relatedSearches) + copiedRelatedSearchesEntries.shuffle() + database.searchHistoryDAO().insertAll(copiedRelatedSearchesEntries) + // make sure all entries were inserted - database.searchHistoryDAO().insertAll(RELATED_SEARCHES_ENTRIES) assertEquals( - RELATED_SEARCHES_ENTRIES.size, + relatedSearches.size, database.searchHistoryDAO().all.blockingFirst().size ) + } + + @Test + fun getRelatedSearches_emptyQuery() { + insertShuffledRelatedSearches(RELATED_SEARCHES_ENTRIES) // make sure correct number of searches is returned and in correct order val searches = manager.getRelatedSearches("", 6, 4).blockingFirst() @@ -117,14 +128,29 @@ class HistoryRecordManagerTest { } @Test - fun getRelatedSearched_nonEmptyQuery() { - // make sure all entries were inserted - database.searchHistoryDAO().insertAll(RELATED_SEARCHES_ENTRIES) - assertEquals( - RELATED_SEARCHES_ENTRIES.size, - database.searchHistoryDAO().all.blockingFirst().size + fun getRelatedSearches_emptyQuery_manyDuplicates() { + insertShuffledRelatedSearches( + listOf( + SearchHistoryEntry(time.minusSeconds(9), 3, "A"), + SearchHistoryEntry(time.minusSeconds(8), 3, "AB"), + SearchHistoryEntry(time.minusSeconds(7), 3, "A"), + SearchHistoryEntry(time.minusSeconds(6), 3, "A"), + SearchHistoryEntry(time.minusSeconds(5), 3, "BA"), + SearchHistoryEntry(time.minusSeconds(4), 3, "A"), + SearchHistoryEntry(time.minusSeconds(3), 3, "A"), + SearchHistoryEntry(time.minusSeconds(2), 0, "A"), + SearchHistoryEntry(time.minusSeconds(1), 2, "AA"), + ) ) + val searches = manager.getRelatedSearches("", 9, 3).blockingFirst() + assertThat(searches).containsExactly("AA", "A", "BA") + } + + @Test + fun getRelatedSearched_nonEmptyQuery() { + insertShuffledRelatedSearches(RELATED_SEARCHES_ENTRIES) + // make sure correct number of searches is returned and in correct order val searches = manager.getRelatedSearches("A", 3, 5).blockingFirst() assertThat(searches).containsExactly( From f5c6f232556ef2c9bd14ff1291271fee0c0a7910 Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 26 Jan 2022 09:36:04 +0100 Subject: [PATCH 2/2] Improve shuffle as requested --- .../newpipe/local/history/HistoryRecordManagerTest.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt b/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt index b4d4f336f6a..24be0f868d1 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt +++ b/app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt @@ -14,7 +14,6 @@ import org.schabi.newpipe.testUtil.TrampolineSchedulerRule import java.time.LocalDateTime import java.time.OffsetDateTime import java.time.ZoneOffset -import kotlin.collections.ArrayList class HistoryRecordManagerTest { @@ -101,10 +100,9 @@ class HistoryRecordManagerTest { private fun insertShuffledRelatedSearches(relatedSearches: Collection) { // shuffle to make sure the order of items returned by queries depends only on - // SearchHistoryEntry.creationDate, not on the actual insertion time - val copiedRelatedSearchesEntries = ArrayList(relatedSearches) - copiedRelatedSearchesEntries.shuffle() - database.searchHistoryDAO().insertAll(copiedRelatedSearchesEntries) + // SearchHistoryEntry.creationDate, not on the actual insertion time, so that we can + // verify that the `ORDER BY` clause does its job + database.searchHistoryDAO().insertAll(relatedSearches.shuffled()) // make sure all entries were inserted assertEquals(