Reputation: 8340
I have a test that I'm trying to test my Room database. When trying to run a test in an inMemory Database, it does not seem like it is inserting data. so the test is failing.
My Table Entity:
@Entity
class EpisodesDownload(
@PrimaryKey(autoGenerate = true)
var id: Long,
val uuid: String,
val mediaItemId: String,
val isDownloaded: Boolean,
var percentDownloaded: Int,
val dateDownload: OffsetDateTime?
) {
fun updatePercentage(percentDownloaded: Int) {
this.percentDownloaded = percentDownloaded
}
}
The Dao:
@Dao
interface EpisodesDownloadDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(episodesDownload: EpisodesDownload): Single<Long>
@Update
fun update(episodesDownload: EpisodesDownload): Completable
}
My test:
@RunWith(AndroidJUnit4::class)
class EpisodesDownloadTest {
private val db = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext,
LuminaryRoomDatabase::class.java
)
.addMigrations(LuminaryRoomDatabase.MIGRATION_1_2)
.build()
private val underTest = db.episodesDownloadDao()
@Test
fun update() {
val entity = EpisodesDownload(
0,
UUID.randomUUID().toString(),
UUID.randomUUID().toString(),
true,
100,
OffsetDateTime.now()
)
underTest.insert(entity)
val list = underTest.selectAllTest() //list.size() is equal to zero
val updated = entity
updated.updatePercentage(50)
underTest.update(updated)
underTest.selectAllTest().let {
assertThat(it, hasSize(equalTo(1))) //fails
assertThat(it[0], equalTo(updated))
}
}
}
Is it because I am setting my primary key Id = 0 when it is auto incremented? I am not getting any exceptions when running the code (other than the test failing).
Upvotes: 0
Views: 101
Reputation: 8340
Since I was returning a Single from the room database, I needed to change my code to:
val insertedId = underTest.insert(entity)
.subscribeOn(Schedulers.io())
.subscribe()
val list = underTest.selectAllTest()
val updated = entity
updated.updatePercentage(50)
updated.id = list[0].id
underTest.update(updated)
.subscribeOn(Schedulers.io())
.subscribe()
Upvotes: 1