After shifting from SqliteOpenHelper
to room
in my app, I've trying to write tests for the DAO
class.
My DAO looks something like this:
@Query("SELECT * FROM cards")
fun getAllCards(): List<CardData>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertCard(vararg cardData: CardData): List<Long>
@Query("SELECT * FROM cards ORDER BY isRead ASC, id DESC")
fun getItemList(): DataSource.Factory<Int, CardData>
@Query("SELECT * FROM cards where instr(title, :query) > 0 ORDER BY isRead ASC, id DESC")
fun getItemList(query: String): DataSource.Factory<Int, CardData>
@Query("UPDATE cards set isRead = 1 where title = :title")
fun markRead(title: String): Int
While writing test for getAllCards
, insertCard
and markRead
is trivial, I am still not sure how do I test the apis which return DataSource.Factory
, i.e getItemList
apis.
After searching on internet, I couldn't find anything related to this.
Can someone please help.
this is how I did:
val factory = dao.getItemList()
val list = (factory.create() as LimitOffsetDataSource).loadRange(0, 10)
Quoting CommonsWare
If you use paging with Room and have a @Dao method return a DataSource.Factory, the generated code uses an internal class named LimitOffsetDataSource to perform the SQLite operations and fulfill the PositionalDataSource contract.
source: https://commonsware.com/AndroidArch/previews/paging-beyond-room#head206
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With