-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_builder_test.go
More file actions
233 lines (216 loc) · 6.88 KB
/
insert_builder_test.go
File metadata and controls
233 lines (216 loc) · 6.88 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package squildx
import "testing"
func TestInsertBuilder_SimpleInsert(t *testing.T) {
sql, params, err := NewInsert().
Into("users").
Columns("name", "email").
Values(":name, :email", Params{"name": "Alice", "email": "a@b.com"}).
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name, email) VALUES (:name, :email)"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
assertParam(t, params, "name", "Alice")
assertParam(t, params, "email", "a@b.com")
}
func TestInsertBuilder_MultiRow(t *testing.T) {
sql, params, err := NewInsert().
Into("users").
Columns("name", "email").
Values(":n1, :e1", Params{"n1": "Alice", "e1": "a@b.com"}).
Values(":n2, :e2", Params{"n2": "Bob", "e2": "b@b.com"}).
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name, email) VALUES (:n1, :e1), (:n2, :e2)"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
assertParam(t, params, "n1", "Alice")
assertParam(t, params, "e1", "a@b.com")
assertParam(t, params, "n2", "Bob")
assertParam(t, params, "e2", "b@b.com")
}
func TestInsertBuilder_StructInsert(t *testing.T) {
type User struct {
Name string `db:"name"`
Email string `db:"email"`
}
sql, params, err := NewInsert().
Into("users").
ValuesObject(User{Name: "Alice", Email: "a@b.com"}).
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name, email) VALUES (:name, :email)"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
assertParam(t, params, "name", "Alice")
assertParam(t, params, "email", "a@b.com")
}
func TestInsertBuilder_InsertSelect(t *testing.T) {
sub := New().Select("name", "email").From("temp_users").Where("active = :active", Params{"active": true})
sql, params, err := NewInsert().
Into("users").
Columns("name", "email").
Select(sub).
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name, email) SELECT name, email FROM temp_users WHERE active = :active"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
assertParam(t, params, "active", true)
}
func TestInsertBuilder_OnConflictDoNothing(t *testing.T) {
sql, _, err := NewInsert().
Into("users").
Columns("id", "name", "email").
Values(":id, :name, :email", Params{"id": 1, "name": "Alice", "email": "a@b.com"}).
OnConflictDoNothing("id").
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (id, name, email) VALUES (:id, :name, :email) ON CONFLICT (id) DO NOTHING"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
}
func TestInsertBuilder_OnConflictDoUpdate(t *testing.T) {
sql, _, err := NewInsert().
Into("users").
Columns("id", "name", "email").
Values(":id, :name, :email", Params{"id": 1, "name": "Alice", "email": "a@b.com"}).
OnConflictDoUpdate([]string{"id"}, "name = EXCLUDED.name, email = EXCLUDED.email").
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (id, name, email) VALUES (:id, :name, :email) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, email = EXCLUDED.email"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
}
func TestInsertBuilder_Returning(t *testing.T) {
sql, _, err := NewInsert().
Into("users").
Columns("name", "email").
Values(":name, :email", Params{"name": "Alice", "email": "a@b.com"}).
Returning("id", "created_at").
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name, email) VALUES (:name, :email) RETURNING id, created_at"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
}
func TestInsertBuilder_FullCombination(t *testing.T) {
sql, params, err := NewInsert().
Into("users").
Columns("id", "name", "email").
Values(":id, :name, :email", Params{"id": 1, "name": "Alice", "email": "a@b.com"}).
OnConflictDoUpdate([]string{"id"}, "name = EXCLUDED.name, updated_at = :now", Params{"now": "2024-01-01"}).
Returning("id", "updated_at").
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (id, name, email) VALUES (:id, :name, :email) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, updated_at = :now RETURNING id, updated_at"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
assertParam(t, params, "id", 1)
assertParam(t, params, "name", "Alice")
assertParam(t, params, "email", "a@b.com")
assertParam(t, params, "now", "2024-01-01")
}
func TestInsertBuilder_Immutability(t *testing.T) {
base := NewInsert().Into("users").Columns("name").
Values(":name", Params{"name": "Alice"})
withReturning := base.Returning("id")
withConflict := base.OnConflictDoNothing("name")
sql1, _, err1 := base.Build()
sql2, _, err2 := withReturning.Build()
sql3, _, err3 := withConflict.Build()
if err1 != nil {
t.Fatalf("base build error: %v", err1)
}
if err2 != nil {
t.Fatalf("withReturning build error: %v", err2)
}
if err3 != nil {
t.Fatalf("withConflict build error: %v", err3)
}
if sql1 != "INSERT INTO users (name) VALUES (:name)" {
t.Errorf("base SQL unexpected: %s", sql1)
}
if sql2 != "INSERT INTO users (name) VALUES (:name) RETURNING id" {
t.Errorf("withReturning SQL unexpected: %s", sql2)
}
if sql3 != "INSERT INTO users (name) VALUES (:name) ON CONFLICT (name) DO NOTHING" {
t.Errorf("withConflict SQL unexpected: %s", sql3)
}
}
func TestInsertBuilder_ReturningObject(t *testing.T) {
type Result struct {
ID int `db:"id"`
CreatedAt string `db:"created_at"`
}
sql, _, err := NewInsert().
Into("users").
Columns("name").
Values(":name", Params{"name": "Alice"}).
ReturningObject(Result{}).
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name) VALUES (:name) RETURNING id, created_at"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
}
func TestInsertBuilder_ColumnsObject(t *testing.T) {
type User struct {
Name string `db:"name"`
Email string `db:"email"`
}
sql, _, err := NewInsert().
Into("users").
ColumnsObject(User{}).
Values(":name, :email", Params{"name": "Alice", "email": "a@b.com"}).
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name, email) VALUES (:name, :email)"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
}
func TestInsertBuilder_AtPrefix(t *testing.T) {
sql, params, err := NewInsert().
Into("users").
Columns("name").
Values("@name", Params{"name": "Alice"}).
Build()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "INSERT INTO users (name) VALUES (@name)"
if sql != want {
t.Errorf("SQL mismatch\n got: %s\nwant: %s", sql, want)
}
assertParam(t, params, "name", "Alice")
}