-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Fix order of local search results #7491
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
app/src/androidTest/java/org/schabi/newpipe/local/history/HistoryRecordManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package org.schabi.newpipe.local.history | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.Timeout | ||
import org.schabi.newpipe.database.AppDatabase | ||
import org.schabi.newpipe.database.history.model.SearchHistoryEntry | ||
import org.schabi.newpipe.testUtil.TestDatabase | ||
import org.schabi.newpipe.testUtil.TrampolineSchedulerRule | ||
import java.time.OffsetDateTime | ||
import java.util.concurrent.TimeUnit | ||
|
||
class HistoryRecordManagerTest { | ||
|
||
private lateinit var manager: HistoryRecordManager | ||
private lateinit var database: AppDatabase | ||
|
||
@get:Rule | ||
val trampolineScheduler = TrampolineSchedulerRule() | ||
|
||
@get:Rule | ||
val timeout = Timeout(1, TimeUnit.SECONDS) | ||
|
||
@Before | ||
fun setup() { | ||
database = TestDatabase.createReplacingNewPipeDatabase() | ||
manager = HistoryRecordManager(ApplicationProvider.getApplicationContext()) | ||
} | ||
|
||
@After | ||
fun cleanUp() { | ||
database.close() | ||
} | ||
|
||
@Test | ||
fun onSearched() { | ||
manager.onSearched(0, "Hello").test().await().assertValue(1) | ||
|
||
// For some reason the Flowable returned by getAll() never completes, so we can't assert | ||
// that the number of Lists it returns is exactly 1, we can only check if the first List is | ||
// correct. Why on earth has a Flowable been used instead of a Single for getAll()?!? | ||
val entities = database.searchHistoryDAO().all.blockingFirst() | ||
assertEquals(1, entities.size) | ||
assertEquals(1, entities[0].id) | ||
assertEquals(0, entities[0].serviceId) | ||
assertEquals("Hello", entities[0].search) | ||
} | ||
|
||
@Test | ||
fun deleteSearchHistory() { | ||
val entries = listOf( | ||
SearchHistoryEntry(OffsetDateTime.now(), 0, "A"), | ||
SearchHistoryEntry(OffsetDateTime.now(), 2, "A"), | ||
SearchHistoryEntry(OffsetDateTime.now(), 1, "B"), | ||
SearchHistoryEntry(OffsetDateTime.now(), 0, "B"), | ||
) | ||
|
||
// make sure all 4 were inserted | ||
database.searchHistoryDAO().insertAll(entries) | ||
assertEquals(entries.size, database.searchHistoryDAO().all.blockingFirst().size) | ||
|
||
// try to delete only "A" entries, "B" entries should be untouched | ||
manager.deleteSearchHistory("A").test().await().assertValue(2) | ||
val entities = database.searchHistoryDAO().all.blockingFirst() | ||
assertEquals(2, entities.size) | ||
assertTrue(entries[2].hasEqualValues(entities[0])) | ||
assertTrue(entries[3].hasEqualValues(entities[1])) | ||
|
||
// assert that nothing happens if we delete a search query that does exist in the db | ||
manager.deleteSearchHistory("A").test().await().assertValue(0) | ||
val entities2 = database.searchHistoryDAO().all.blockingFirst() | ||
assertEquals(2, entities2.size) | ||
assertTrue(entries[2].hasEqualValues(entities2[0])) | ||
assertTrue(entries[3].hasEqualValues(entities2[1])) | ||
|
||
// delete all remaining entries | ||
manager.deleteSearchHistory("B").test().await().assertValue(2) | ||
assertEquals(0, database.searchHistoryDAO().all.blockingFirst().size) | ||
} | ||
|
||
@Test | ||
fun deleteCompleteSearchHistory() { | ||
val entries = listOf( | ||
SearchHistoryEntry(OffsetDateTime.now(), 1, "A"), | ||
SearchHistoryEntry(OffsetDateTime.now(), 2, "B"), | ||
SearchHistoryEntry(OffsetDateTime.now(), 0, "C"), | ||
) | ||
|
||
// make sure all 3 were inserted | ||
database.searchHistoryDAO().insertAll(entries) | ||
assertEquals(entries.size, database.searchHistoryDAO().all.blockingFirst().size) | ||
|
||
// should remove everything | ||
manager.deleteCompleteSearchHistory().test().await().assertValue(entries.size) | ||
assertEquals(0, database.searchHistoryDAO().all.blockingFirst().size) | ||
} | ||
|
||
@Test | ||
fun getRelatedSearches_emptyQuery() { | ||
// make sure all entries were inserted | ||
database.searchHistoryDAO().insertAll(RELATED_SEARCHES_ENTRIES) | ||
assertEquals( | ||
RELATED_SEARCHES_ENTRIES.size, | ||
database.searchHistoryDAO().all.blockingFirst().size | ||
) | ||
|
||
// make sure correct number of searches is returned and in correct order | ||
val searches = manager.getRelatedSearches("", 6, 4).blockingFirst() | ||
assertEquals(4, searches.size) | ||
assertEquals(RELATED_SEARCHES_ENTRIES[6].search, searches[0]) // A (even if in two places) | ||
assertEquals(RELATED_SEARCHES_ENTRIES[4].search, searches[1]) // B | ||
assertEquals(RELATED_SEARCHES_ENTRIES[5].search, searches[2]) // AA | ||
assertEquals(RELATED_SEARCHES_ENTRIES[2].search, searches[3]) // BA | ||
} | ||
|
||
@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 | ||
) | ||
|
||
// make sure correct number of searches is returned and in correct order | ||
val searches = manager.getRelatedSearches("A", 3, 5).blockingFirst() | ||
assertEquals(3, searches.size) | ||
assertEquals(RELATED_SEARCHES_ENTRIES[6].search, searches[0]) // A (even if in two places) | ||
assertEquals(RELATED_SEARCHES_ENTRIES[5].search, searches[1]) // AA | ||
assertEquals(RELATED_SEARCHES_ENTRIES[1].search, searches[2]) // BA | ||
|
||
// also make sure that the string comparison is case insensitive | ||
val searches2 = manager.getRelatedSearches("a", 3, 5).blockingFirst() | ||
assertEquals(searches, searches2) | ||
} | ||
|
||
companion object { | ||
val RELATED_SEARCHES_ENTRIES = listOf( | ||
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(7), 2, "AC"), | ||
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(6), 0, "ABC"), | ||
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(5), 1, "BA"), | ||
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(4), 3, "A"), | ||
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(2), 0, "B"), | ||
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(3), 2, "AA"), | ||
SearchHistoryEntry(OffsetDateTime.now().minusSeconds(1), 1, "A"), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/src/androidTest/java/org/schabi/newpipe/testUtil/TestDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.schabi.newpipe.testUtil | ||
|
||
import androidx.room.Room | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.Assert.assertSame | ||
import org.schabi.newpipe.NewPipeDatabase | ||
import org.schabi.newpipe.database.AppDatabase | ||
|
||
class TestDatabase { | ||
companion object { | ||
fun createReplacingNewPipeDatabase(): AppDatabase { | ||
val database = Room.inMemoryDatabaseBuilder( | ||
ApplicationProvider.getApplicationContext(), | ||
AppDatabase::class.java | ||
) | ||
.allowMainThreadQueries() | ||
.build() | ||
|
||
val databaseField = NewPipeDatabase::class.java.getDeclaredField("databaseInstance") | ||
databaseField.isAccessible = true | ||
databaseField.set(NewPipeDatabase::class, database) | ||
|
||
assertSame( | ||
"Mocking database failed!", | ||
database, | ||
NewPipeDatabase.getInstance(ApplicationProvider.getApplicationContext()) | ||
) | ||
|
||
return database | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆