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

时间:2019-02-28 07:55:03

标签: android android-room android-testing android-paging

在我的应用中从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的api,即{{1 }} API。

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

这就是我的做法:

typedef unsigned long long uint64_t;
typedef unsigned char uint8_t;

typedef struct frameHeader_t
    {
        uint64_t encryption       : 2;
        uint64_t frameVersion     : 2;
        uint64_t probeType        : 4;
        uint64_t dataType         : 5;
        uint64_t measurePeriod    : 3;
        uint64_t remontePeriod    : 4;
        uint64_t nbrMeasure       : 2;
        uint64_t year             : 7;
        uint64_t month            : 4;
        uint64_t day              : 5;
        uint64_t hour             : 5;
        uint64_t minute           : 6;
        uint64_t second           : 6;
        uint64_t randomization    : 5;
        uint64_t status           : 4;

    }FrameHeader;

int main()
{
    //declarations
    int i;
    uint8_t test[8];
    FrameHeader frameHeader;

    //put value
    frameHeader.encryption = 2;
    frameHeader.frameVersion = 0;
    frameHeader.probeType = 0;
    frameHeader.dataType = 1;
    frameHeader.measurePeriod = 0;
    frameHeader.remontePeriod = 6;
    frameHeader.nbrMeasure = 1;
    frameHeader.year = 19;
    frameHeader.month = 6;
    frameHeader.day = 25;
    frameHeader.hour = 15;
    frameHeader.minute = 49;
    frameHeader.second = 00;
    frameHeader.randomization = 15;
    frameHeader.status = 0;

    //copy
    memcpy(test, &frameHeader, 8);

    //print result
    for(i=0;i<sizeof(test);i++)
    {
        printf("%02x ", test[i]);
    }

    return 0;
}

引用CommonsWare

  

如果将Room与分页一起使用,并且有@Dao方法返回DataSource.Factory,则生成的代码将使用名为LimitOffsetDataSource的内部类来执行SQLite操作并履行PositionalDataSource合同。

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