Skip to content

Commit

Permalink
Fixing decoding times if times was never encoded error (#12)
Browse files Browse the repository at this point in the history
# [*Fixing Decoding Times
Error*](#12 (comment))

## ⚙️ Release Notes 
- This PR changes the decoding schedule, so it only decodes the times
field if it is present. This is necessary because times is not encoded
if it was empty, so then it errors when trying to decode it if it was
never encoded.

## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
apgupta3303 authored Mar 13, 2024
1 parent f5e284d commit 03d82c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/SpeziMedication/Models/Schedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct Schedule: Codable, Equatable, Hashable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.frequency = try container.decode(Frequency.self, forKey: .frequency)
self.times = try container.decode([ScheduledTime].self, forKey: .times)
self.times = try container.decodeIfPresent([ScheduledTime].self, forKey: .times) ?? []
self.startDate = try container.decode(Date.self, forKey: .startDate)
}

Expand Down
53 changes: 53 additions & 0 deletions Tests/SpeziMedicationTests/SpeziMedicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,59 @@ import XCTest


final class SpeziMedicationTests: XCTestCase {
func testScheduleDecodingWithAndWithoutTimes() throws {
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .iso8601

// Test decoding without times
let noTimesJSON = """
{
"frequency": {
"asNeeded": true
},
"startDate": "2023-12-07T08:00:00Z"
}
"""
guard let data = noTimesJSON.data(using: .utf8) else {
XCTFail("Failed to encode JSON string to Data")
return
}
let noTimesSchedule = try jsonDecoder.decode(Schedule.self, from: data)
XCTAssertEqual(noTimesSchedule.times, [])

// Test decoding with times
let withTimesJSON = """
{
"frequency": {
"regularDayIntervals": 2
},
"startDate": "2023-12-07T08:00:00Z",
"times": [
{
"time": {
"hour": 8,
"minute": 0
},
"dosage": 5.0
},
{
"time": {
"hour": 15,
"minute": 0
},
"dosage": 5.0
}
]
}
"""
guard let dataWithTimes = withTimesJSON.data(using: .utf8) else {
XCTFail("Failed to encode JSON string to Data")
return
}
let withTimesSchedule = try jsonDecoder.decode(Schedule.self, from: dataWithTimes)
XCTAssertNotNil(withTimesSchedule.times)
}

// swiftlint:disable:next function_body_length
func testScheduleEncoding() throws {
let jsonEncoder = JSONEncoder()
Expand Down

0 comments on commit 03d82c2

Please sign in to comment.