From 78fe18735ba4f17619ff0a01ad8f3bb47168e352 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 21 Jul 2021 12:03:18 +0300 Subject: [PATCH] Database migrations test --- app/build.gradle | 11 ++++ .../kotatsu/core/db/MangaDatabaseTest.kt | 55 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 app/src/androidTest/java/org/koitharu/kotatsu/core/db/MangaDatabaseTest.kt diff --git a/app/build.gradle b/app/build.gradle index b4f852eb3..d8fb63356 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,6 +16,7 @@ android { versionCode 367 versionName '1.1.2' generatedDensities = [] + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" kapt { arguments { @@ -41,6 +42,9 @@ android { buildFeatures { viewBinding true } + sourceSets { + androidTest.assets.srcDirs += files("$projectDir/schemas".toString()) + } lintOptions { disable 'MissingTranslation' abortOnError false @@ -106,4 +110,11 @@ dependencies { testImplementation 'org.json:json:20210307' testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1' testImplementation 'io.insert-koin:koin-test-junit4:3.1.2' + + androidTestImplementation 'androidx.test:runner:1.4.0' + androidTestImplementation 'androidx.test:rules:1.4.0' + androidTestImplementation 'androidx.test:core-ktx:1.4.0' + androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3' + androidTestImplementation 'androidx.room:room-testing:2.3.0' + androidTestImplementation 'com.google.truth:truth:1.1.3' } \ No newline at end of file diff --git a/app/src/androidTest/java/org/koitharu/kotatsu/core/db/MangaDatabaseTest.kt b/app/src/androidTest/java/org/koitharu/kotatsu/core/db/MangaDatabaseTest.kt new file mode 100644 index 000000000..f0f37c2a1 --- /dev/null +++ b/app/src/androidTest/java/org/koitharu/kotatsu/core/db/MangaDatabaseTest.kt @@ -0,0 +1,55 @@ +package org.koitharu.kotatsu.core.db + +import androidx.room.testing.MigrationTestHelper +import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.koitharu.kotatsu.core.db.migrations.* +import java.io.IOException + +@RunWith(AndroidJUnit4::class) +class MangaDatabaseTest { + + @get:Rule + val helper: MigrationTestHelper = MigrationTestHelper( + InstrumentationRegistry.getInstrumentation(), + MangaDatabase::class.java.canonicalName, + FrameworkSQLiteOpenHelperFactory() + ) + + @Test + @Throws(IOException::class) + fun migrateAll() { + helper.createDatabase(TEST_DB, 1).apply { + // TODO execSQL("") + close() + } + for (migration in migrations) { + helper.runMigrationsAndValidate( + TEST_DB, + migration.endVersion, + true, + migration + ) + } + } + + + private companion object { + + const val TEST_DB = "test-db" + + val migrations = arrayOf( + Migration1To2(), + Migration2To3(), + Migration3To4(), + Migration4To5(), + Migration5To6(), + Migration6To7(), + Migration7To8(), + ) + } +} \ No newline at end of file