|
| 1 | +package fhirjson_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + codes_go_proto "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/codes_go_proto" |
| 9 | + dtpb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/datatypes_go_proto" |
| 10 | + opb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/observation_go_proto" |
| 11 | + ppb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/patient_go_proto" |
| 12 | + |
| 13 | + "github.com/verily-src/fhirpath-go/fhirpath/fhirjson" |
| 14 | + "github.com/verily-src/fhirpath-go/internal/fhir" |
| 15 | + |
| 16 | + "github.com/google/go-cmp/cmp" |
| 17 | + "google.golang.org/protobuf/testing/protocmp" |
| 18 | +) |
| 19 | + |
| 20 | +func TestDecodeNew_Success(t *testing.T) { |
| 21 | + testCases := []struct { |
| 22 | + name string |
| 23 | + input string |
| 24 | + want fhir.Resource |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "Patient resource", |
| 28 | + input: `{"resourceType": "Patient", "id": "p1"}`, |
| 29 | + want: &ppb.Patient{ |
| 30 | + Id: &dtpb.Id{Value: "p1"}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Minimal Patient", |
| 35 | + input: `{"resourceType": "Patient"}`, |
| 36 | + want: &ppb.Patient{}, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + for _, tc := range testCases { |
| 41 | + t.Run(tc.name, func(t *testing.T) { |
| 42 | + dec := fhirjson.NewDecoder(bytes.NewReader([]byte(tc.input))) |
| 43 | + |
| 44 | + res, err := dec.DecodeNew() |
| 45 | + if err != nil { |
| 46 | + t.Errorf("DecodeNew error: got %v, want nil", err) |
| 47 | + } |
| 48 | + |
| 49 | + if got, want := res, tc.want; !cmp.Equal(got, want, protocmp.Transform()) { |
| 50 | + t.Errorf("DecodeNew resource: got %v, want %v", got, want) |
| 51 | + } |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestDecodeNew_Error(t *testing.T) { |
| 57 | + testCases := []struct { |
| 58 | + name string |
| 59 | + input string |
| 60 | + }{ |
| 61 | + { |
| 62 | + name: "Invalid JSON", |
| 63 | + input: `{"resourceType": "Patient",`, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Empty input", |
| 67 | + input: ``, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tc := range testCases { |
| 72 | + t.Run(tc.name, func(t *testing.T) { |
| 73 | + dec := fhirjson.NewDecoder(bytes.NewReader([]byte(tc.input))) |
| 74 | + |
| 75 | + res, err := dec.DecodeNew() |
| 76 | + if err == nil { |
| 77 | + t.Errorf("DecodeNew: expected error, got nil") |
| 78 | + } |
| 79 | + |
| 80 | + if res != nil { |
| 81 | + t.Errorf("DecodeNew: expected nil resource, got %v", res) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestDecoderTimeZone(t *testing.T) { |
| 88 | + testCases := []struct { |
| 89 | + name string |
| 90 | + json string |
| 91 | + zone *time.Location |
| 92 | + want fhir.Resource |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "Not setting timezone implies Local", |
| 96 | + json: `{ |
| 97 | + "resourceType": "Patient", |
| 98 | + "id": "example-patient", |
| 99 | + "birthDate": "1990-05-27" |
| 100 | + }`, |
| 101 | + zone: nil, |
| 102 | + want: &ppb.Patient{ |
| 103 | + Id: &dtpb.Id{Value: "example-patient"}, |
| 104 | + BirthDate: &dtpb.Date{ |
| 105 | + ValueUs: time.Date(1990, 5, 27, 0, 0, 0, 0, time.UTC).UnixMicro(), |
| 106 | + Timezone: "Local", |
| 107 | + Precision: dtpb.Date_DAY, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "Set timezone to UTC", |
| 113 | + json: `{ |
| 114 | + "resourceType": "Patient", |
| 115 | + "id": "example-patient", |
| 116 | + "birthDate": "1990-05-27" |
| 117 | + }`, |
| 118 | + zone: time.UTC, |
| 119 | + want: &ppb.Patient{ |
| 120 | + Id: &dtpb.Id{Value: "example-patient"}, |
| 121 | + BirthDate: &dtpb.Date{ |
| 122 | + ValueUs: time.Date(1990, 5, 27, 0, 0, 0, 0, time.UTC).UnixMicro(), |
| 123 | + Timezone: "UTC", |
| 124 | + Precision: dtpb.Date_DAY, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "Timezone does not apply to DateTime", |
| 130 | + json: `{ |
| 131 | + "resourceType": "Observation", |
| 132 | + "id": "example-observation", |
| 133 | + "status": "final", |
| 134 | + "code": { "coding": [ { "system": "http://loinc.org", "code": "1234-5" } ] }, |
| 135 | + "effectiveDateTime": "2022-01-02T03:04:05Z" |
| 136 | + }`, |
| 137 | + zone: time.UTC, |
| 138 | + want: &opb.Observation{ |
| 139 | + Id: &dtpb.Id{Value: "example-observation"}, |
| 140 | + Status: &opb.Observation_StatusCode{Value: codes_go_proto.ObservationStatusCode_FINAL}, |
| 141 | + Code: &dtpb.CodeableConcept{ |
| 142 | + Coding: []*dtpb.Coding{ |
| 143 | + { |
| 144 | + System: &dtpb.Uri{Value: "http://loinc.org"}, |
| 145 | + Code: &dtpb.Code{Value: "1234-5"}, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + Effective: &opb.Observation_EffectiveX{ |
| 150 | + Choice: &opb.Observation_EffectiveX_DateTime{ |
| 151 | + DateTime: &dtpb.DateTime{ |
| 152 | + ValueUs: time.Date(2022, 1, 2, 3, 4, 5, 0, time.UTC).UnixMicro(), |
| 153 | + Timezone: "Z", |
| 154 | + Precision: dtpb.DateTime_SECOND, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + } |
| 161 | + |
| 162 | + for _, tc := range testCases { |
| 163 | + t.Run(tc.name, func(t *testing.T) { |
| 164 | + dec := fhirjson.NewDecoder(bytes.NewReader([]byte(tc.json))) |
| 165 | + if tc.zone != nil { |
| 166 | + dec.TimeZone(tc.zone) |
| 167 | + } |
| 168 | + |
| 169 | + var got fhir.Resource |
| 170 | + switch tc.want.(type) { |
| 171 | + case *ppb.Patient: |
| 172 | + got = &ppb.Patient{} |
| 173 | + case *opb.Observation: |
| 174 | + got = &opb.Observation{} |
| 175 | + default: |
| 176 | + t.Fatalf("unsupported resource type: %T", tc.want) |
| 177 | + } |
| 178 | + |
| 179 | + err := dec.Decode(got) |
| 180 | + if err != nil { |
| 181 | + t.Errorf("Decode error: got %v, want nil", err) |
| 182 | + } |
| 183 | + |
| 184 | + if got, want := got, tc.want; !cmp.Equal(got, want, protocmp.Transform()) { |
| 185 | + t.Errorf("Decode resource diff: %s", cmp.Diff(got, want, protocmp.Transform())) |
| 186 | + } |
| 187 | + }) |
| 188 | + } |
| 189 | +} |
0 commit comments