Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Dao methods which return DataSource.Factory?

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.

like image 719
Yash Avatar asked Feb 28 '19 07:02

Yash


1 Answers

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

like image 90
guness Avatar answered Oct 30 '22 13:10

guness