Skip to content

Commit

Permalink
Update OCKDSTU2ScheduleCoder.swift (#629)
Browse files Browse the repository at this point in the history
* Update OCKDSTU2ScheduleCoder.swift

Additional check when DateComponent is 0 and nil

* OCKSchedule description of the time the element represents into 'Timing' 'when' variable during convert

* Revert "OCKSchedule description of the time the element represents into 'Timing' 'when' variable during convert"

This reverts commit 7bdf4fa.

* Tests added

added two tests to check for zero and nil DateComponents passing through Timing convert

* Update DSTU2MedicationOrderConverterTests.swift

changed to suggested XCTAssertNotNil method
  • Loading branch information
magray authored Oct 12, 2021
1 parent 9903b01 commit c9682a7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "FHIRModels",
"repositoryURL": "https://github.com/apple/FHIRModels.git",
"state": {
"branch": null,
"revision": "c91e5bb74397136f79656bebdfda76a523d3e88c",
"version": "0.3.2"
}
}
]
},
"version": 1
}
4 changes: 2 additions & 2 deletions CareKitFHIR/CareKitFHIR/Tasks/OCKDSTU2ScheduleCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct OCKDSTU2ScheduleCoder: OCKFHIRResourceCoder {
var nonZeroComponentCount = 0
let keypaths: [KeyPath<DateComponents, Int?>] = [\.second, \.minute, \.hour, \.day, \.weekOfYear, \.month, \.year]
for path in keypaths {
if element.interval[keyPath: path] != nil {
if element.interval[keyPath: path] != nil && element.interval[keyPath: path] != 0 {
nonZeroComponentCount += 1
}
}
Expand All @@ -102,7 +102,7 @@ struct OCKDSTU2ScheduleCoder: OCKFHIRResourceCoder {
}

// Reject components not support by FHIR.
if element.interval.year != nil {
if element.interval.year != nil && element.interval.year != 0 {
throw OCKFHIRCodingError.unrepresentableContent("OCKScheduleElements with an interval in units of years are not supported in FHIR.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,24 @@ class DSTU2MedicationOrderConverterTest: XCTestCase {
XCTAssert(period.start?.value?.foundationDate == startDate)
XCTAssert(period.end?.value?.foundationDate == endDate)
}

func testOCKScheduleWithDateComponentsNilToTiming() throws {
let start = Date()
let interval = DateComponents(year: nil, month: nil, day: 1, hour: nil, minute: nil, second: nil)
let element = OCKScheduleElement(start: start, end: nil, interval: interval)
let schedule = OCKSchedule(composing: [element])

let timing = try OCKDSTU2ScheduleCoder().convert(entity: schedule)
XCTAssertNotNil(timing.repeat)
}

func testOCKScheduleWithDateComponentsZeroToTiming() throws {
let start = Date()
let interval = DateComponents(year: 0, month: 0, day: 1, hour: 0, minute: 0, second: 0)
let element = OCKScheduleElement(start: start, end: nil, interval: interval)
let schedule = OCKSchedule(composing: [element])

let timing = try OCKDSTU2ScheduleCoder().convert(entity: schedule)
XCTAssertNotNil(timing.repeat)
}
}

0 comments on commit c9682a7

Please sign in to comment.