Android Room-在迁移单元测试期间验证DB索引的正确性
我们刚刚开始公开迁移单元测试代码。我们喜欢它,因为它可以帮助我们识别一些bug 这有助于我们验证预期的表结构 但是,我注意到迁移代码无法检查索引是否正确创建。如果我故意在迁移中忽略索引创建代码,那么单元测试仍然是通过的 我注意到在生成的模式json文件中,它们包含索引信息。因此,我希望迁移测试代码能够捕获丢失的索引 这是我们的迁移测试代码Android Room-在迁移单元测试期间验证DB索引的正确性,android,unit-testing,android-sqlite,android-room,androidx,Android,Unit Testing,Android Sqlite,Android Room,Androidx,我们刚刚开始公开迁移单元测试代码。我们喜欢它,因为它可以帮助我们识别一些bug 这有助于我们验证预期的表结构 但是,我注意到迁移代码无法检查索引是否正确创建。如果我故意在迁移中忽略索引创建代码,那么单元测试仍然是通过的 我注意到在生成的模式json文件中,它们包含索引信息。因此,我希望迁移测试代码能够捕获丢失的索引 这是我们的迁移测试代码 @RunWith(AndroidJUnit4.class) public class MigrationTest { private static f
@RunWith(AndroidJUnit4.class)
public class MigrationTest {
private static final String TEST_DB = "migration-test";
// Array of all migrations
private static final Migration[] ALL_MIGRATIONS = new Migration[]{
new Migration_1_2(),
new Migration_2_3(),
new Migration_3_4(),
new Migration_4_5(),
new Migration_5_6(),
new Migration_6_7(),
new Migration_7_8(),
new Migration_8_9(),
new Migration_9_10(),
new Migration_10_11(),
new Migration_11_12(),
new Migration_12_13(),
new Migration_13_14(),
new Migration_14_15(),
new Migration_15_16(),
new Migration_16_17(),
new Migration_17_18(),
new Migration_18_19(),
new Migration_19_20(),
new Migration_20_21(),
new Migration_21_22(),
new Migration_22_23(),
new Migration_23_24()
};
@Rule
public MigrationTestHelper helper;
public MigrationTest() {
helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
MyAppRoomDatabase.class.getCanonicalName(),
new FrameworkSQLiteOpenHelperFactory());
}
@Test
public void migrateAll() throws IOException {
// Create earliest version of the database.
SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
db.close();
// Open latest version of the database. Room will validate the schema
// once all migrations execute.
MyAppRoomDatabaseappDb = Room.databaseBuilder(
InstrumentationRegistry.getInstrumentation().getTargetContext(),
MyAppRoomDatabase.class,
TEST_DB)
.addMigrations(ALL_MIGRATIONS).build();
appDb.getOpenHelper().getWritableDatabase();
appDb.close();
}
}
有什么方法可以验证DB索引的正确性吗?您可以直接查询table
sqlite\u master
,也可以使用扩展名
这将是PRAGMA index_列表(tablename)
和PRAGMA index_信息(indexname)
;还有PRAGMA schema.index\u xinfo(indexname)
。我不知道有任何不那么详细的方法来检查这一点,因为androidx.room:room testing
似乎不支持这一点。然而,当将这样的查询封装到测试方法中时(例如,将表名和索引名作为参数),这仍然可以方便地认为它是可接受的。报告还描述了这一点
类的源代码确认方法
onValidateSchema()
(第430行)没有查询WHERE type='index'
。您仍然可以在问题追踪器上提交功能请求:
他们将能够提供权威的答案。索引未被验证的原因是我使用的是本地单元测试- 然而,Robolectric在模拟正确行为方面存在问题 使用仪器单元测试更安全- 这一点在本报告中提到
以下是Room开发团队的一些解释-请注意,Room的问题应该归档,这就是Github链接将指向您的地方。@ianhanniballake已经相应地更新了超链接。