@@ -29,122 +29,6 @@ import (
2929 "google.golang.org/protobuf/testing/protocmp"
3030)
3131
32- func TestCalculateAge (t * testing.T ) {
33- tests := []struct {
34- name string
35- cql string
36- wantModel model.IExpression
37- wantResult result.Value
38- }{
39- // Age calculation functions - Note that these use the default evaluation timestamp
40- {
41- name : "AgeInYears Date" ,
42- cql : "AgeInYears()" ,
43- wantModel : & model.CalculateAge {
44- Precision : model .YEAR ,
45- UnaryExpression : & model.UnaryExpression {
46- Expression : model .ResultType (types .Integer ),
47- Operand : & model.ToDateTime {
48- UnaryExpression : & model.UnaryExpression {
49- Expression : model .ResultType (types .DateTime ),
50- Operand : & model.Property {
51- Expression : model .ResultType (types .Date ),
52- Source : & model.Property {
53- Expression : model .ResultType (& types.Named {TypeName : "FHIR.date" }),
54- Source : & model.ExpressionRef {
55- Expression : model .ResultType (& types.Named {TypeName : "FHIR.Patient" }),
56- Name : "Patient" ,
57- },
58- Path : "birthDate" ,
59- },
60- Path : "value" ,
61- },
62- },
63- },
64- },
65- },
66- wantResult : newOrFatal (t , 75 ), // The test patient birthday is 1950-01-01
67- },
68- {
69- name : "AgeInMonths Date" ,
70- cql : "AgeInMonths()" ,
71- wantResult : newOrFatal (t , 903 ), // 75 years * 12 months/year, approximately
72- },
73- {
74- name : "AgeInWeeks Date" ,
75- cql : "AgeInWeeks()" ,
76- wantResult : newOrFatal (t , 3928 ), // 75 years * 52 weeks/year, approximately
77- },
78- {
79- name : "AgeInDays Date" ,
80- cql : "AgeInDays()" ,
81- wantResult : newOrFatal (t , 27500 ), // 75 years * 365.25 days/year, approximately
82- },
83- {
84- name : "AgeInHours Date" ,
85- cql : "AgeInHours()" ,
86- wantResult : newOrFatal (t , 660000 ), // 75 years * 365.25 days/year * 24 hours/day, approximately
87- },
88- {
89- name : "AgeInMinutes Date" ,
90- cql : "AgeInMinutes()" ,
91- wantResult : newOrFatal (t , 39600000 ), // 75 years * 365.25 days/year * 24 hours/day * 60 minutes/hour, approximately
92- },
93- {
94- name : "AgeInSeconds Date" ,
95- cql : "AgeInSeconds()" ,
96- wantResult : newOrFatal (t , 2376000000 ), // 75 years * 365.25 days/year * 24 hours/day * 60 minutes/hour * 60 seconds/minute, approximately
97- },
98- // Custom birth date tests (using the evaluation time of 2025-04-16)
99- {
100- name : "AgeInYears with explicit Date" ,
101- cql : "CalculateAgeInYears(@1981-06-15)" ,
102- wantResult : newOrFatal (t , 43 ), // Age from 1981-06-15 to test time (2025-04-16)
103- },
104- {
105- name : "AgeInMonths with explicit Date" ,
106- cql : "CalculateAgeInMonths(@2022-06-15)" ,
107- wantResult : newOrFatal (t , 34 ), // Age from 2022-06-15 to test time (2025-04-16)
108- },
109- {
110- name : "AgeInDays with explicit DateTime" ,
111- cql : "CalculateAgeInDays(@2023-06-15T10:01:01.000Z)" ,
112- wantResult : newOrFatal (t , 671 ), // Days from 2023-06-15 to test time (2025-04-16)
113- },
114- {
115- name : "AgeInHours with explicit DateTime" ,
116- cql : "CalculateAgeInHours(@2025-04-15T10:01:01.000Z)" ,
117- wantResult : newOrFatal (t , 24 ), // Hours from 2025-04-15 to test time (2025-04-16)
118- },
119- // Null input
120- {
121- name : "CalculateAgeInYears with null" ,
122- cql : "CalculateAgeInYears(null as Date)" ,
123- wantResult : newOrFatal (t , nil ),
124- },
125- }
126-
127- for _ , tc := range tests {
128- t .Run (tc .name , func (t * testing.T ) {
129- p := newFHIRParser (t )
130- parsedLibs , err := p .Libraries (context .Background (), wrapInLib (t , tc .cql ), parser.Config {})
131- if err != nil {
132- t .Fatalf ("Parse returned unexpected error: %v" , err )
133- }
134- if diff := cmp .Diff (tc .wantModel , getTESTRESULTModel (t , parsedLibs )); tc .wantModel != nil && diff != "" {
135- t .Errorf ("Parse diff (-want +got):\n %s" , diff )
136- }
137-
138- results , err := interpreter .Eval (context .Background (), parsedLibs , defaultInterpreterConfig (t , p ))
139- if err != nil {
140- t .Fatalf ("Eval returned unexpected error: %v" , err )
141- }
142- if diff := cmp .Diff (tc .wantResult , getTESTRESULT (t , results ), protocmp .Transform ()); diff != "" {
143- t .Errorf ("Eval diff (-want +got)\n %v" , diff )
144- }
145- })
146- }
147- }
14832
14933func TestCalculateAgeAt (t * testing.T ) {
15034 tests := []struct {
0 commit comments