-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphase07_review_test.go
More file actions
340 lines (307 loc) · 8.93 KB
/
phase07_review_test.go
File metadata and controls
340 lines (307 loc) · 8.93 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package gin
import (
"encoding/json"
stderrors "errors"
"math"
"reflect"
"strconv"
"strings"
"testing"
)
func TestWalkJSONPropagatesStagingErrors(t *testing.T) {
builder := mustNewBuilder(t, DefaultConfig(), 1)
err := builder.walkJSON("$.score", complex(1, 2), 0)
if err == nil {
t.Fatal("walkJSON() error = nil, want unsupported type error")
}
if !strings.Contains(err.Error(), "unsupported transformed value type") {
t.Fatalf("walkJSON() error = %v, want unsupported transformed value type", err)
}
if got := builder.Finalize().PathDirectory; len(got) != 0 {
t.Fatalf("walkJSON() merged rejected path state: PathDirectory len = %d, want 0", len(got))
}
}
func TestWalkJSONRecoversMergePanic(t *testing.T) {
builder := mustNewBuilder(t, DefaultConfig(), 1)
builder.testHooks.mergeStagedPathsPanicHook = func() { panic("simulated walkJSON merge panic") }
err := builder.walkJSON("$.name", "alice", 0)
if err == nil {
t.Fatal("walkJSON() error = nil, want recovered merge panic")
}
if !strings.Contains(err.Error(), "builder tragic: recovered panic in merge") {
t.Fatalf("walkJSON() error = %q, want recovered merge panic context", err.Error())
}
if builder.tragicErr == nil {
t.Fatal("builder.tragicErr = nil, want recovered merge panic error")
}
builder.testHooks.mergeStagedPathsPanicHook = nil
err = builder.walkJSON("$.name", "bob", 0)
if err == nil {
t.Fatal("walkJSON() after tragedy = nil, want refusal")
}
if !strings.Contains(err.Error(), "builder closed by prior tragic failure") {
t.Fatalf("walkJSON() after tragedy = %q, want tragic closure context", err.Error())
}
}
func TestSortedObjectKeys(t *testing.T) {
got := sortedObjectKeys(map[string]any{
"z": 1,
"a": 2,
"m": 3,
})
want := []string{"a", "m", "z"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("sortedObjectKeys() = %v, want %v", got, want)
}
}
func TestAddDocumentReportsLexicographicallyFirstObjectFieldError(t *testing.T) {
cfg, err := NewConfig(
WithCustomTransformer("$.a", "invalid", func(value any) (any, bool) { return complex(1, 0), true }),
WithCustomTransformer("$.z", "invalid", func(value any) (any, bool) { return complex(1, 0), true }),
)
if err != nil {
t.Fatalf("NewConfig() error = %v", err)
}
builder := mustNewBuilder(t, cfg, 1)
err = builder.AddDocument(0, []byte(`{"z":"bad","a":"bad"}`))
if err == nil {
t.Fatal("AddDocument() error = nil, want staged field error")
}
if !strings.Contains(err.Error(), "$.a") {
t.Fatalf("AddDocument() error = %v, want $.a to fail first", err)
}
}
func TestPrepareTransformerValueRecursesThroughNestedArrays(t *testing.T) {
input := map[string]any{
"items": []any{
json.Number("1"),
map[string]any{
"values": []any{
json.Number("2.5"),
map[string]any{"deep": json.Number("3e2")},
},
},
},
}
want := map[string]any{
"items": []any{
float64(1),
map[string]any{
"values": []any{
float64(2.5),
map[string]any{"deep": float64(300)},
},
},
},
}
if got := prepareTransformerValue(input); !reflect.DeepEqual(got, want) {
t.Fatalf("prepareTransformerValue() = %#v, want %#v", got, want)
}
}
func TestParseJSONNumberLiteralEdgeCases(t *testing.T) {
tests := []struct {
name string
raw string
wantInt bool
wantI64 int64
wantF64 float64
wantErrIs error
}{
{
name: "negative zero stays integral",
raw: "-0",
wantInt: true,
wantI64: 0,
},
{
name: "scientific notation stays float",
raw: "1e2",
wantF64: 100,
},
{
name: "int overflow preserves parse error",
raw: "9223372036854775808",
wantErrIs: strconv.ErrRange,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isInt, intVal, floatVal, err := parseJSONNumberLiteral(tt.raw)
if tt.wantErrIs != nil {
if err == nil {
t.Fatalf("parseJSONNumberLiteral(%q) error = nil, want %v", tt.raw, tt.wantErrIs)
}
var numErr *strconv.NumError
if !stderrors.As(err, &numErr) {
t.Fatalf("parseJSONNumberLiteral(%q) error = %T, want *strconv.NumError", tt.raw, err)
}
if !stderrors.Is(err, tt.wantErrIs) {
t.Fatalf("parseJSONNumberLiteral(%q) error = %v, want errors.Is(..., %v)", tt.raw, err, tt.wantErrIs)
}
return
}
if err != nil {
t.Fatalf("parseJSONNumberLiteral(%q) error = %v", tt.raw, err)
}
if isInt != tt.wantInt {
t.Fatalf("parseJSONNumberLiteral(%q) isInt = %v, want %v", tt.raw, isInt, tt.wantInt)
}
if intVal != tt.wantI64 {
t.Fatalf("parseJSONNumberLiteral(%q) intVal = %d, want %d", tt.raw, intVal, tt.wantI64)
}
if floatVal != tt.wantF64 {
t.Fatalf("parseJSONNumberLiteral(%q) floatVal = %v, want %v", tt.raw, floatVal, tt.wantF64)
}
})
}
}
func TestMixedNumericPathPromotesSafelyAcrossRowGroups(t *testing.T) {
builder := mustNewBuilder(t, DefaultConfig(), 4)
docs := []struct {
docID DocID
json string
}{
{0, `{"score":10}`},
{1, `{"score":20}`},
{2, `{"score":30}`},
{3, `{"score":15.5}`},
}
for _, doc := range docs {
if err := builder.AddDocument(doc.docID, []byte(doc.json)); err != nil {
t.Fatalf("AddDocument(%d) failed: %v", doc.docID, err)
}
}
idx := builder.Finalize()
scorePathID, ok := idx.pathLookup["$.score"]
if !ok {
t.Fatal("$.score missing from pathLookup")
}
ni, ok := idx.NumericIndexes[scorePathID]
if !ok {
t.Fatal("$.score missing numeric index")
}
if ni.ValueType != NumericValueTypeFloatMixed {
t.Fatalf("ValueType = %v, want %v", ni.ValueType, NumericValueTypeFloatMixed)
}
wantBounds := []struct {
min float64
max float64
}{
{min: 10, max: 10},
{min: 20, max: 20},
{min: 30, max: 30},
{min: 15.5, max: 15.5},
}
for rgID, want := range wantBounds {
stat := ni.RGStats[rgID]
if !stat.HasValue {
t.Fatalf("RGStats[%d].HasValue = false, want true", rgID)
}
if stat.Min != want.min || stat.Max != want.max {
t.Fatalf("RGStats[%d] bounds = [%v,%v], want [%v,%v]", rgID, stat.Min, stat.Max, want.min, want.max)
}
}
if got := idx.Evaluate([]Predicate{EQ("$.score", int64(20))}).ToSlice(); len(got) != 1 || got[0] != 1 {
t.Fatalf(`EQ("$.score", 20) = %v, want [1]`, got)
}
}
func TestQueryNEOnIntOnlyNumericPath(t *testing.T) {
builder := mustNewBuilder(t, DefaultConfig(), 3)
docs := []struct {
docID DocID
json string
}{
{0, `{"score":1}`},
{1, `{"score":2}`},
{2, `{"score":3}`},
}
for _, doc := range docs {
if err := builder.AddDocument(doc.docID, []byte(doc.json)); err != nil {
t.Fatalf("AddDocument(%d) failed: %v", doc.docID, err)
}
}
idx := builder.Finalize()
if got := idx.Evaluate([]Predicate{NE("$.score", int64(2))}).ToSlice(); len(got) != 2 || got[0] != 0 || got[1] != 2 {
t.Fatalf(`NE("$.score", 2) = %v, want [0 2]`, got)
}
if got := idx.Evaluate([]Predicate{NE("$.score", 2.5)}).ToSlice(); len(got) != 3 || got[0] != 0 || got[1] != 1 || got[2] != 2 {
t.Fatalf(`NE("$.score", 2.5) = %v, want [0 1 2]`, got)
}
if got := idx.Evaluate([]Predicate{EQ("$.score", 2.0)}).ToSlice(); len(got) != 1 || got[0] != 1 {
t.Fatalf(`EQ("$.score", 2.0) = %v, want [1]`, got)
}
if got := idx.Evaluate([]Predicate{EQ("$.score", 2.5)}).ToSlice(); len(got) != 0 {
t.Fatalf(`EQ("$.score", 2.5) = %v, want []`, got)
}
}
func TestAddDocumentRejectsTrailingJSONContent(t *testing.T) {
builder := mustNewBuilder(t, DefaultConfig(), 1)
err := builder.AddDocument(0, []byte(`{"score":1} {"extra":2}`))
if err == nil {
t.Fatal("AddDocument() error = nil, want trailing JSON error")
}
if !strings.Contains(err.Error(), "unexpected trailing JSON content") {
t.Fatalf("AddDocument() error = %v, want trailing JSON error", err)
}
if builder.numDocs != 0 {
t.Fatalf("numDocs = %d, want 0", builder.numDocs)
}
if got := builder.Finalize().PathDirectory; len(got) != 0 {
t.Fatalf("AddDocument() merged rejected trailing JSON: PathDirectory len = %d, want 0", len(got))
}
}
func TestToRoundedInt64EdgeCases(t *testing.T) {
tests := []struct {
name string
value any
round func(float64) float64
want int64
ok bool
}{
{
name: "nan rejected",
value: math.NaN(),
round: math.Floor,
ok: false,
},
{
name: "positive infinity rejected",
value: math.Inf(1),
round: math.Floor,
ok: false,
},
{
name: "negative fraction floors away from zero",
value: -1.2,
round: math.Floor,
want: -2,
ok: true,
},
{
name: "negative fraction ceils toward zero",
value: -1.2,
round: math.Ceil,
want: -1,
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := toRoundedInt64(tt.value, tt.round)
if ok != tt.ok {
t.Fatalf("toRoundedInt64(%v) ok = %v, want %v", tt.value, ok, tt.ok)
}
if got != tt.want {
t.Fatalf("toRoundedInt64(%v) = %d, want %d", tt.value, got, tt.want)
}
})
}
}
func TestMustNewTrigramIndexPanicsOnInvalidOption(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("MustNewTrigramIndex() did not panic on invalid option")
}
}()
_ = MustNewTrigramIndex(1, WithN(1))
}