Skip to content

Commit

Permalink
Primitive test for the database migration
Browse files Browse the repository at this point in the history
- 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
archie94 committed Oct 28, 2018
1 parent 2d8f256 commit 448a1fd
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 4 deletions.
15 changes: 13 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.android.tools.build:gradle:3.3.0-alpha07'
classpath 'com.android.tools.build:gradle:3.4.0-alpha01'

}

Expand Down Expand Up @@ -57,7 +57,7 @@ allprojects {

android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
buildToolsVersion '28.0.3'

packagingOptions {
exclude 'META-INF/LICENSE.txt'
Expand Down Expand Up @@ -97,6 +97,12 @@ android {

testInstrumentationRunner "android.test.InstrumentationTestRunner"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}


Expand All @@ -115,6 +121,10 @@ android {
checkReleaseBuilds false
abortOnError false
}

sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
}

dependencies {
Expand Down Expand Up @@ -157,4 +167,5 @@ dependencies {
testImplementation "junit:junit:4.12"
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2"
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"
androidTestImplementation "android.arch.persistence.room:testing:1.1.1"
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
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
83 changes: 83 additions & 0 deletions schemas/org.havenapp.main.database.HavenEventDB/4.json
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\")"
]
}
}
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
}
}
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) {

}
}
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()
}
}
}

0 comments on commit 448a1fd

Please sign in to comment.