简体   繁体   English

如何测试返回DataSource.Factory的Dao方法?

[英]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. 在从我的应用程序中的SqliteOpenHelper转移到room后,我试图为DAO类编写测试。

My DAO looks something like this: 我的DAO看起来像这样:

    @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 , ie getItemList apis. 在为getAllCards编写测试时, insertCardmarkRead是微不足道的,我仍然不确定如何测试返回DataSource.Factory的api,即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 引用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. 如果对Room使用分页并使用@Dao方法返回DataSource.Factory,则生成的代码使用名为LimitOffsetDataSource的内部类来执行SQLite操作并完成PositionalDataSource合同。

source: https://commonsware.com/AndroidArch/previews/paging-beyond-room#head206 来源: https//commonsware.com/AndroidArch/previews/paging-beyond-room#head206

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 DataSource.Factory 获取数据 - How to get data from DataSource.Factory 房间本地单元测试 - 从 DataSource.Factory 查询 PagedList - Room Local Unit Test - Query PagedList from DataSource.Factory Android Room 在使用 DataSource.Factory 时如何按文本搜索? - Android Room how to search by text when using DataSource.Factory? 具有多视图 RecyclerView 的 Room DataSource.Factory - Room DataSource.Factory with multiple view RecyclerView Android 分页库 - Map 房间 DataSource.Factory&lt;*, DatabaseModel&gt; 到 DataSource.Factory&lt;*, PresenterModel&gt; - Android Paging Library - Map Room DataSource.Factory<*, DatabaseModel> to DataSource.Factory<*, PresenterModel> Android体系结构组件分页DataSource.Factory错误 - Android architecture components paging DataSource.Factory error DataSource.Factory的create方法不会得到调用 - create method of DataSource.Factory doesn't get call 自定义构造函数PageKeyedDataSource()在分页库的datasource.factory()中崩溃应用程序 - Custom constructor PageKeyedDataSource() crashes app in datasource.factory() of paging library Kotlin 协程挂起错误与 Room DataSource.Factory - Kotlin coroutine suspend error with Room DataSource.Factory DataSource.Factory 未使用正确的变量值 Android 分页 - DataSource.Factory is not using correct variable value Android Paging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM