How to test Dao methods which return DataSource.Factory?
Asked Answered
C

1

7

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.

Consumerism answered 28/2, 2019 at 7:55 Comment(2)
Hi @Yashavi, have you been able to get a solution to this, other than creating a class to implement the interface and returning mocks for it's functions?Administration
@Enoobong: Hey, no. I haven't found anything yet.Consumerism
R
7

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

Repulse answered 27/6, 2019 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.