-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnested_array_error_path_regression_test.go
More file actions
98 lines (85 loc) · 2.37 KB
/
nested_array_error_path_regression_test.go
File metadata and controls
98 lines (85 loc) · 2.37 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
package jsonlogic2sql
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
func decodeLogicMapForPathTest(t *testing.T, logic string) map[string]interface{} {
t.Helper()
var m map[string]interface{}
if err := json.Unmarshal([]byte(logic), &m); err != nil {
t.Fatalf("json.Unmarshal(map) failed: %v", err)
}
return m
}
func assertNestedPathNotTruncated(t *testing.T, err error, parentOp string) {
t.Helper()
if err == nil {
t.Fatal("expected error, got nil")
}
msg := err.Error()
if !strings.Contains(msg, "oops") {
t.Fatalf("expected error to mention custom operator, got: %v", err)
}
if strings.Contains(msg, "$[1].oops") {
t.Fatalf("path is truncated (missing parent context): %v", err)
}
if !strings.Contains(msg, "$."+parentOp) {
t.Fatalf("expected parent operator path in error, got: %v", err)
}
if !strings.Contains(msg, ".map") {
t.Fatalf("expected nested map segment in error path, got: %v", err)
}
}
func TestNestedArrayCustomOperatorErrorPath_Preserved_InlineAndParam(t *testing.T) {
t.Parallel()
dialects := []Dialect{
DialectBigQuery,
DialectSpanner,
DialectPostgreSQL,
DialectDuckDB,
DialectClickHouse,
}
cases := []struct {
name string
parentOp string
logic string
}{
{
name: "array under comparison",
parentOp: "==",
logic: `{"==":[{"map":[{"var":"nums"},{"oops":[{"var":"item"}]}]},1]}`,
},
{
name: "array under logical",
parentOp: "and",
logic: `{"and":[true,{"map":[{"var":"nums"},{"oops":[{"var":"item"}]}]}]}`,
},
}
for _, d := range dialects {
t.Run(d.String(), func(t *testing.T) {
t.Parallel()
tr, err := NewTranspiler(d)
if err != nil {
t.Fatalf("NewTranspiler() error: %v", err)
}
tr.RegisterOperatorFunc("oops", func(_ string, _ []interface{}) (string, error) {
return "", fmt.Errorf("boom")
})
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
logicMap := decodeLogicMapForPathTest(t, tc.logic)
_, err := tr.Transpile(tc.logic)
assertNestedPathNotTruncated(t, err, tc.parentOp)
_, _, err = tr.TranspileParameterized(tc.logic)
assertNestedPathNotTruncated(t, err, tc.parentOp)
_, err = tr.TranspileFromMap(logicMap)
assertNestedPathNotTruncated(t, err, tc.parentOp)
_, _, err = tr.TranspileParameterizedFromMap(logicMap)
assertNestedPathNotTruncated(t, err, tc.parentOp)
})
}
})
}
}