Skip to content

Commit

Permalink
Fixed the db query to get references for LocalChanges (google#2497)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-07 authored Mar 21, 2024
1 parent 693b8c6 commit bb19c19
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,7 @@ import org.hl7.fhir.r4.model.CarePlan
import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Enumerations
import org.hl7.fhir.r4.model.Observation
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.Reference
import org.junit.After
Expand Down Expand Up @@ -356,4 +357,44 @@ class LocalChangeDaoTest {
assertThat(localChangeResourceReferences[2].resourceReferenceValue)
.isEqualTo(practitionerReference)
}

@Test
fun getReferencesForLocalChanges_should_return_all_changes(): Unit = runBlocking {
listOf(
Observation().apply {
id = "1"
subject = Reference("Patient/1")
encounter = Reference("Encounter/1")
},
Observation().apply {
id = "2"
subject = Reference("Patient/2")
encounter = Reference("Encounter/2")
},
)
.forEach {
localChangeDao.addInsert(
it,
UUID.randomUUID(),
Instant.now(),
)
}

// we need LocalChangeEntity.id (auto generated) to call getReferencesForLocalChanges api
val localChangesMap = localChangeDao.getAllLocalChanges().associateBy { it.resourceId }

// Calling the api for single local change
var result = localChangeDao.getReferencesForLocalChanges(listOf(localChangesMap["1"]!!.id))
assertThat(result).hasSize(2)
assertThat(result.map { it.resourceReferenceValue }).containsExactly("Patient/1", "Encounter/1")

// Calling the api for multiple local changes
result =
localChangeDao.getReferencesForLocalChanges(
listOf(localChangesMap["1"]!!.id, localChangesMap["2"]!!.id),
)
assertThat(result).hasSize(4)
assertThat(result.map { it.resourceReferenceValue })
.containsExactly("Patient/1", "Encounter/1", "Patient/2", "Encounter/2")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ internal abstract class LocalChangeDao {
"""
SELECT *
FROM LocalChangeResourceReferenceEntity
WHERE localChangeId = (:localChangeId)
WHERE localChangeId IN (:localChangeId)
""",
)
abstract suspend fun getReferencesForLocalChanges(
Expand Down

0 comments on commit bb19c19

Please sign in to comment.