forked from h22rana/jsonlogic2sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_in_scope_regression_test.go
More file actions
65 lines (58 loc) · 1.77 KB
/
Copy patharray_in_scope_regression_test.go
File metadata and controls
65 lines (58 loc) · 1.77 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
package jsonlogic2sql
import (
"strings"
"testing"
)
func TestTranspile_ArrayLambdaInUsesElementScopeRightOperand(t *testing.T) {
modes := []struct {
name string
schema *Schema
}{
{name: "schema-required", schema: testArrayScopeSchema()},
}
for _, mode := range modes {
t.Run(mode.name, func(t *testing.T) {
for _, d := range allDialects() {
t.Run(d.String(), func(t *testing.T) {
tr, err := NewTranspilerWithConfig(&TranspilerConfig{
Dialect: d,
Schema: mode.schema,
})
if err != nil {
t.Fatalf("NewTranspilerWithConfig() error: %v", err)
}
inlineSQL, err := tr.TranspileValue(`{"filter":[{"var":"numbers"},{"in":[123,{"var":"name"}]}]}`)
if err != nil {
t.Fatalf("TranspileValue() error: %v", err)
}
assertArrayInMembershipSQL(t, d, inlineSQL, "123")
paramSQL, params, err := tr.TranspileParameterizedValue(`{"filter":[{"var":"numbers"},{"in":[123,{"var":"name"}]}]}`)
if err != nil {
t.Fatalf("TranspileParameterizedValue() error: %v", err)
}
if len(params) != 1 {
t.Fatalf("param count = %d, want 1 (params=%v)", len(params), params)
}
assertArrayInMembershipSQL(t, d, paramSQL, firstPlaceholderForDialect(d))
})
}
})
}
}
func assertArrayInMembershipSQL(t *testing.T, d Dialect, sql, valueSQL string) {
t.Helper()
if !strings.Contains(sql, "elem.name") {
t.Fatalf("expected element-scoped RHS elem.name, got: %s", sql)
}
assertSQLFragments(t, sql, []string{testNullSafeArrayMembershipSQL(d, valueSQL, "elem.name")}, []string{"UNNEST(name)", "has(name,"})
}
func firstPlaceholderForDialect(d Dialect) string {
switch d {
case DialectPostgreSQL, DialectDuckDB:
return "$1"
case DialectClickHouse:
return "{p1:Float64}"
default:
return "@p1"
}
}