-
Notifications
You must be signed in to change notification settings - Fork 3
/
query_test.go
93 lines (87 loc) · 1.77 KB
/
query_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
package fauna
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type fqlSuccessCase struct {
testName string
query string
args map[string]any
wants *Query
}
func TestFQL(t *testing.T) {
testDate := time.Date(2023, 2, 24, 0, 0, 0, 0, time.UTC)
testDino := map[string]any{
"name": "Dino",
"age": 0,
"birthdate": testDate,
}
testInnerDino, _ := FQL("let x = ${my_var}", map[string]any{"my_var": testDino})
testCases := []fqlSuccessCase{
{
"simple literal case",
"let x = 11",
nil,
&Query{
fragments: []*queryFragment{{true, "let x = 11"}},
},
},
{
"simple literal case with brace",
"let x = { y: 11 }",
nil,
&Query{
fragments: []*queryFragment{{true, "let x = { y: 11 }"}},
},
},
{
"template variable and fauna variable",
"let age = ${n1}\n\"Alice is #{age} years old.\"",
map[string]any{"n1": 5},
&Query{
fragments: []*queryFragment{
{true, "let age = "},
{false, 5},
{true, "\n\"Alice is #{age} years old.\""},
},
},
},
{
"template variable",
"let x = ${my_var}",
map[string]any{"my_var": testDino},
&Query{
fragments: []*queryFragment{
{true, "let x = "},
{false, testDino},
},
},
},
{
"query variable",
"${inner}\nx { name }",
map[string]any{
"inner": testInnerDino,
},
&Query{
fragments: []*queryFragment{
{false, testInnerDino},
{true, "\nx { name }"},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
if q, err := FQL(tc.query, tc.args); assert.NoError(t, err) {
assert.Equal(t, tc.wants, q)
}
})
}
}
func BenchmarkFQL(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = FQL(`${arg0}.length`, map[string]any{"arg0": "foo"})
}
}