Browse Source

Primitive test for the database migration

- the database schema for sugar db version 3 is mocked as I could
  export
- this test checks that the migration from Sugar To Room  is
  succesful and adds a primitive validation on the data

Signed-off-by: Arka Prava Basu <arkaprava94@gmail.com>
Arka Prava Basu 6 months ago
parent
commit
448a1fd770

+ 13 - 2
build.gradle

@@ -6,7 +6,7 @@ buildscript {
6 6
     }
7 7
     dependencies {
8 8
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
9
-        classpath 'com.android.tools.build:gradle:3.3.0-alpha07'
9
+        classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
10 10
 
11 11
     }
12 12
 
@@ -57,7 +57,7 @@ allprojects {
57 57
 
58 58
 android {
59 59
     compileSdkVersion 27
60
-    buildToolsVersion '27.0.3'
60
+    buildToolsVersion '28.0.3'
61 61
 
62 62
     packagingOptions {
63 63
         exclude 'META-INF/LICENSE.txt'
@@ -97,6 +97,12 @@ android {
97 97
 
98 98
         testInstrumentationRunner "android.test.InstrumentationTestRunner"
99 99
         testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
100
+
101
+        javaCompileOptions {
102
+            annotationProcessorOptions {
103
+                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
104
+            }
105
+        }
100 106
     }
101 107
 
102 108
 
@@ -115,6 +121,10 @@ android {
115 121
         checkReleaseBuilds false
116 122
         abortOnError false
117 123
     }
124
+
125
+    sourceSets {
126
+        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
127
+    }
118 128
 }
119 129
 
120 130
 dependencies {
@@ -157,4 +167,5 @@ dependencies {
157 167
     testImplementation "junit:junit:4.12"
158 168
     androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2"
159 169
     androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"
170
+    androidTestImplementation "android.arch.persistence.room:testing:1.1.1"
160 171
 }

+ 2 - 2
gradle/wrapper/gradle-wrapper.properties

@@ -1,7 +1,7 @@
1
-#Tue Aug 28 12:14:00 IST 2018
1
+#Sun Oct 21 00:47:40 IST 2018
2 2
 distributionBase=GRADLE_USER_HOME
3 3
 distributionPath=wrapper/dists
4 4
 zipStoreBase=GRADLE_USER_HOME
5 5
 zipStorePath=wrapper/dists
6
-distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
6
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
7 7
 android.enableD8=true

+ 83 - 0
schemas/org.havenapp.main.database.HavenEventDB/4.json

@@ -0,0 +1,83 @@
1
+{
2
+  "formatVersion": 1,
3
+  "database": {
4
+    "version": 4,
5
+    "identityHash": "e6812687ff1f63ddcb6ebbf062ac6267",
6
+    "entities": [
7
+      {
8
+        "tableName": "EVENT",
9
+        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `M_START_TIME` INTEGER)",
10
+        "fields": [
11
+          {
12
+            "fieldPath": "id",
13
+            "columnName": "ID",
14
+            "affinity": "INTEGER",
15
+            "notNull": false
16
+          },
17
+          {
18
+            "fieldPath": "mStartTime",
19
+            "columnName": "M_START_TIME",
20
+            "affinity": "INTEGER",
21
+            "notNull": false
22
+          }
23
+        ],
24
+        "primaryKey": {
25
+          "columnNames": [
26
+            "ID"
27
+          ],
28
+          "autoGenerate": true
29
+        },
30
+        "indices": [],
31
+        "foreignKeys": []
32
+      },
33
+      {
34
+        "tableName": "EVENT_TRIGGER",
35
+        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `M_TYPE` INTEGER, `M_TIME` INTEGER, `M_EVENT_ID` INTEGER, `M_PATH` TEXT)",
36
+        "fields": [
37
+          {
38
+            "fieldPath": "id",
39
+            "columnName": "ID",
40
+            "affinity": "INTEGER",
41
+            "notNull": false
42
+          },
43
+          {
44
+            "fieldPath": "mType",
45
+            "columnName": "M_TYPE",
46
+            "affinity": "INTEGER",
47
+            "notNull": false
48
+          },
49
+          {
50
+            "fieldPath": "mTime",
51
+            "columnName": "M_TIME",
52
+            "affinity": "INTEGER",
53
+            "notNull": false
54
+          },
55
+          {
56
+            "fieldPath": "mEventId",
57
+            "columnName": "M_EVENT_ID",
58
+            "affinity": "INTEGER",
59
+            "notNull": false
60
+          },
61
+          {
62
+            "fieldPath": "mPath",
63
+            "columnName": "M_PATH",
64
+            "affinity": "TEXT",
65
+            "notNull": false
66
+          }
67
+        ],
68
+        "primaryKey": {
69
+          "columnNames": [
70
+            "ID"
71
+          ],
72
+          "autoGenerate": true
73
+        },
74
+        "indices": [],
75
+        "foreignKeys": []
76
+      }
77
+    ],
78
+    "setupQueries": [
79
+      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
80
+      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"e6812687ff1f63ddcb6ebbf062ac6267\")"
81
+    ]
82
+  }
83
+}

+ 69 - 0
src/androidTest/java/org/havenapp/main/database/migration/RoomMigrationTest.kt

@@ -0,0 +1,69 @@
1
+package org.havenapp.main.database.migration
2
+
3
+import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory
4
+import android.arch.persistence.room.Room
5
+import android.arch.persistence.room.testing.MigrationTestHelper
6
+import android.support.test.InstrumentationRegistry
7
+import junit.framework.Assert.assertEquals
8
+import org.havenapp.main.database.converter.HavenEventDBConverters.Companion.dateToTimestamp
9
+import org.junit.After
10
+import org.junit.Before
11
+import org.junit.Rule
12
+import org.junit.Test
13
+
14
+/**
15
+ * Created by Arka Prava Basu <arka.basu@zomato.com> on 27/10/18.
16
+ */
17
+class RoomMigrationTest {
18
+    @get:Rule
19
+    val migrationTestHelper = MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
20
+            org.havenapp.main.database.HavenEventDB::class.java.canonicalName,
21
+            FrameworkSQLiteOpenHelperFactory())
22
+
23
+    private var sugarDbOpenHelper: SugarDbOpenHelper? = null
24
+
25
+    private val TEST_DB_NAME = "test.db"
26
+
27
+    @Before
28
+    fun setUpDb() {
29
+        sugarDbOpenHelper =
30
+                SugarDbOpenHelper(InstrumentationRegistry.getTargetContext(), TEST_DB_NAME)
31
+        SugarDbTestHelper.createTables(sugarDbOpenHelper!!)
32
+    }
33
+
34
+    @Test
35
+    fun validateMigrationAndData() {
36
+        SugarDbTestHelper.insertEvent(123, sugarDbOpenHelper!!)
37
+        SugarDbTestHelper.insertEventTrigger(1, "abcabd", 124, 1, sugarDbOpenHelper!!)
38
+
39
+        migrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 4,
40
+                true, RoomMigration())
41
+
42
+        val migratedDb = getMigratedRoomDb()
43
+
44
+        val event = migratedDb.getEventDAO().getAllEvent()[0]
45
+        val eventTrigger = migratedDb.getEventTriggerDAO().getAllEventTriggers()[0]
46
+
47
+        assertEquals(dateToTimestamp(event.mStartTime)?.toInt(), 123)
48
+
49
+        assertEquals(dateToTimestamp(eventTrigger.mTime)?.toInt(), 124)
50
+        assertEquals(eventTrigger.mPath, "abcabd")
51
+        assertEquals(eventTrigger.mType, 1)
52
+    }
53
+
54
+    @After
55
+    fun clearDb() {
56
+        SugarDbTestHelper.clearDb(sugarDbOpenHelper!!)
57
+    }
58
+
59
+    private fun getMigratedRoomDb(): org.havenapp.main.database.HavenEventDB {
60
+        val db = Room.databaseBuilder(InstrumentationRegistry.getTargetContext(),
61
+                org.havenapp.main.database.HavenEventDB::class.java, TEST_DB_NAME)
62
+                .addMigrations(RoomMigration())
63
+                .build()
64
+
65
+        migrationTestHelper.closeWhenFinished(db)
66
+
67
+        return db
68
+    }
69
+}

+ 32 - 0
src/androidTest/java/org/havenapp/main/database/migration/SugarDbOpenHelper.kt

@@ -0,0 +1,32 @@
1
+package org.havenapp.main.database.migration
2
+
3
+import android.content.Context
4
+import android.content.Context.MODE_PRIVATE
5
+import android.database.sqlite.SQLiteDatabase
6
+import android.database.sqlite.SQLiteOpenHelper
7
+
8
+/**
9
+ * Created by Arka Prava Basu <arka.basu@zomato.com> on 27/10/18.
10
+ */
11
+class SugarDbOpenHelper(context: Context, dbName: String)
12
+    : SQLiteOpenHelper(context, dbName, null, 3) {
13
+
14
+
15
+    private val createEventTable =
16
+            "CREATE TABLE IF NOT EXISTS EVENT ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_START_TIME INTEGER )"
17
+    private val createEventTriggerTable =
18
+            "CREATE TABLE IF NOT EXISTS EVENT_TRIGGER ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_EVENT_ID INTEGER, M_PATH TEXT, M_TIME INTEGER , M_TYPE INTEGER )"
19
+
20
+    override fun onCreate(db: SQLiteDatabase?) {
21
+        db?.execSQL(createEventTable)
22
+        db?.execSQL(createEventTriggerTable)
23
+    }
24
+
25
+    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
26
+
27
+    }
28
+
29
+    override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
30
+
31
+    }
32
+}

+ 50 - 0
src/androidTest/java/org/havenapp/main/database/migration/SugarDbTestHelper.kt

@@ -0,0 +1,50 @@
1
+package org.havenapp.main.database.migration
2
+
3
+/**
4
+ * Created by Arka Prava Basu <arka.basu@zomato.com> on 28/10/18.
5
+ */
6
+class SugarDbTestHelper {
7
+
8
+    companion object {
9
+        fun createTables(helper: SugarDbOpenHelper) {
10
+            val db = helper.writableDatabase
11
+
12
+            val createEventTable =
13
+                    "CREATE TABLE IF NOT EXISTS EVENT ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_START_TIME INTEGER )"
14
+            val createEventTriggerTable =
15
+                    "CREATE TABLE IF NOT EXISTS EVENT_TRIGGER ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_EVENT_ID INTEGER, M_PATH TEXT, M_TIME INTEGER , M_TYPE INTEGER )"
16
+
17
+            db.execSQL(createEventTable)
18
+            db.execSQL(createEventTriggerTable)
19
+
20
+            db.close()
21
+        }
22
+
23
+
24
+        fun insertEvent(startTime: Int, helper: SugarDbOpenHelper) {
25
+            val db = helper.writableDatabase
26
+
27
+            db?.execSQL("INSERT INTO EVENT(M_START_TIME) VALUES ($startTime)")
28
+
29
+            db.close()
30
+        }
31
+
32
+        fun insertEventTrigger(eventId: Int, path: String, startTime: Int,
33
+                               type: Int, helper: SugarDbOpenHelper) {
34
+            val db = helper.writableDatabase
35
+
36
+            db?.execSQL("INSERT INTO EVENT_TRIGGER(M_EVENT_ID, M_PATH, M_TIME, M_TYPE) VALUES ($eventId, \"$path\", $startTime, $type)")
37
+
38
+            db.close()
39
+        }
40
+
41
+        fun clearDb(helper: SugarDbOpenHelper) {
42
+            val db = helper.writableDatabase
43
+
44
+            db?.execSQL("DROP TABLE IF EXISTS EVENT")
45
+            db?.execSQL("DROP TABLE IF EXISTS EVENT_TRIGGER")
46
+
47
+            db.close()
48
+        }
49
+    }
50
+}