【问题标题】:Android Room migrations test: discrepancy in primary key position causes unwarranted test failuresAndroid Room迁移测试:主键位置的差异导致无根据的测试失败
【发布时间】:2020-12-13 10:03:12
【问题描述】:

Android 上的 Room 具有一种机制,您可以通过该机制将数据库架构导出为 JSON,然后在单元测试套件中根据导出的架构验证数据库迁移。我在具有多个主键的表上遇到了问题,其中主键位置在迁移运行后都是 1。但是,模式导出的 V1 和 V2 的主键都以相同的顺序列出。此外,导出到 JSON 的表架构包含一个空的索引列表 ([]),而内存中的测试数据库是使用空索引创建的。

我很好奇这是否是 Room 中的错误,或者我是否可能误用了测试框架。给定迁移的唯一变化是添加了一个全新且不相关的表。这是我的测试代码:

@RunWith(RobolectricTestRunner::class)
class MigrationTest {
    @Rule @JvmField
    val helper: MigrationTestHelper = MigrationTestHelper(
        InstrumentationRegistry.getInstrumentation(),
        ApplicationDatabase::class.java.canonicalName,
        FrameworkSQLiteOpenHelperFactory()
    )

    @Test
    @Throws(IOException::class)
    fun `Test migration from 1 to 2`() {
        helper.createDatabase(TEST_DB, 1).apply {
            close()
        }

        helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2)
    }

    companion object {
        const val TEST_DB = "migration-test"
    }
}

运行此测试时,我收到以下错误:

java.lang.IllegalStateException: Migration didn't properly handle: Foo
Expected: TableInfo{name='Foo', columns={bar=Column{name='bar', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, baz=Column{name='baz', type='TEXT', affinity='3', notNull=true, primaryKeyPosition=2, defaultValue='null'}}, foreignKeys=[], indices=[]} 
found: TableInfo{name='Foo', columns={bar=Column{name='bar', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, baz=Column{name='baz', type='TEXT', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=null}

用于显示列顺序的 JSON 模式导出的 sn-p 在两种情况下都是相同的。

1.json:

"tableName": "Foo",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`bar` INTEGER NOT NULL, `baz` INTEGER NOT NULL, PRIMARY KEY(`bar`, `baz`))",

2.json:

 "tableName": "Foo",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`bar` INTEGER NOT NULL, `baz` INTEGER NOT NULL, PRIMARY KEY(`bar`, `baz`))",

【问题讨论】:

  • 嘿兄弟你有解决这个问题的办法吗?

标签: android android-sqlite android-room


【解决方案1】:

我有同样的问题。我通过更新 robolectric 解决了这个问题:从 4.3 到 4.5.1。

见:https://github.com/robolectric/robolectric/issues/4209

已在 robolectric 4.5 版本中推送了针对此问题的补丁

【讨论】:

  • 虽然这可能会回答这个问题,但如果可能的话,您应该edit 您的答案包括答案本身提供的链接中最重要的信息。如果链接停止工作或内容发生重大变化,这有助于防止您的答案无效。
猜你喜欢
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 2017-02-18
  • 2015-02-05
  • 1970-01-01
相关资源
最近更新 更多