Skip to content

Commit 6770bd8

Browse files
committed
Assert destructive migrations
1 parent 1541387 commit 6770bd8

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

library/src/test/assets/1.db

12 KB
Binary file not shown.

library/src/test/assets/2.db

12 KB
Binary file not shown.

library/src/test/java/com/github/reline/sqlite/db/SQLiteCopyOpenHelperTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.sqlite.db.SupportSQLiteOpenHelper
66
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
77
import androidx.test.core.app.ApplicationProvider
88
import androidx.test.ext.junit.runners.AndroidJUnit4
9+
import org.junit.Assert.assertEquals
910
import org.junit.Test
1011

1112
import org.junit.runner.RunWith
@@ -24,6 +25,37 @@ class SQLiteCopyOpenHelperTest {
2425
.writableDatabase
2526
.close()
2627
}
28+
29+
@Test
30+
fun destructivelyMigrate() {
31+
val context = ApplicationProvider.getApplicationContext<Context>()
32+
val configBuilder = SupportSQLiteOpenHelper.Configuration.builder(context)
33+
.name("1.db")
34+
35+
SQLiteCopyOpenHelper.Factory(context, CopyFromAssetPath("1.db"), FrameworkSQLiteOpenHelperFactory())
36+
.create(configBuilder.callback(TestCallback(1)).build())
37+
.readableDatabase.use { db ->
38+
assertEquals(1, db.version)
39+
db.beginTransaction()
40+
val cursor = db.query("""SELECT value FROM Test LIMIT 1""")
41+
cursor.moveToFirst()
42+
val value = cursor.getInt(0)
43+
assertEquals(1, value)
44+
db.endTransaction()
45+
}
46+
47+
SQLiteCopyOpenHelper.Factory(context, CopyFromAssetPath("2.db"), FrameworkSQLiteOpenHelperFactory())
48+
.create(configBuilder.callback(TestCallback(2)).build())
49+
.readableDatabase.use { db ->
50+
assertEquals(2, db.version)
51+
db.beginTransaction()
52+
val cursor = db.query("""SELECT value FROM Test LIMIT 1""")
53+
cursor.moveToFirst()
54+
val value = cursor.getInt(0)
55+
assertEquals(2, value)
56+
db.endTransaction()
57+
}
58+
}
2759
}
2860

2961
private class TestCallback(version: Int = 1) : SupportSQLiteOpenHelper.Callback(version) {

0 commit comments

Comments
 (0)