-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
6 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#Tue Aug 28 12:14:00 IST 2018 | ||
#Sun Oct 21 00:47:40 IST 2018 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip | ||
android.enableD8=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{ | ||
"formatVersion": 1, | ||
"database": { | ||
"version": 4, | ||
"identityHash": "e6812687ff1f63ddcb6ebbf062ac6267", | ||
"entities": [ | ||
{ | ||
"tableName": "EVENT", | ||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `M_START_TIME` INTEGER)", | ||
"fields": [ | ||
{ | ||
"fieldPath": "id", | ||
"columnName": "ID", | ||
"affinity": "INTEGER", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "mStartTime", | ||
"columnName": "M_START_TIME", | ||
"affinity": "INTEGER", | ||
"notNull": false | ||
} | ||
], | ||
"primaryKey": { | ||
"columnNames": [ | ||
"ID" | ||
], | ||
"autoGenerate": true | ||
}, | ||
"indices": [], | ||
"foreignKeys": [] | ||
}, | ||
{ | ||
"tableName": "EVENT_TRIGGER", | ||
"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)", | ||
"fields": [ | ||
{ | ||
"fieldPath": "id", | ||
"columnName": "ID", | ||
"affinity": "INTEGER", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "mType", | ||
"columnName": "M_TYPE", | ||
"affinity": "INTEGER", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "mTime", | ||
"columnName": "M_TIME", | ||
"affinity": "INTEGER", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "mEventId", | ||
"columnName": "M_EVENT_ID", | ||
"affinity": "INTEGER", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "mPath", | ||
"columnName": "M_PATH", | ||
"affinity": "TEXT", | ||
"notNull": false | ||
} | ||
], | ||
"primaryKey": { | ||
"columnNames": [ | ||
"ID" | ||
], | ||
"autoGenerate": true | ||
}, | ||
"indices": [], | ||
"foreignKeys": [] | ||
} | ||
], | ||
"setupQueries": [ | ||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", | ||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"e6812687ff1f63ddcb6ebbf062ac6267\")" | ||
] | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/androidTest/java/org/havenapp/main/database/migration/RoomMigrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.havenapp.main.database.migration | ||
|
||
import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory | ||
import android.arch.persistence.room.Room | ||
import android.arch.persistence.room.testing.MigrationTestHelper | ||
import android.support.test.InstrumentationRegistry | ||
import junit.framework.Assert.assertEquals | ||
import org.havenapp.main.database.converter.HavenEventDBConverters.Companion.dateToTimestamp | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
/** | ||
* Created by Arka Prava Basu <[email protected]> on 27/10/18. | ||
*/ | ||
class RoomMigrationTest { | ||
@get:Rule | ||
val migrationTestHelper = MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), | ||
org.havenapp.main.database.HavenEventDB::class.java.canonicalName, | ||
FrameworkSQLiteOpenHelperFactory()) | ||
|
||
private var sugarDbOpenHelper: SugarDbOpenHelper? = null | ||
|
||
private val TEST_DB_NAME = "test.db" | ||
|
||
@Before | ||
fun setUpDb() { | ||
sugarDbOpenHelper = | ||
SugarDbOpenHelper(InstrumentationRegistry.getTargetContext(), TEST_DB_NAME) | ||
SugarDbTestHelper.createTables(sugarDbOpenHelper!!) | ||
} | ||
|
||
@Test | ||
fun validateMigrationAndData() { | ||
SugarDbTestHelper.insertEvent(123, sugarDbOpenHelper!!) | ||
SugarDbTestHelper.insertEventTrigger(1, "abcabd", 124, 1, sugarDbOpenHelper!!) | ||
|
||
migrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 4, | ||
true, RoomMigration()) | ||
|
||
val migratedDb = getMigratedRoomDb() | ||
|
||
val event = migratedDb.getEventDAO().getAllEvent()[0] | ||
val eventTrigger = migratedDb.getEventTriggerDAO().getAllEventTriggers()[0] | ||
|
||
assertEquals(dateToTimestamp(event.mStartTime)?.toInt(), 123) | ||
|
||
assertEquals(dateToTimestamp(eventTrigger.mTime)?.toInt(), 124) | ||
assertEquals(eventTrigger.mPath, "abcabd") | ||
assertEquals(eventTrigger.mType, 1) | ||
} | ||
|
||
@After | ||
fun clearDb() { | ||
SugarDbTestHelper.clearDb(sugarDbOpenHelper!!) | ||
} | ||
|
||
private fun getMigratedRoomDb(): org.havenapp.main.database.HavenEventDB { | ||
val db = Room.databaseBuilder(InstrumentationRegistry.getTargetContext(), | ||
org.havenapp.main.database.HavenEventDB::class.java, TEST_DB_NAME) | ||
.addMigrations(RoomMigration()) | ||
.build() | ||
|
||
migrationTestHelper.closeWhenFinished(db) | ||
|
||
return db | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/androidTest/java/org/havenapp/main/database/migration/SugarDbOpenHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.havenapp.main.database.migration | ||
|
||
import android.content.Context | ||
import android.content.Context.MODE_PRIVATE | ||
import android.database.sqlite.SQLiteDatabase | ||
import android.database.sqlite.SQLiteOpenHelper | ||
|
||
/** | ||
* Created by Arka Prava Basu <[email protected]> on 27/10/18. | ||
*/ | ||
class SugarDbOpenHelper(context: Context, dbName: String) | ||
: SQLiteOpenHelper(context, dbName, null, 3) { | ||
|
||
|
||
private val createEventTable = | ||
"CREATE TABLE IF NOT EXISTS EVENT ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_START_TIME INTEGER )" | ||
private val createEventTriggerTable = | ||
"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 )" | ||
|
||
override fun onCreate(db: SQLiteDatabase?) { | ||
db?.execSQL(createEventTable) | ||
db?.execSQL(createEventTriggerTable) | ||
} | ||
|
||
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { | ||
|
||
} | ||
|
||
override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { | ||
|
||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/androidTest/java/org/havenapp/main/database/migration/SugarDbTestHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.havenapp.main.database.migration | ||
|
||
/** | ||
* Created by Arka Prava Basu <[email protected]> on 28/10/18. | ||
*/ | ||
class SugarDbTestHelper { | ||
|
||
companion object { | ||
fun createTables(helper: SugarDbOpenHelper) { | ||
val db = helper.writableDatabase | ||
|
||
val createEventTable = | ||
"CREATE TABLE IF NOT EXISTS EVENT ( ID INTEGER PRIMARY KEY AUTOINCREMENT , M_START_TIME INTEGER )" | ||
val createEventTriggerTable = | ||
"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 )" | ||
|
||
db.execSQL(createEventTable) | ||
db.execSQL(createEventTriggerTable) | ||
|
||
db.close() | ||
} | ||
|
||
|
||
fun insertEvent(startTime: Int, helper: SugarDbOpenHelper) { | ||
val db = helper.writableDatabase | ||
|
||
db?.execSQL("INSERT INTO EVENT(M_START_TIME) VALUES ($startTime)") | ||
|
||
db.close() | ||
} | ||
|
||
fun insertEventTrigger(eventId: Int, path: String, startTime: Int, | ||
type: Int, helper: SugarDbOpenHelper) { | ||
val db = helper.writableDatabase | ||
|
||
db?.execSQL("INSERT INTO EVENT_TRIGGER(M_EVENT_ID, M_PATH, M_TIME, M_TYPE) VALUES ($eventId, \"$path\", $startTime, $type)") | ||
|
||
db.close() | ||
} | ||
|
||
fun clearDb(helper: SugarDbOpenHelper) { | ||
val db = helper.writableDatabase | ||
|
||
db?.execSQL("DROP TABLE IF EXISTS EVENT") | ||
db?.execSQL("DROP TABLE IF EXISTS EVENT_TRIGGER") | ||
|
||
db.close() | ||
} | ||
} | ||
} |