java - Android 房间迁移测试失败,但没有任何改变
问题描述
我正在尝试测试 Room 迁移,但测试总是失败。模型甚至没有变化。我只增加了版本号,迁移为空——测试仍然失败。我不明白我做错了什么。
假设我们有一个实体EntityExample
,有两列a
和b
。主键由a
和组成b
,我们对两列都有索引。代码如下所示:
@Entity(primaryKeys = {"a", "b"}, indices = {@Index("a"), @Index("b")})
public class EntityExample {
@NonNull public Long a;
@NonNull public Long b;
}
此外,我们在版本 1 中的数据库:
@Database(version=1,entities={EntityExample.class})
public abstract class DBExample extends RoomDatabase {
}
在版本 2 中:
@Database(version=2,entities={EntityExample.class})
public abstract class DBExample extends RoomDatabase {
}
此外,还有迁移:
public class MigrationExample {
public final static Migration MIGRATION_1_2 = new Migration(1,2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
}
};
}
为了在测试中访问模式,在 build.gradle 中添加了:
android {
sourceSets {
// Adds exported schema location as test app assets.
debug.assets.srcDirs += files("$projectDir/schemas".toString())
}
}
最后,测试:
@Config(sdk = Build.VERSION_CODES.O_MR1)
@RunWith(RobolectricTestRunner.class)
public class MigrationExampleTest {
@Rule
public MigrationTestHelper helper;
public MigrationExampleTest() {
helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
DBExample.class.getCanonicalName(),
new FrameworkSQLiteOpenHelperFactory());
}
@Test
public void migrationTest() throws IOException {
SupportSQLiteDatabase dbV1 = helper.createDatabase("migration-test", 1);
dbV1.close();
DBExample dbV2 = Room.databaseBuilder(
InstrumentationRegistry.getInstrumentation().getTargetContext(),
DBExample.class,
"migration-test")
.addMigrations(MigrationExample.MIGRATION_1_2)
.build();
dbV2.getOpenHelper().getWritableDatabase();
dbV2.close();
}
}
测试失败就行dbV2.getOpenHelper().getWritableDatabase();
了
java.lang.IllegalStateException: Migration didn't properly handle: EntityExample(....EntityExample).
Expected:
TableInfo{name='EntityExample', columns={a=Column{name='a', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, b=Column{name='b', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=2, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_EntityExample_a', unique=false, columns=[a]}, Index{name='index_EntityExample_b', unique=false, columns=[b]}]}
Found:
TableInfo{name='EntityExample', columns={a=Column{name='a', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, b=Column{name='b', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=null}
如您所见(向右滚动测试输出时),找到的 TableInfo 中缺少索引部分。此外,primaryKeyPosition的值b
在 1 和 2 之间有所不同。我不明白为什么,我不知道究竟是什么导致了测试失败,也不知道如何解决这个问题。运行应用程序有效,不会引发异常。我检查了模式,除了更新的版本号(参见https://justpaste.it/68l2b和https://justpaste.it/3r45p)之外,它们完全一样。但是,测试失败!
解决方案
如果您还没有,可以查看这篇关于迁移测试的好文章:https ://medium.com/androiddevelopers/testing-room-migrations-be93cdb0d975
然后,在您的迁移中,您必须实际迁移数据库,执行 SQL 以从 1 变为 2,例如通过创建索引。
Room 处理迁移链接、结构完整性和测试,但不处理迁移本身。在文档https://developer.android.com/training/data-storage/room/migrating-db-versions他们会迁移他们的数据库,你也应该这样做。
推荐阅读
- xaml - 为什么此 XAML C# 代码版本之间的 UI 输出不同?
- python - 函数不返回或附加到新列表
- ios - 将数据传递给实例化的视图控制器而无需 segue
- regex - 检测自由标记中的单引号
- c# - 无法在使用电子表格 API 添加的 Google Drive API 中获取电子表格
- python-3.x - NameError:未定义全局名称“Pubnub”
- sublimetext3 - 每当我写一行文字时,有没有办法在崇高的文字左侧显示当前时间戳?
- actions-on-google - 使用 DialogFlow 接收消息
- php - Laravel 查询 whereHas 和 whereIn
- enums - 将所有变体实现相同特征的枚举转换为 Rust 中的框?