-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoperator_datetime_test.go
235 lines (229 loc) · 7.33 KB
/
operator_datetime_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package interpreter
import (
"strings"
"testing"
"time"
"github.com/google/cql/model"
"github.com/google/cql/result"
)
func TestCompareWithPrecision(t *testing.T) {
tests := []struct {
name string
l result.DateTime
r result.DateTime
precision model.DateTimePrecision
want comparison
}{
{
name: "same dates with year precision returns 0",
l: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
precision: model.YEAR,
want: leftEqualRight,
},
{
name: "two different dates with same year and year precision returns 0",
l: result.DateTime{
Date: time.Date(2024, time.February, 5, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 22, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
precision: model.YEAR,
want: leftEqualRight,
},
{
name: "same dates with month precision returns 0",
precision: model.MONTH,
l: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
want: leftEqualRight,
},
{
name: "left month is lower with month precision returns -1",
precision: model.MONTH,
l: result.DateTime{
Date: time.Date(2024, time.February, 22, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 22, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
want: leftBeforeRight,
},
{
name: "left month is higher with month precision returns 1",
precision: model.MONTH,
l: result.DateTime{
Date: time.Date(2024, time.September, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
want: leftAfterRight,
},
{
name: "left year is greater but same month at month precision returns 1",
precision: model.MONTH,
l: result.DateTime{
Date: time.Date(2030, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
want: leftAfterRight,
},
{
name: "dates are the same, l has higher precision and should return precision error",
precision: model.DAY,
l: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.MONTH,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
want: insufficientPrecision,
},
{
name: "leftAfterRight: unset precision uses finest precision of l and r (model.DAY)",
precision: model.UNSETDATETIMEPRECISION,
l: result.DateTime{
Date: time.Date(2024, time.April, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.MONTH,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.DAY,
},
want: leftAfterRight,
},
{
name: "leftEqualRight: unset precision uses finest precision of l and r (model.DAY)",
precision: model.UNSETDATETIMEPRECISION,
l: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.DAY,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.DAY,
},
want: leftEqualRight,
},
{
name: "leftBeforeRight: unset precision uses finest precision of l and r (model.DAY)",
precision: model.UNSETDATETIMEPRECISION,
l: result.DateTime{
Date: time.Date(2024, time.January, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.DAY,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.MONTH,
},
want: leftBeforeRight,
},
{
name: "insufficientPrecision: unset precision uses finest precision of l and r (model.DAY)",
precision: model.UNSETDATETIMEPRECISION,
l: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.MONTH,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.DAY,
},
want: insufficientPrecision,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := compareDateTimeWithPrecision(test.l, test.r, test.precision)
if err != nil {
t.Errorf("compareDateTimeWithPrecision(%v, %v, %v) returned unexpected error: %v", test.l, test.r, test.precision, err)
}
if got != test.want {
t.Errorf("compareDateTimeWithPrecision(%v, %v, %v) = %v, want %v", test.l, test.r, test.precision, got, test.want)
}
})
}
}
func TestCompareDateTimeWithPrecision_Errors(t *testing.T) {
tests := []struct {
name string
l result.DateTime
r result.DateTime
precision model.DateTimePrecision
wantErrContains string
}{
{
name: "unset precision on l DateTimeValue",
l: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.UNSETDATETIMEPRECISION,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
precision: model.UNSETDATETIMEPRECISION,
wantErrContains: "internal error -- input to getFinestPrecision must not be unset.",
},
{
name: "unset precision on r DateTimeValue",
l: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.SECOND,
},
r: result.DateTime{
Date: time.Date(2024, time.March, 31, 1, 20, 30, 1e8, time.UTC),
Precision: model.UNSETDATETIMEPRECISION,
},
precision: model.UNSETDATETIMEPRECISION,
wantErrContains: "internal error -- input to getFinestPrecision must not be unset.",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := compareDateTimeWithPrecision(test.l, test.r, test.precision)
if !strings.Contains(err.Error(), test.wantErrContains) {
t.Errorf("compareDateTimeWithPrecision(%v, %v, %v) returned unexpected error: got: %v, want contains: %v", test.l, test.r, test.precision, err, test.wantErrContains)
}
})
}
}