-
-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'room_experiments' of https://github.com/archie94/haven …
…into archie94-room_experiments
- Loading branch information
Showing
38 changed files
with
1,408 additions
and
571 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 |
---|---|---|
@@ -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\")" | ||
] | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
package org.havenapp.main.database.migration | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.room.Room | ||
import androidx.room.testing.MigrationTestHelper | ||
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import junit.framework.Assert.assertEquals | ||
import org.havenapp.main.database.HavenEventDB | ||
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(), | ||
HavenEventDB::class.java.canonicalName, | ||
FrameworkSQLiteOpenHelperFactory()) | ||
|
||
private var sugarDbOpenHelper: SugarDbOpenHelper? = null | ||
|
||
private val TEST_DB_NAME = "test.db" | ||
|
||
@Before | ||
fun setUpDb() { | ||
sugarDbOpenHelper = | ||
SugarDbOpenHelper(ApplicationProvider.getApplicationContext(), 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.startTime)?.toInt(), 123) | ||
|
||
assertEquals(dateToTimestamp(eventTrigger.time)?.toInt(), 124) | ||
assertEquals(eventTrigger.path, "abcabd") | ||
assertEquals(eventTrigger.type, 1) | ||
} | ||
|
||
@After | ||
fun clearDb() { | ||
SugarDbTestHelper.clearDb(sugarDbOpenHelper!!) | ||
} | ||
|
||
private fun getMigratedRoomDb(): org.havenapp.main.database.HavenEventDB { | ||
val db = Room.databaseBuilder(ApplicationProvider.getApplicationContext(), | ||
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() | ||
} | ||
} | ||
} |
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
Oops, something went wrong.