Skip to content

Commit

Permalink
Write tests for ResourceProxy+DisplayName
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisho committed Jan 2, 2025
1 parent dc70d44 commit 1c1e9e0
Showing 1 changed file with 135 additions and 18 deletions.
153 changes: 135 additions & 18 deletions Tests/SpeziFHIRTests/FHIRResourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
// SPDX-License-Identifier: MIT
//

import ModelsR4
import ModelsDSTU2
import ModelsR4
@testable import SpeziFHIR
import XCTest

final class FHIRResourceTests: XCTestCase {

let testDate = Date(timeIntervalSince1970: 1735123266)
let calendar = Calendar.current

func testModelsR4ResourceInitialization() throws {
let mockObservation = try ModelsR4Mocks.createObservation(date: testDate)

let resource = FHIRResource(
resource: mockObservation,
displayName: "Test Observation"
)

XCTAssertEqual(resource.id, "observation-id")
XCTAssertEqual(resource.displayName, "Test Observation")
XCTAssertEqual(resource.resourceType, "Observation")
Expand All @@ -41,7 +41,7 @@ final class FHIRResourceTests: XCTestCase {
XCTAssertEqual(resource.displayName, "Test Observation")
XCTAssertEqual(resource.resourceType, "Observation")
}

func testModelsR4ResourceDates() throws {
let modelsR4Resources: [(ModelsR4.Resource, String)] = try [
(ModelsR4Mocks.createCarePlan(date: testDate), "CarePlan"),
Expand All @@ -62,7 +62,7 @@ final class FHIRResourceTests: XCTestCase {
(ModelsR4Mocks.createProvenance(date: testDate), "Provenance"),
(ModelsR4Mocks.createSupplyDelivery(date: testDate), "SupplyDelivery")
]

for (resource, name) in modelsR4Resources {
let fhirResource = FHIRResource(
versionedResource: .r4(resource),
Expand Down Expand Up @@ -108,18 +108,18 @@ final class FHIRResourceTests: XCTestCase {
id: "test-id".asFHIRStringPrimitive(),
status: FHIRPrimitive(.final)
)

let resource = FHIRResource(
versionedResource: .r4(observation),
displayName: "Test"
)

let jsonString = resource.json(withConfiguration: [])

let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let decodedObservation = try decoder.decode(ModelsR4.Observation.self, from: jsonData)

XCTAssertEqual(decodedObservation.id, observation.id)
XCTAssertEqual(decodedObservation.code, observation.code)
XCTAssertEqual(decodedObservation.status, observation.status)
Expand All @@ -135,31 +135,31 @@ final class FHIRResourceTests: XCTestCase {
id: "test-id".asFHIRStringPrimitive(),
status: FHIRPrimitive(.final)
)

let resource = FHIRResource(
versionedResource: .dstu2(observation),
displayName: "Test"
)

let jsonString = resource.json(withConfiguration: [])

let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let decodedObservation = try decoder.decode(ModelsDSTU2.Observation.self, from: jsonData)

XCTAssertEqual(decodedObservation.id, observation.id)
XCTAssertEqual(decodedObservation.code, observation.code)
XCTAssertEqual(decodedObservation.status, observation.status)
}

func testMatchesDisplayName() throws {
let observation = try ModelsR4Mocks.createObservation(date: testDate)

let resource = FHIRResource(
versionedResource: .r4(observation),
displayName: "Test Resource"
)

XCTAssertTrue(resource.matchesDisplayName(with: "test"))
XCTAssertTrue(resource.matchesDisplayName(with: "resource"))
XCTAssertTrue(resource.matchesDisplayName(with: " test "))
Expand All @@ -172,7 +172,7 @@ final class FHIRResourceTests: XCTestCase {
let observation = try ModelsR4Mocks.createObservation(date: testDate)
let patient = try ModelsR4Mocks.createPatient(date: testDate)
let medicationRequest = try ModelsR4Mocks.createMedicationRequest(date: testDate)

let resource1 = FHIRResource(
versionedResource: .r4(observation),
displayName: "Test Resource1"
Expand All @@ -189,12 +189,129 @@ final class FHIRResourceTests: XCTestCase {
)

let resources = [resource1, resource2, resource3]

XCTAssertEqual(resources.filterByDisplayName(with: "test").count, 3)
XCTAssertEqual(resources.filterByDisplayName(with: "resource1").count, 1)
XCTAssertEqual(resources.filterByDisplayName(with: "xyz").count, 0)
XCTAssertEqual(resources.filterByDisplayName(with: "").count, 3)
}

func testConditionDisplayName() throws {
let mockCondition = try ModelsR4Mocks.createCondition(date: testDate)

// Test with text value
mockCondition.code = CodeableConcept(text: "Hypertension")
let proxy = ResourceProxy(with: mockCondition)
XCTAssertEqual(proxy.displayName, "Hypertension")

// Test with no code
mockCondition.code = nil
XCTAssertEqual(proxy.displayName, "Condition")
}

func testDiagnosticReportDisplayName() throws {
let mockReport = try ModelsR4Mocks.createDiagnosticReport(date: testDate)

// Test with display coding
mockReport.code.coding = [Coding(display: "Blood Test")]
let proxy = ResourceProxy(with: mockReport)
XCTAssertEqual(proxy.displayName, "Blood Test")

// Test with no codings
mockReport.code.coding?.removeAll()
XCTAssertEqual(proxy.displayName, "DiagnosticReport")
}

func testEncounterDisplayName() throws {
let mockEncounter = try ModelsR4Mocks.createEncounter(date: testDate)

// Test with reason code
mockEncounter.reasonCode = [CodeableConcept(coding: [Coding(display: "Follow-up")])]
let proxy = ResourceProxy(with: mockEncounter)
XCTAssertEqual(proxy.displayName, "Follow-up")

// Test with encounter type
mockEncounter.reasonCode = nil
mockEncounter.type = [CodeableConcept(coding: [Coding(display: "Office Visit")])]
XCTAssertEqual(proxy.displayName, "Office Visit")

// Test with no type or reason
mockEncounter.type = nil
XCTAssertEqual(proxy.displayName, "Encounter")
}

func testImmunizationDisplayName() throws {
let mockImmunization = try ModelsR4Mocks.createImmunization(date: testDate)

// Test with vaccine text
mockImmunization.vaccineCode.text = "Flu Shot"
let proxy = ResourceProxy(with: mockImmunization)
XCTAssertEqual(proxy.displayName, "Flu Shot")

// Test with no vaccine text
mockImmunization.vaccineCode.text = nil
XCTAssertEqual(proxy.displayName, "Immunization")
}

func testMedicationRequestDisplayName() throws {
let mockMedRequest = try ModelsR4Mocks.createMedicationRequest(date: testDate)

// Test with codeable concept text
if case let .codeableConcept(medicationCode) = mockMedRequest.medication {
medicationCode.text = "Aspirin"
}
let proxy = ResourceProxy(with: mockMedRequest)
XCTAssertEqual(proxy.displayName, "Aspirin")

// Test with no text in codeable concept
if case let .codeableConcept(medicationCode) = mockMedRequest.medication {
medicationCode.text = nil
}
XCTAssertEqual(proxy.displayName, "MedicationRequest")

// Test with reference instead of codeable concept
mockMedRequest.medication = .reference(Reference())
XCTAssertEqual(proxy.displayName, "MedicationRequest")
}

func testObservationDisplayName() throws {
let mockObservation = try ModelsR4Mocks.createObservation(date: testDate)

// Test with code text
mockObservation.code.text = "Blood Pressure"
let proxy = ResourceProxy(with: mockObservation)
XCTAssertEqual(proxy.displayName, "Blood Pressure")

// Test with no code text
mockObservation.code.text = nil
XCTAssertEqual(proxy.displayName, "Observation")
}

func testProcedureDisplayName() throws {
let mockProcedure = try ModelsR4Mocks.createProcedure(date: testDate)

// Test with code text
mockProcedure.code = CodeableConcept(text: "Hip Surgery")
let proxy = ResourceProxy(with: mockProcedure)
XCTAssertEqual(proxy.displayName, "Hip Surgery")

// Test with no code
mockProcedure.code = nil
XCTAssertEqual(proxy.displayName, "Procedure")
}

func testPatientDisplayName() throws {
let mockPatient = try ModelsR4Mocks.createPatient(date: testDate)

// Test with name components
mockPatient.name = [HumanName(family: "Doe", given: ["John"])]
let proxy = ResourceProxy(with: mockPatient)
XCTAssertEqual(proxy.displayName, "JohnDoe")

// Test with no name
mockPatient.name = nil
XCTAssertEqual(proxy.displayName, "Patient")
}
}

private extension FHIRResourceTests {
Expand Down

0 comments on commit 1c1e9e0

Please sign in to comment.