Skip to content

Commit

Permalink
Merge branch 'master' into master-release
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Sep 10, 2024
2 parents 9d2f6d1 + 8301d62 commit 60d20e1
Show file tree
Hide file tree
Showing 15 changed files with 579 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.google.android.fhir.db.impl
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.filters.MediumTest
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.rest.gclient.StringClientParam
import ca.uhn.fhir.rest.param.ParamPrefixEnum
import com.google.android.fhir.DateProvider
Expand Down Expand Up @@ -4209,6 +4211,142 @@ class DatabaseImplTest {
assertThat(searchedObservations[0].logicalId).isEqualTo(locallyCreatedObservationResourceId)
}

@Test
fun updateResourcePostSync_shouldUpdateResourceId() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
database.insert(preSyncPatient)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

val patientResourceEntityPostSync =
database.selectEntity(preSyncPatient.resourceType, postSyncResourceId)
assertThat(patientResourceEntityPostSync.resourceId).isEqualTo(postSyncResourceId)
}

@Test
fun updateResourcePostSync_shouldUpdateResourceMeta() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
database.insert(preSyncPatient)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

val patientResourceEntityPostSync =
database.selectEntity(preSyncPatient.resourceType, postSyncResourceId)
assertThat(patientResourceEntityPostSync.versionId).isEqualTo(newVersionId)
assertThat(patientResourceEntityPostSync.lastUpdatedRemote?.toEpochMilli())
.isEqualTo(lastUpdatedRemote.toEpochMilli())
}

@Test
fun updateResourcePostSync_shouldDeleteOldResourceId() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
database.insert(preSyncPatient)
val postSyncResourceId = "patient2"

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
null,
null,
)

val exception =
assertThrows(ResourceNotFoundException::class.java) {
runBlocking { database.select(ResourceType.Patient, "patient1") }
}
assertThat(exception.message).isEqualTo("Resource not found with type Patient and id patient1!")
}

@Test
fun updateResourcePostSync_shouldUpdateReferringResourceReferenceValue() = runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
val observation =
Observation().apply {
id = "observation1"
subject = Reference().apply { reference = "Patient/patient1" }
}
database.insert(preSyncPatient, observation)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

assertThat(
(database.select(ResourceType.Observation, "observation1") as Observation)
.subject
.reference,
)
.isEqualTo("Patient/patient2")
}

@Test
fun updateResourcePostSync_shouldUpdateReferringResourceReferenceValueInLocalChange() =
runBlocking {
val preSyncPatient = Patient().apply { id = "patient1" }
val observation =
Observation().apply {
id = "observation1"
subject = Reference().apply { reference = "Patient/patient1" }
}
database.insert(preSyncPatient, observation)
val postSyncResourceId = "patient2"
val newVersionId = "1"
val lastUpdatedRemote = Instant.now()

database.updateResourcePostSync(
preSyncPatient.logicalId,
postSyncResourceId,
preSyncPatient.resourceType,
newVersionId,
lastUpdatedRemote,
)

assertThat(
(database.select(ResourceType.Observation, "observation1") as Observation)
.subject
.reference,
)
.isEqualTo("Patient/patient2")
val observationLocalChanges =
database.getLocalChanges(
observation.resourceType,
observation.logicalId,
)
val observationReferenceValue =
(FhirContext.forCached(FhirVersionEnum.R4)
.newJsonParser()
.parseResource(observationLocalChanges.first().payload) as Observation)
.subject
.reference
assertThat(observationReferenceValue).isEqualTo("Patient/$postSyncResourceId")
}

@Test // https://github.com/google/android-fhir/issues/2512
fun included_results_sort_ascending_should_have_distinct_resources() = runBlocking {
/**
Expand Down
Loading

0 comments on commit 60d20e1

Please sign in to comment.