From 1c1e9e04b6bb9442f9b28727db5f26ced9dcebd2 Mon Sep 17 00:00:00 2001 From: jdisho Date: Thu, 2 Jan 2025 13:17:06 +0100 Subject: [PATCH] Write tests for `ResourceProxy+DisplayName` --- Tests/SpeziFHIRTests/FHIRResourceTests.swift | 153 ++++++++++++++++--- 1 file changed, 135 insertions(+), 18 deletions(-) diff --git a/Tests/SpeziFHIRTests/FHIRResourceTests.swift b/Tests/SpeziFHIRTests/FHIRResourceTests.swift index 4340820..aab092b 100644 --- a/Tests/SpeziFHIRTests/FHIRResourceTests.swift +++ b/Tests/SpeziFHIRTests/FHIRResourceTests.swift @@ -6,8 +6,8 @@ // SPDX-License-Identifier: MIT // -import ModelsR4 import ModelsDSTU2 +import ModelsR4 @testable import SpeziFHIR import XCTest @@ -15,15 +15,15 @@ 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") @@ -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"), @@ -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), @@ -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) @@ -135,18 +135,18 @@ 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) @@ -154,12 +154,12 @@ final class FHIRResourceTests: XCTestCase { 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 ")) @@ -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" @@ -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 {