Skip to content

Commit

Permalink
test: add MeshtasticDatabaseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Aug 4, 2024
1 parent 8bd8bca commit 6d01fb4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ android {
lint {
abortOnError false
}
sourceSets {
// Adds exported schema location as test app assets.
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
}

// per protobuf-gradle-plugin docs, this is recommended for android
Expand Down Expand Up @@ -171,7 +175,7 @@ dependencies {
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
androidTestImplementation "androidx.room:room-testing:$room_version"

// Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.geeksville.mesh

import androidx.room.Room
import androidx.room.testing.MigrationTestHelper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.geeksville.mesh.database.MeshtasticDatabase
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException

@RunWith(AndroidJUnit4::class)
class MeshtasticDatabaseTest {

companion object {
private const val TEST_DB = "migration-test"
}

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

@Test
@Throws(IOException::class)
fun migrateAll() {
// Create earliest version of the database.
helper.createDatabase(TEST_DB, 3).apply {
close()
}

// Open latest version of the database. Room validates the schema
// once all migrations execute.
Room.databaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext,
MeshtasticDatabase::class.java,
TEST_DB
).build().apply {
openHelper.writableDatabase.close()
}
}
}

0 comments on commit 6d01fb4

Please sign in to comment.