gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:45:49 24 4
gpt4 key购买 nike

在我的应用程序中从 SqliteOpenHelper 转移到 room 之后,我尝试为 DAO 类编写测试。

我的 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

虽然为 getAllCardsinsertCardmarkRead 编写测试很简单,但我仍然不确定如何测试返回 DataSource.Factory,即getItemList api。

在网上搜索后,我找不到与此相关的任何内容。

有人可以帮忙吗

最佳答案

我是这样做的:

val factory = dao.getItemList()
val list = (factory.create() as LimitOffsetDataSource).loadRange(0, 10)

引用 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.

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

关于android - 如何测试返回 DataSource.Factory 的 Dao 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54920843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com