Skip to content

Commit 2963cd5

Browse files
committed
Add HistoryRecordManagerTest
1 parent 7d6688f commit 2963cd5

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package org.schabi.newpipe.local.history
2+
3+
import androidx.test.core.app.ApplicationProvider
4+
import org.junit.After
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Assert.assertTrue
7+
import org.junit.Before
8+
import org.junit.Rule
9+
import org.junit.Test
10+
import org.junit.rules.Timeout
11+
import org.schabi.newpipe.database.AppDatabase
12+
import org.schabi.newpipe.database.history.model.SearchHistoryEntry
13+
import org.schabi.newpipe.testUtil.TestDatabase
14+
import org.schabi.newpipe.testUtil.TrampolineSchedulerRule
15+
import java.time.OffsetDateTime
16+
import java.util.concurrent.TimeUnit
17+
18+
class HistoryRecordManagerTest {
19+
20+
private lateinit var manager: HistoryRecordManager
21+
private lateinit var database: AppDatabase
22+
23+
@get:Rule
24+
val trampolineScheduler = TrampolineSchedulerRule()
25+
26+
@get:Rule
27+
val timeout = Timeout(1, TimeUnit.SECONDS)
28+
29+
@Before
30+
fun setup() {
31+
database = TestDatabase.createReplacingNewPipeDatabase()
32+
manager = HistoryRecordManager(ApplicationProvider.getApplicationContext())
33+
}
34+
35+
@After
36+
fun cleanUp() {
37+
database.close()
38+
}
39+
40+
@Test
41+
fun onSearched() {
42+
manager.onSearched(0, "Hello").test().await().assertValue(1)
43+
44+
// For some reason the Flowable returned by getAll() never completes, so we can't assert
45+
// that the number of Lists it returns is exactly 1, we can only check if the first List is
46+
// correct. Why on earth has a Flowable been used instead of a Single for getAll()?!?
47+
val entities = database.searchHistoryDAO().all.blockingFirst()
48+
assertEquals(1, entities.size)
49+
assertEquals(1, entities[0].id)
50+
assertEquals(0, entities[0].serviceId)
51+
assertEquals("Hello", entities[0].search)
52+
}
53+
54+
@Test
55+
fun deleteSearchHistory() {
56+
val entries = listOf(
57+
SearchHistoryEntry(OffsetDateTime.now(), 0, "A"),
58+
SearchHistoryEntry(OffsetDateTime.now(), 2, "A"),
59+
SearchHistoryEntry(OffsetDateTime.now(), 1, "B"),
60+
SearchHistoryEntry(OffsetDateTime.now(), 0, "B"),
61+
)
62+
63+
// make sure all 4 were inserted
64+
database.searchHistoryDAO().insertAll(entries)
65+
assertEquals(entries.size, database.searchHistoryDAO().all.blockingFirst().size)
66+
67+
// try to delete only "A" entries, "B" entries should be untouched
68+
manager.deleteSearchHistory("A").test().await().assertValue(2)
69+
val entities = database.searchHistoryDAO().all.blockingFirst()
70+
assertEquals(2, entities.size)
71+
assertTrue(entries[2].hasEqualValues(entities[0]))
72+
assertTrue(entries[3].hasEqualValues(entities[1]))
73+
74+
// assert that nothing happens if we delete a search query that does exist in the db
75+
manager.deleteSearchHistory("A").test().await().assertValue(0)
76+
val entities2 = database.searchHistoryDAO().all.blockingFirst()
77+
assertEquals(2, entities2.size)
78+
assertTrue(entries[2].hasEqualValues(entities2[0]))
79+
assertTrue(entries[3].hasEqualValues(entities2[1]))
80+
81+
// delete all remaining entries
82+
manager.deleteSearchHistory("B").test().await().assertValue(2)
83+
assertEquals(0, database.searchHistoryDAO().all.blockingFirst().size)
84+
}
85+
86+
@Test
87+
fun deleteCompleteSearchHistory() {
88+
val entries = listOf(
89+
SearchHistoryEntry(OffsetDateTime.now(), 1, "A"),
90+
SearchHistoryEntry(OffsetDateTime.now(), 2, "B"),
91+
SearchHistoryEntry(OffsetDateTime.now(), 0, "C"),
92+
)
93+
94+
// make sure all 3 were inserted
95+
database.searchHistoryDAO().insertAll(entries)
96+
assertEquals(entries.size, database.searchHistoryDAO().all.blockingFirst().size)
97+
98+
// should remove everything
99+
manager.deleteCompleteSearchHistory().test().await().assertValue(entries.size)
100+
assertEquals(0, database.searchHistoryDAO().all.blockingFirst().size)
101+
}
102+
103+
@Test
104+
fun getRelatedSearches_emptyQuery() {
105+
// make sure all entries were inserted
106+
database.searchHistoryDAO().insertAll(RELATED_SEARCHES_ENTRIES)
107+
assertEquals(
108+
RELATED_SEARCHES_ENTRIES.size,
109+
database.searchHistoryDAO().all.blockingFirst().size
110+
)
111+
112+
// make sure correct number of searches is returned and in correct order
113+
val searches = manager.getRelatedSearches("", 6, 4).blockingFirst()
114+
assertEquals(4, searches.size)
115+
assertEquals(RELATED_SEARCHES_ENTRIES[6].search, searches[0]) // A (even if in two places)
116+
assertEquals(RELATED_SEARCHES_ENTRIES[4].search, searches[1]) // B
117+
assertEquals(RELATED_SEARCHES_ENTRIES[5].search, searches[2]) // AA
118+
assertEquals(RELATED_SEARCHES_ENTRIES[2].search, searches[3]) // BA
119+
}
120+
121+
@Test
122+
fun getRelatedSearched_nonEmptyQuery() {
123+
// make sure all entries were inserted
124+
database.searchHistoryDAO().insertAll(RELATED_SEARCHES_ENTRIES)
125+
assertEquals(
126+
RELATED_SEARCHES_ENTRIES.size,
127+
database.searchHistoryDAO().all.blockingFirst().size
128+
)
129+
130+
// make sure correct number of searches is returned and in correct order
131+
val searches = manager.getRelatedSearches("A", 3, 5).blockingFirst()
132+
assertEquals(3, searches.size)
133+
assertEquals(RELATED_SEARCHES_ENTRIES[6].search, searches[0]) // A (even if in two places)
134+
assertEquals(RELATED_SEARCHES_ENTRIES[5].search, searches[1]) // AA
135+
assertEquals(RELATED_SEARCHES_ENTRIES[1].search, searches[2]) // BA
136+
137+
// also make sure that the string comparison is case insensitive
138+
val searches2 = manager.getRelatedSearches("a", 3, 5).blockingFirst()
139+
assertEquals(searches, searches2)
140+
}
141+
142+
companion object {
143+
val RELATED_SEARCHES_ENTRIES = listOf(
144+
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(7), 2, "AC"),
145+
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(6), 0, "ABC"),
146+
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(5), 1, "BA"),
147+
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(4), 3, "A"),
148+
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(2), 0, "B"),
149+
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(3), 2, "AA"),
150+
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(1), 1, "A"),
151+
)
152+
}
153+
}

0 commit comments

Comments
 (0)