Skip to content

Commit

Permalink
Add index migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Dec 18, 2024
1 parent 0824937 commit a4f9206
Showing 1 changed file with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,71 @@ class ResourceDatabaseMigrationTest {
assertThat(retrievedTask).isEqualTo(bedNetTask)
}

@Test fun migrate9To10_should_execute_with_no_exception(): Unit = runBlocking { TODO() }
@Test
fun migrate9To10_should_execute_with_no_exception(): Unit = runBlocking {
val patient1Id = "patient-001"
val patient1ResourceUuid = "e2c79e28-ed4d-4029-a12c-108d1eb5bedb"
val patient1: String =
Patient()
.apply {
id = patient1Id
addName(HumanName().apply { addGiven("Brad") })
}
.let { iParser.encodeResourceToString(it) }

val patient2Id = "patient-002"
val patient2ResourceUuid = "541782b3-48f5-4c36-bd20-cae265e974e7"
val patient2: String =
Patient()
.apply {
id = patient2Id
addName(HumanName().apply { addGiven("Alex") })
}
.let { iParser.encodeResourceToString(it) }

helper.createDatabase(DB_NAME, 9).apply {
execSQL(
"INSERT INTO ResourceEntity (resourceUuid, resourceType, resourceId, serializedResource) VALUES ('$patient1ResourceUuid', 'Patient', '$patient1', '$patient1');",
)
execSQL(
"INSERT INTO ResourceEntity (resourceUuid, resourceType, resourceId, serializedResource) VALUES ('$patient2ResourceUuid', 'Patient', '$patient2', '$patient2');",
)

close()
}

val migratedDatabase = helper.runMigrationsAndValidate(DB_NAME, 10, true, Migration_9_10)

val patientResult1: String?
val patientResult2: String?

migratedDatabase.let { database ->
database
.query(
"""
SELECT a.serializedResource
FROM ResourceEntity a
LEFT JOIN StringIndexEntity b
ON a.resourceUuid = b.resourceUuid AND b.index_name = 'name'
WHERE a.resourceType = 'Patient'
GROUP BY a.resourceUuid
HAVING MAX(IFNULL(b.index_value,0)) >= -9223372036854775808
ORDER BY IFNULL(b.index_value, -9223372036854775808) ASC
"""
.trimIndent(),
)
.let {
it.moveToFirst()
patientResult1 = it.getString(0)
it.moveToNext()
patientResult2 = it.getString(0)
}
}
migratedDatabase.close()

assertThat(patientResult1).isEqualTo(patient2)
assertThat(patientResult2).isEqualTo(patient1)
}

companion object {
const val DB_NAME = "migration_tests.db"
Expand Down

0 comments on commit a4f9206

Please sign in to comment.