-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_test.go
More file actions
365 lines (341 loc) · 9.87 KB
/
Copy pathinsert_test.go
File metadata and controls
365 lines (341 loc) · 9.87 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package psql
import (
"reflect"
"testing"
)
// Test struct for INSERT tests
type insertTestStruct struct {
Id int
Name string
Email string
}
// Test struct with JSONB for INSERT tests
type insertJsonbStruct struct {
Id int
Picture string `jsonb:"meta"`
Tags string `jsonb:"meta"`
}
func TestInsert(t *testing.T) {
t.Parallel()
m := NewModel(insertTestStruct{})
tests := []struct {
name string
build func() *InsertSQL
wantSQL string
wantArgs []interface{}
}{
{
name: "single field using field name",
build: func() *InsertSQL {
return m.Insert("Name", "test")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1)",
wantArgs: []interface{}{"test"},
},
{
name: "multiple fields using field names",
build: func() *InsertSQL {
return m.Insert("Name", "test", "Email", "test@example.com")
},
wantSQL: "INSERT INTO insert_test_structs (name, email) VALUES ($1, $2)",
wantArgs: []interface{}{"test", "test@example.com"},
},
{
name: "using Changes",
build: func() *InsertSQL {
changes := m.Changes(RawChanges{"Name": "test"})
return m.Insert(changes)
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1)",
wantArgs: []interface{}{"test"},
},
{
name: "using FieldChanges",
build: func() *InsertSQL {
changes := m.FieldChanges(RawChanges{"Name": "test"})
return m.Insert(changes)
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1)",
wantArgs: []interface{}{"test"},
},
{
name: "mixed changes",
build: func() *InsertSQL {
changes := m.Changes(RawChanges{"Name": "test"})
return m.Insert(changes, "Email", "test@example.com")
},
wantSQL: "INSERT INTO insert_test_structs (name, email) VALUES ($1, $2)",
wantArgs: []interface{}{"test", "test@example.com"},
},
{
name: "duplicate field uses last value",
build: func() *InsertSQL {
return m.Insert("Name", "first", "Name", "second")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1)",
wantArgs: []interface{}{"second"},
},
{
name: "invalid field is ignored",
build: func() *InsertSQL {
return m.Insert("InvalidField", "test", "Name", "valid")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1)",
wantArgs: []interface{}{"valid"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sql := tt.build()
gotSQL, gotArgs := sql.StringValues()
if gotSQL != tt.wantSQL {
t.Errorf("SQL = %q, want %q", gotSQL, tt.wantSQL)
}
if !reflect.DeepEqual(gotArgs, tt.wantArgs) {
t.Errorf("Args = %v, want %v", gotArgs, tt.wantArgs)
}
})
}
}
func TestInsertReturning(t *testing.T) {
t.Parallel()
m := NewModel(insertTestStruct{})
tests := []struct {
name string
build func() *InsertSQL
wantSQL string
}{
{
name: "single column",
build: func() *InsertSQL {
return m.Insert("Name", "test").Returning("id")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) RETURNING id",
},
{
name: "multiple columns",
build: func() *InsertSQL {
return m.Insert("Name", "test").Returning("id", "name")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) RETURNING id, name",
},
{
name: "with alias",
build: func() *InsertSQL {
return m.Insert("Name", "test").Returning("id AS foobar", "name")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) RETURNING id AS foobar, name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.build().String()
if got != tt.wantSQL {
t.Errorf("String() = %q, want %q", got, tt.wantSQL)
}
})
}
}
func TestInsertOnConflict(t *testing.T) {
t.Parallel()
m := NewModel(insertTestStruct{})
tests := []struct {
name string
build func() *InsertSQL
wantSQL string
}{
{
name: "do nothing without target",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict().DoNothing()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT DO NOTHING",
},
{
name: "do nothing with single target",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict("name").DoNothing()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT (name) DO NOTHING",
},
{
name: "do nothing with multiple targets",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict("name", "email").DoNothing()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT (name, email) DO NOTHING",
},
{
name: "do nothing with expression target",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict("lower(name)").DoNothing()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT (lower(name)) DO NOTHING",
},
{
name: "do nothing with partial index",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict("(name) WHERE TRUE").DoNothing()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT (name) WHERE TRUE DO NOTHING",
},
{
name: "do nothing reversed order",
build: func() *InsertSQL {
return m.Insert("Name", "test").DoNothing().OnConflict()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT DO NOTHING",
},
{
name: "only OnConflict without action",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1)",
},
{
name: "only DoNothing without OnConflict",
build: func() *InsertSQL {
return m.Insert("Name", "test").DoNothing()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1)",
},
{
name: "with returning",
build: func() *InsertSQL {
return m.Insert("Name", "test").Returning("id").OnConflict().DoNothing()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT DO NOTHING RETURNING id",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.build().String()
if got != tt.wantSQL {
t.Errorf("String() = %q, want %q", got, tt.wantSQL)
}
})
}
}
func TestInsertDoUpdate(t *testing.T) {
t.Parallel()
m := NewModel(insertTestStruct{})
tests := []struct {
name string
build func() *InsertSQL
wantSQL string
}{
{
name: "do update with expression",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict("name").DoUpdate("email = NULL")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT (name) DO UPDATE SET email = NULL",
},
{
name: "do update all",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict("name").DoUpdateAll()
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name",
},
{
name: "do update all with additional expression",
build: func() *InsertSQL {
return m.Insert("Name", "test").OnConflict("name").DoUpdateAll().DoUpdate("email = NULL")
},
wantSQL: "INSERT INTO insert_test_structs (name) VALUES ($1) ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name, email = NULL",
},
{
name: "do update all except",
build: func() *InsertSQL {
return m.Insert("Name", "test", "Email", "test@example.com").OnConflict("name").DoUpdateAllExcept("name")
},
wantSQL: "INSERT INTO insert_test_structs (name, email) VALUES ($1, $2) ON CONFLICT (name) DO UPDATE SET email = EXCLUDED.email",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.build().String()
if got != tt.wantSQL {
t.Errorf("String() = %q, want %q", got, tt.wantSQL)
}
})
}
}
func TestInsertWithJsonb(t *testing.T) {
t.Parallel()
m := NewModel(insertJsonbStruct{})
tests := []struct {
name string
build func() *InsertSQL
wantSQL string
wantArgs []interface{}
}{
{
name: "single jsonb field",
build: func() *InsertSQL {
changes := m.Changes(RawChanges{"Picture": "test.jpg"})
return m.Insert(changes)
},
wantSQL: "INSERT INTO insert_jsonb_structs (meta) VALUES ($1)",
wantArgs: []interface{}{`{"picture":"test.jpg"}`},
},
{
name: "multiple jsonb fields in same column",
build: func() *InsertSQL {
changes := m.Changes(RawChanges{"Picture": "test.jpg", "Tags": "a,b,c"})
return m.Insert(changes)
},
wantSQL: "INSERT INTO insert_jsonb_structs (meta) VALUES ($1)",
// Note: the order may vary, so we just check that SQL is correct
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sql := tt.build()
gotSQL, gotArgs := sql.StringValues()
if gotSQL != tt.wantSQL {
t.Errorf("SQL = %q, want %q", gotSQL, tt.wantSQL)
}
if tt.wantArgs != nil && len(gotArgs) != len(tt.wantArgs) {
t.Errorf("Args count = %d, want %d", len(gotArgs), len(tt.wantArgs))
}
})
}
}
func TestInsertTap(t *testing.T) {
t.Parallel()
m := NewModel(insertTestStruct{})
sql := m.Insert("Name", "test").Tap(func(i *InsertSQL) *InsertSQL {
return i.Returning("id")
})
got := sql.String()
want := "INSERT INTO insert_test_structs (name) VALUES ($1) RETURNING id"
if got != want {
t.Errorf("String() = %q, want %q", got, want)
}
}
func TestInsertAsInsert(t *testing.T) {
t.Parallel()
m := NewModel(insertTestStruct{})
sql := m.NewSQL("INSERT INTO custom_table (name) VALUES ($?)", "test").AsInsert()
got := sql.OnConflict().DoNothing().String()
want := "INSERT INTO custom_table (name) VALUES ($1) ON CONFLICT DO NOTHING"
if got != want {
t.Errorf("String() = %q, want %q", got, want)
}
}
func TestInsertEmpty(t *testing.T) {
t.Parallel()
m := NewModel(insertTestStruct{})
// Insert with no changes should return empty SQL string
sql := m.Insert()
sqlStr, args := sql.StringValues()
if sqlStr != "" {
t.Errorf("SQL = %q, want empty string", sqlStr)
}
if len(args) != 0 {
t.Errorf("Args = %v, want empty", args)
}
}