-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_nested_scope_and_path_regression_test.go
More file actions
105 lines (91 loc) · 2.78 KB
/
array_nested_scope_and_path_regression_test.go
File metadata and controls
105 lines (91 loc) · 2.78 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
package jsonlogic2sql
import (
"fmt"
"strings"
"testing"
)
func TestNestedCurrentDottedUsesInnerAlias_AllDialects(t *testing.T) {
t.Parallel()
dialects := []Dialect{
DialectBigQuery,
DialectSpanner,
DialectPostgreSQL,
DialectDuckDB,
DialectClickHouse,
}
logic := `{"map":[{"var":"groups"},{"filter":[{"var":"item.values"},{"and":[{"==":[{"var":"current"},1]},{">=":[{"var":"current.base"},0]}]}]}]}`
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)
}
sql, err := tr.Transpile(logic)
if err != nil {
t.Fatalf("Transpile() error: %v", err)
}
if strings.Contains(sql, "AND elem.base") {
t.Fatalf("unexpected outer alias in inner current.* predicate: %s", sql)
}
if !strings.Contains(sql, "AND elem1.base") {
t.Fatalf("expected inner alias for current.* in predicate, got: %s", sql)
}
psql, _, err := tr.TranspileParameterized(logic)
if err != nil {
t.Fatalf("TranspileParameterized() error: %v", err)
}
if strings.Contains(psql, "AND elem.base") {
t.Fatalf("unexpected outer alias in parameterized inner current.* predicate: %s", psql)
}
if !strings.Contains(psql, "AND elem1.base") {
t.Fatalf("expected inner alias for current.* in parameterized predicate, got: %s", psql)
}
})
}
}
func TestCustomOperatorPathInsideArrayContexts_InlineAndParam(t *testing.T) {
t.Parallel()
tr, err := NewTranspiler(DialectBigQuery)
if err != nil {
t.Fatalf("NewTranspiler() error: %v", err)
}
tr.RegisterOperatorFunc("oops", func(_ string, _ []interface{}) (string, error) {
return "", fmt.Errorf("boom")
})
cases := []struct {
name string
logic string
wantPath string
}{
{
name: "direct custom operator in map transform",
logic: `{"map":[{"var":"bag.records"},{"oops":[{"var":"item"}]}]}`,
wantPath: "$.map[1].oops",
},
{
name: "nested custom operator under logical in map transform",
logic: `{"map":[{"var":"bag.records"},{"and":[{"oops":[{"var":"item"}]},{">":[{"var":"current"},0]}]}]}`,
wantPath: "$.map[1].and[0].oops",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, err := tr.Transpile(tc.logic)
if err == nil {
t.Fatalf("expected inline error for %s, got nil", tc.name)
}
if !strings.Contains(err.Error(), tc.wantPath) {
t.Fatalf("inline error missing expected path %q: %v", tc.wantPath, err)
}
_, _, err = tr.TranspileParameterized(tc.logic)
if err == nil {
t.Fatalf("expected parameterized error for %s, got nil", tc.name)
}
if !strings.Contains(err.Error(), tc.wantPath) {
t.Fatalf("parameterized error missing expected path %q: %v", tc.wantPath, err)
}
})
}
}