mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 08:30:44 +00:00
Add DatabaseMocker to mock NewPipeDatabase
This commit is contained in:
parent
8a5e2ffa57
commit
7d6688f497
@ -1,7 +1,5 @@
|
|||||||
package org.schabi.newpipe.local.playlist
|
package org.schabi.newpipe.local.playlist
|
||||||
|
|
||||||
import androidx.room.Room
|
|
||||||
import androidx.test.core.app.ApplicationProvider
|
|
||||||
import org.junit.After
|
import org.junit.After
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
@ -10,6 +8,7 @@ import org.junit.rules.Timeout
|
|||||||
import org.schabi.newpipe.database.AppDatabase
|
import org.schabi.newpipe.database.AppDatabase
|
||||||
import org.schabi.newpipe.database.stream.model.StreamEntity
|
import org.schabi.newpipe.database.stream.model.StreamEntity
|
||||||
import org.schabi.newpipe.extractor.stream.StreamType
|
import org.schabi.newpipe.extractor.stream.StreamType
|
||||||
|
import org.schabi.newpipe.testUtil.TestDatabase
|
||||||
import org.schabi.newpipe.testUtil.TrampolineSchedulerRule
|
import org.schabi.newpipe.testUtil.TrampolineSchedulerRule
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
@ -22,17 +21,11 @@ class LocalPlaylistManagerTest {
|
|||||||
val trampolineScheduler = TrampolineSchedulerRule()
|
val trampolineScheduler = TrampolineSchedulerRule()
|
||||||
|
|
||||||
@get:Rule
|
@get:Rule
|
||||||
val timeout = Timeout(10, TimeUnit.SECONDS)
|
val timeout = Timeout(1, TimeUnit.SECONDS)
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setup() {
|
fun setup() {
|
||||||
database = Room.inMemoryDatabaseBuilder(
|
database = TestDatabase.createReplacingNewPipeDatabase()
|
||||||
ApplicationProvider.getApplicationContext(),
|
|
||||||
AppDatabase::class.java
|
|
||||||
)
|
|
||||||
.allowMainThreadQueries()
|
|
||||||
.build()
|
|
||||||
|
|
||||||
manager = LocalPlaylistManager(database)
|
manager = LocalPlaylistManager(database)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user