Repository navigation
Expand file tree
/
Copy pathbuilder_test.go
More file actions
178 lines (163 loc) · 5.53 KB
/
Copy pathbuilder_test.go
File metadata and controls
178 lines (163 loc) · 5.53 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
package pipeql
import (
"strings"
"testing"
)
func TestBuilderReadPipeline(t *testing.T) {
q := From("notes").
Filter("is_archived == 0").
Sort([]string{"created_at desc"}).
Take(10)
want := "from notes | filter is_archived == 0 | sort [created_at desc] | take 10"
if got := q.Source(); got != want {
t.Errorf("Source() = %q, want %q", got, want)
}
res, err := q.Compile("postgres")
if err != nil {
t.Fatalf("Compile: %v", err)
}
if !strings.Contains(res.SQL, "SELECT * FROM notes") {
t.Errorf("unexpected sql: %s", res.SQL)
}
if !strings.Contains(res.SQL, "ORDER BY created_at DESC") {
t.Errorf("unexpected sql: %s", res.SQL)
}
}
func TestBuilderJoins(t *testing.T) {
cases := []struct {
name string
got string
want string
}{
{"inner", From("a").Join("b", "a.id == b.a_id").Source(), "from a | join b on a.id == b.a_id"},
{"left", From("a").LeftJoin("b", "a.id == b.a_id").Source(), "from a | left join b on a.id == b.a_id"},
{"full", From("a").FullJoin("b", "a.id == b.a_id").Source(), "from a | full join b on a.id == b.a_id"},
}
for _, c := range cases {
if c.got != c.want {
t.Errorf("%s: Source() = %q, want %q", c.name, c.got, c.want)
}
}
}
func TestBuilderGroup(t *testing.T) {
q := From("orders").Group([]string{"region"}, "total = sum(amt), n = count(*)").Filter("total > 100")
want := "from orders | group [region] (total = sum(amt), n = count(*)) | filter total > 100"
if got := q.Source(); got != want {
t.Errorf("Source() = %q, want %q", got, want)
}
}
func TestBuilderUnion(t *testing.T) {
other := From("archived").Select([]string{"id"})
q := From("active").Select([]string{"id"}).Union(other)
want := "from active | select [id] | union from archived | select [id]"
if got := q.Source(); got != want {
t.Errorf("Source() = %q, want %q", got, want)
}
q2 := From("a").Select([]string{"id"}).UnionAll("from b | select [id]")
if got := q2.Source(); got != "from a | select [id] | union all from b | select [id]" {
t.Errorf("Source() = %q", got)
}
}
func TestBuilderObjectInsert(t *testing.T) {
// Maps are sorted for deterministic SQL: flag < title.
q := Into("notes").Insert(map[string]any{"title": "Hi", "flag": 1})
want := "into notes | insert [flag = $b0, title = $b1]"
if got := q.Source(); got != want {
t.Errorf("Source() = %q, want %q", got, want)
}
vals := q.Values()
if vals["b0"] != 1 || vals["b1"] != "Hi" {
t.Errorf("Values() = %v", vals)
}
res, err := q.Compile("sqlite")
if err != nil {
t.Fatalf("Compile: %v", err)
}
if !strings.Contains(res.SQL, "INSERT INTO notes (flag, title) VALUES (?, ?)") {
t.Errorf("unexpected sql: %s", res.SQL)
}
if len(res.Params) != 2 {
t.Errorf("expected 2 params, got %v", res.Params)
}
}
func TestBuilderObjectInsertOrderedPairs(t *testing.T) {
// Pairs preserves insertion order exactly.
q := Into("notes").Insert(PairsOf("title", "Hi", "flag", 1))
want := "into notes | insert [title = $b0, flag = $b1]"
if got := q.Source(); got != want {
t.Errorf("Source() = %q, want %q", got, want)
}
}
func TestBuilderNullDistinct(t *testing.T) {
q := Into("t").Insert(map[string]any{"a": nil, "b": 2})
want := "into t | insert [a = $b0, b = $b1]"
if got := q.Source(); got != want {
t.Errorf("Source() = %q, want %q", got, want)
}
if q.Values()["b0"] != nil {
t.Errorf("expected nil value, got %v", q.Values()["b0"])
}
}
func TestBuilderAssignmentStrings(t *testing.T) {
q := Into("t").Insert([]string{"title = $title", "flag = 1"})
if got := q.Source(); got != "into t | insert [title = $title, flag = 1]" {
t.Errorf("Source() = %q", got)
}
if len(q.Values()) != 0 {
t.Errorf("expected no values, got %v", q.Values())
}
}
func TestBuilderUpdateDelete(t *testing.T) {
q := From("notes").Filter("id == $id").Update(map[string]any{"title": "new"})
if got := q.Source(); got != "from notes | filter id == $id | update [title = $b0]" {
t.Errorf("Source() = %q", got)
}
d := From("notes").Filter("id == $id").Delete()
if got := d.Source(); got != "from notes | filter id == $id | delete" {
t.Errorf("Source() = %q", got)
}
}
func TestBuilderUpdateAllDeleteAllEscapeHatch(t *testing.T) {
u := From("notes").UpdateAll(map[string]any{"title": "new"})
if got := u.Source(); got != "from notes | update all [title = $b0]" {
t.Errorf("Source() = %q", got)
}
res, err := u.Compile("sqlite")
if err != nil || res == nil || res.SQL == "" || strings.Contains(res.SQL, "WHERE") {
t.Errorf("UpdateAll compiled err=%v res=%v, want full-table UPDATE without WHERE", err, res)
}
d := From("notes").DeleteAll()
if got := d.Source(); got != "from notes | delete all" {
t.Errorf("Source() = %q", got)
}
res, err = d.Compile("sqlite")
if err != nil || res == nil || res.SQL != "DELETE FROM notes;" {
t.Errorf("DeleteAll compiled err=%v res=%v", err, res)
}
}
func TestBuilderUpsert(t *testing.T) {
q := Into("users").
Upsert(PairsOf("id", 1, "name", "Ann")).
Conflict([]string{"id"}).
DoUpdate(map[string]any{"name": "Ann"})
want := "into users | upsert [id = $b0, name = $b1] | conflict [id] | do update [name = $b2]"
if got := q.Source(); got != want {
t.Errorf("Source() = %q, want %q", got, want)
}
res, err := q.Compile("postgres")
if err != nil {
t.Fatalf("Compile: %v", err)
}
if res.StatementType != "upsert" {
t.Errorf("expected upsert, got %s", res.StatementType)
}
if !strings.Contains(res.SQL, "ON CONFLICT (id) DO UPDATE SET name = $3") {
t.Errorf("unexpected sql: %s", res.SQL)
}
}
func TestBuilderString(t *testing.T) {
q := From("t").Take(1)
if got := q.String(); got != "from t | take 1" {
t.Errorf("String() = %q", got)
}
}