Skip to content

Commit

Permalink
test: db migration All test
Browse files Browse the repository at this point in the history
  • Loading branch information
murjune committed Jan 22, 2025
1 parent cf2c7e2 commit ffe50d3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "12a643d55ca1f7cd0240a2b873dfe241",
"entities": [
{
"tableName": "Goal",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `isPinned` INTEGER NOT NULL, `state` TEXT NOT NULL, `createdAt` INTEGER NOT NULL, `completedAt` INTEGER)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "isPinned",
"columnName": "isPinned",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "state",
"columnName": "state",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "createdAt",
"columnName": "createdAt",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "completedAt",
"columnName": "completedAt",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"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, '12a643d55ca1f7cd0240a2b873dfe241')"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.chipichipi.dobedobe.core.database.db

import android.content.Context
import androidx.room.Room
import androidx.room.testing.MigrationTestHelper
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.DisplayName
import java.io.IOException

class DobeDobeMigrationTest {
@get:Rule
val helper =
MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
DobeDobeDatabase::class.java,
)

@Test
@DisplayName("모든 database 버전 마이그레이션 테스트")
@Throws(IOException::class)
fun test_migrateAll() {
// given
val dbName = "TEST_DB"
val oldestDbVersion = 1
val testContext = ApplicationProvider.getApplicationContext<Context>()

// when : 가장 오래된 버전의 DB를 생성하고 닫음
helper.createDatabase(dbName, oldestDbVersion)
.close()

// then : 최신 버전 DB로 마이그레이션 후 검증
Room.databaseBuilder(
testContext,
DobeDobeDatabase::class.java,
dbName,
).addMigrations(*DobeDobeDatabase.MIGRATIONS)
.build().apply {
openHelper.writableDatabase.close()
}
}
}

0 comments on commit ffe50d3

Please sign in to comment.