-
Notifications
You must be signed in to change notification settings - Fork 208
/
issues_test.go
496 lines (423 loc) · 13.6 KB
/
issues_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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package goqu_test
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/stretchr/testify/suite"
)
type githubIssuesSuite struct {
suite.Suite
}
func (gis *githubIssuesSuite) AfterTest(_, _ string) {
goqu.SetColumnRenameFunction(strings.ToLower)
}
// Test for https://github.com/doug-martin/goqu/issues/49
func (gis *githubIssuesSuite) TestIssue49() {
dialect := goqu.Dialect("default")
filters := goqu.Or()
sql, args, err := dialect.From("table").Where(filters).ToSQL()
gis.NoError(err)
gis.Empty(args)
gis.Equal(`SELECT * FROM "table"`, sql)
sql, args, err = dialect.From("table").Where(goqu.Ex{}).ToSQL()
gis.NoError(err)
gis.Empty(args)
gis.Equal(`SELECT * FROM "table"`, sql)
sql, args, err = dialect.From("table").Where(goqu.ExOr{}).ToSQL()
gis.NoError(err)
gis.Empty(args)
gis.Equal(`SELECT * FROM "table"`, sql)
}
// Test for https://github.com/doug-martin/goqu/issues/115
func (gis *githubIssuesSuite) TestIssue115() {
type TestStruct struct {
Field string
}
goqu.SetColumnRenameFunction(func(col string) string {
return ""
})
_, _, err := goqu.Insert("test").Rows(TestStruct{Field: "hello"}).ToSQL()
gis.EqualError(err, `goqu: a empty identifier was encountered, please specify a "schema", "table" or "column"`)
}
// Test for https://github.com/doug-martin/goqu/issues/118
func (gis *githubIssuesSuite) TestIssue118_withEmbeddedStructWithoutExportedFields() {
// struct is in a custom package
type SimpleRole struct {
permissions []string //nolint:structcheck,unused //needed for test
}
// .....
type Role struct {
*SimpleRole
ID string `json:"id" db:"id" goqu:"skipinsert"`
Key string `json:"key" db:"key"`
Name string `json:"name" db:"name"`
CreatedAt time.Time `json:"-" db:"created_at" goqu:"skipinsert"`
}
rUser := &Role{
Key: `user`,
Name: `User role`,
}
sql, arg, err := goqu.Insert(`rbac_roles`).
Returning(goqu.C(`id`)).
Rows(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(`INSERT INTO "rbac_roles" ("key", "name") VALUES ('user', 'User role') RETURNING "id"`, sql)
sql, arg, err = goqu.Update(`rbac_roles`).
Returning(goqu.C(`id`)).
Set(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`UPDATE "rbac_roles" SET "created_at"='0001-01-01T00:00:00Z',"id"='',"key"='user',"name"='User role' RETURNING "id"`,
sql,
)
rUser = &Role{
SimpleRole: &SimpleRole{},
Key: `user`,
Name: `User role`,
}
sql, arg, err = goqu.Insert(`rbac_roles`).
Returning(goqu.C(`id`)).
Rows(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(`INSERT INTO "rbac_roles" ("key", "name") VALUES ('user', 'User role') RETURNING "id"`, sql)
sql, arg, err = goqu.Update(`rbac_roles`).
Returning(goqu.C(`id`)).
Set(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`UPDATE "rbac_roles" SET `+
`"created_at"='0001-01-01T00:00:00Z',"id"='',"key"='user',"name"='User role' RETURNING "id"`,
sql,
)
}
// Test for https://github.com/doug-martin/goqu/issues/118
func (gis *githubIssuesSuite) TestIssue118_withNilEmbeddedStructWithExportedFields() {
// struct is in a custom package
type SimpleRole struct {
permissions []string //nolint:structcheck,unused // needed for test
IDStr string
}
// .....
type Role struct {
*SimpleRole
ID string `json:"id" db:"id" goqu:"skipinsert"`
Key string `json:"key" db:"key"`
Name string `json:"name" db:"name"`
CreatedAt time.Time `json:"-" db:"created_at" goqu:"skipinsert"`
}
rUser := &Role{
Key: `user`,
Name: `User role`,
}
sql, arg, err := goqu.Insert(`rbac_roles`).
Returning(goqu.C(`id`)).
Rows(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
// it should not insert fields on nil embedded pointers
gis.Equal(`INSERT INTO "rbac_roles" ("key", "name") VALUES ('user', 'User role') RETURNING "id"`, sql)
sql, arg, err = goqu.Update(`rbac_roles`).
Returning(goqu.C(`id`)).
Set(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
// it should not insert fields on nil embedded pointers
gis.Equal(
`UPDATE "rbac_roles" SET "created_at"='0001-01-01T00:00:00Z',"id"='',"key"='user',"name"='User role' RETURNING "id"`,
sql,
)
rUser = &Role{
SimpleRole: &SimpleRole{},
Key: `user`,
Name: `User role`,
}
sql, arg, err = goqu.Insert(`rbac_roles`).
Returning(goqu.C(`id`)).
Rows(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
// it should not insert fields on nil embedded pointers
gis.Equal(
`INSERT INTO "rbac_roles" ("idstr", "key", "name") VALUES ('', 'user', 'User role') RETURNING "id"`,
sql,
)
sql, arg, err = goqu.Update(`rbac_roles`).
Returning(goqu.C(`id`)).
Set(rUser).
ToSQL()
gis.NoError(err)
gis.Empty(arg)
// it should not insert fields on nil embedded pointers
gis.Equal(
`UPDATE "rbac_roles" SET `+
`"created_at"='0001-01-01T00:00:00Z',"id"='',"idstr"='',"key"='user',"name"='User role' RETURNING "id"`,
sql,
)
}
// Test for https://github.com/doug-martin/goqu/issues/118
func (gis *githubIssuesSuite) TestIssue140() {
sql, arg, err := goqu.Insert(`test`).Returning().ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(`INSERT INTO "test" DEFAULT VALUES`, sql)
sql, arg, err = goqu.Update(`test`).Set(goqu.Record{"a": "b"}).Returning().ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`UPDATE "test" SET "a"='b'`,
sql,
)
sql, arg, err = goqu.Delete(`test`).Returning().ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`DELETE FROM "test"`,
sql,
)
sql, arg, err = goqu.Insert(`test`).Returning(nil).ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(`INSERT INTO "test" DEFAULT VALUES`, sql)
sql, arg, err = goqu.Update(`test`).Set(goqu.Record{"a": "b"}).Returning(nil).ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`UPDATE "test" SET "a"='b'`,
sql,
)
sql, arg, err = goqu.Delete(`test`).Returning(nil).ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`DELETE FROM "test"`,
sql,
)
}
// Test for https://github.com/doug-martin/goqu/issues/164
func (gis *githubIssuesSuite) TestIssue164() {
insertDs := goqu.Insert("foo").Rows(goqu.Record{"user_id": 10}).Returning("id")
ds := goqu.From("bar").
With("ins", insertDs).
Select("bar_name").
Where(goqu.Ex{"bar.user_id": goqu.I("ins.user_id")})
sql, args, err := ds.ToSQL()
gis.NoError(err)
gis.Empty(args)
gis.Equal(
`WITH ins AS (INSERT INTO "foo" ("user_id") VALUES (10) RETURNING "id") `+
`SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "ins"."user_id")`,
sql,
)
sql, args, err = ds.Prepared(true).ToSQL()
gis.NoError(err)
gis.Equal([]interface{}{int64(10)}, args)
gis.Equal(
`WITH ins AS (INSERT INTO "foo" ("user_id") VALUES (?) RETURNING "id")`+
` SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "ins"."user_id")`,
sql,
)
updateDs := goqu.Update("foo").Set(goqu.Record{"bar": "baz"}).Returning("id")
ds = goqu.From("bar").
With("upd", updateDs).
Select("bar_name").
Where(goqu.Ex{"bar.user_id": goqu.I("upd.user_id")})
sql, args, err = ds.ToSQL()
gis.NoError(err)
gis.Empty(args)
gis.Equal(
`WITH upd AS (UPDATE "foo" SET "bar"='baz' RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "upd"."user_id")`,
sql,
)
sql, args, err = ds.Prepared(true).ToSQL()
gis.NoError(err)
gis.Equal([]interface{}{"baz"}, args)
gis.Equal(
`WITH upd AS (UPDATE "foo" SET "bar"=? RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "upd"."user_id")`,
sql,
)
deleteDs := goqu.Delete("foo").Where(goqu.Ex{"bar": "baz"}).Returning("id")
ds = goqu.From("bar").
With("del", deleteDs).
Select("bar_name").
Where(goqu.Ex{"bar.user_id": goqu.I("del.user_id")})
sql, args, err = ds.ToSQL()
gis.NoError(err)
gis.Empty(args)
gis.Equal(
`WITH del AS (DELETE FROM "foo" WHERE ("bar" = 'baz') RETURNING "id")`+
` SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "del"."user_id")`,
sql,
)
sql, args, err = ds.Prepared(true).ToSQL()
gis.NoError(err)
gis.Equal([]interface{}{"baz"}, args)
gis.Equal(
`WITH del AS (DELETE FROM "foo" WHERE ("bar" = ?) RETURNING "id")`+
` SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "del"."user_id")`,
sql,
)
}
// Test for https://github.com/doug-martin/goqu/issues/177
func (gis *githubIssuesSuite) TestIssue177() {
ds := goqu.Dialect("postgres").
From("ins1").
With("ins1",
goqu.Dialect("postgres").
Insert("account").
Rows(goqu.Record{"email": "[email protected]", "status": "active", "uuid": "XXX-XXX-XXXX"}).
Returning("*"),
).
With("ins2",
goqu.Dialect("postgres").
Insert("account_user").
Cols("account_id", "user_id").
FromQuery(goqu.Dialect("postgres").
From("ins1").
Select(
"id",
goqu.V(1001),
),
),
).
Select("*")
sql, args, err := ds.ToSQL()
gis.NoError(err)
gis.Equal(`WITH ins1 AS (`+
`INSERT INTO "account" ("email", "status", "uuid") VALUES ('[email protected]', 'active', 'XXX-XXX-XXXX') RETURNING *),`+
` ins2 AS (INSERT INTO "account_user" ("account_id", "user_id") SELECT "id", 1001 FROM "ins1")`+
` SELECT * FROM "ins1"`, sql)
gis.Len(args, 0)
sql, args, err = ds.Prepared(true).ToSQL()
gis.NoError(err)
gis.Equal(`WITH ins1 AS (INSERT INTO "account" ("email", "status", "uuid") VALUES ($1, $2, $3) RETURNING *), ins2`+
` AS (INSERT INTO "account_user" ("account_id", "user_id") SELECT "id", $4 FROM "ins1") SELECT * FROM "ins1"`, sql)
gis.Equal(args, []interface{}{"[email protected]", "active", "XXX-XXX-XXXX", int64(1001)})
}
// Test for https://github.com/doug-martin/goqu/issues/183
func (gis *githubIssuesSuite) TestIssue184() {
expectedErr := fmt.Errorf("an error")
testCases := []struct {
ds exp.AppendableExpression
}{
{ds: goqu.From("test").As("t").SetError(expectedErr)},
{ds: goqu.Insert("test").Rows(goqu.Record{"foo": "bar"}).Returning("foo").SetError(expectedErr)},
{ds: goqu.Update("test").Set(goqu.Record{"foo": "bar"}).Returning("foo").SetError(expectedErr)},
{ds: goqu.Update("test").Set(goqu.Record{"foo": "bar"}).Returning("foo").SetError(expectedErr)},
{ds: goqu.Delete("test").Returning("foo").SetError(expectedErr)},
}
for _, tc := range testCases {
ds := goqu.From(tc.ds)
sql, args, err := ds.ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)
sql, args, err = ds.Prepared(true).ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)
ds = goqu.From("test2").Where(goqu.Ex{"foo": tc.ds})
sql, args, err = ds.ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)
sql, args, err = ds.Prepared(true).ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)
}
}
// Test for https://github.com/doug-martin/goqu/issues/185
func (gis *githubIssuesSuite) TestIssue185() {
mDB, sqlMock, err := sqlmock.New()
gis.NoError(err)
sqlMock.ExpectQuery(
`SELECT \* FROM \(SELECT "id" FROM "table" ORDER BY "id" ASC\) AS "t1" UNION
\(SELECT \* FROM \(SELECT "id" FROM "table" ORDER BY "id" ASC\) AS "t1"\)`,
).
WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n"))
db := goqu.New("mock", mDB)
ds := db.Select("id").From("table").Order(goqu.C("id").Asc()).
Union(
db.Select("id").From("table").Order(goqu.C("id").Asc()),
)
ctx := context.Background()
var i []int
gis.NoError(ds.ScanValsContext(ctx, &i))
gis.Equal([]int{1, 2, 3, 4}, i)
}
// Test for https://github.com/doug-martin/goqu/issues/203
func (gis *githubIssuesSuite) TestIssue203() {
// Schema definitions.
authSchema := goqu.S("company_auth")
// Table definitions
usersTable := authSchema.Table("users")
u := usersTable.As("u")
ds := goqu.From(u).Select(
u.Col("id"),
u.Col("name"),
u.Col("created_at"),
u.Col("updated_at"),
)
sql, args, err := ds.ToSQL()
gis.NoError(err)
gis.Equal(`SELECT "u"."id", "u"."name", "u"."created_at", "u"."updated_at" FROM "company_auth"."users" AS "u"`, sql)
gis.Empty(args, []interface{}{})
sql, args, err = ds.Prepared(true).ToSQL()
gis.NoError(err)
gis.Equal(`SELECT "u"."id", "u"."name", "u"."created_at", "u"."updated_at" FROM "company_auth"."users" AS "u"`, sql)
gis.Empty(args, []interface{}{})
}
func (gis *githubIssuesSuite) TestIssue290() {
type OcomModel struct {
ID uint `json:"id" db:"id" goqu:"skipinsert"`
CreatedDate time.Time `json:"created_date" db:"created_date" goqu:"skipupdate"`
ModifiedDate time.Time `json:"modified_date" db:"modified_date"`
}
type ActiveModel struct {
OcomModel
ActiveStartDate time.Time `json:"active_start_date" db:"active_start_date"`
ActiveEndDate *time.Time `json:"active_end_date" db:"active_end_date"`
}
type CodeModel struct {
ActiveModel
Code string `json:"code" db:"code"`
Description string `json:"description" binding:"required" db:"description"`
}
type CodeExample struct {
CodeModel
}
var item CodeExample
item.Code = "Code"
item.Description = "Description"
item.ID = 1 // Value set HERE!
item.CreatedDate = time.Date(
2021, 1, 1, 1, 1, 1, 1, time.UTC)
item.ModifiedDate = time.Date(
2021, 2, 2, 2, 2, 2, 2, time.UTC) // The Value we Get!
item.ActiveStartDate = time.Date(
2021, 3, 3, 3, 3, 3, 3, time.UTC)
updateQuery := goqu.From("example").Update().Set(item).Where(goqu.C("id").Eq(1))
sql, params, err := updateQuery.ToSQL()
gis.NoError(err)
gis.Empty(params)
gis.Equal(`UPDATE "example" SET "active_end_date"=NULL,"active_start_date"='2021-03-03T03:03:03.000000003Z',"code"='Code',"description"='Description',"id"=1,"modified_date"='2021-02-02T02:02:02.000000002Z' WHERE ("id" = 1)`, sql) //nolint:lll
}
func TestGithubIssuesSuite(t *testing.T) {
suite.Run(t, new(githubIssuesSuite))
}