MCPcopy Index your code
hub / github.com/upper/db / TestUpdate

Function TestUpdate

internal/sqlbuilder/builder_test.go:1307–1458  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1305}
1306
1307func TestUpdate(t *testing.T) {
1308 b := &sqlBuilder{t: newTemplateWithUtils(&testTemplate)}
1309 assert := assert.New(t)
1310
1311 assert.Equal(
1312 `UPDATE "artist" SET "name" = $1`,
1313 b.Update("artist").Set("name", "Artist").String(),
1314 )
1315
1316 assert.Equal(
1317 `UPDATE "artist" SET "name" = $1 RETURNING "name"`,
1318 b.Update("artist").Set("name", "Artist").Amend(func(query string) string {
1319 return query + ` RETURNING "name"`
1320 }).String(),
1321 )
1322
1323 {
1324 idSlice := []int64{8, 7, 6}
1325 q := b.Update("artist").Set(db.Cond{"some_column": 10}).Where(db.Cond{"id": 1}, db.Cond{"another_val": idSlice})
1326 assert.Equal(
1327 `UPDATE "artist" SET "some_column" = $1 WHERE ("id" = $2 AND "another_val" IN ($3, $4, $5))`,
1328 q.String(),
1329 )
1330 assert.Equal(
1331 []interface{}{10, 1, int64(8), int64(7), int64(6)},
1332 q.Arguments(),
1333 )
1334 }
1335
1336 {
1337 idSlice := []int64{}
1338 q := b.Update("artist").Set(db.Cond{"some_column": 10}).Where(db.Cond{"id": 1}, db.Cond{"another_val": idSlice})
1339 assert.Equal(
1340 `UPDATE "artist" SET "some_column" = $1 WHERE ("id" = $2 AND "another_val" IN (NULL))`,
1341 q.String(),
1342 )
1343 assert.Equal(
1344 []interface{}{10, 1},
1345 q.Arguments(),
1346 )
1347 }
1348
1349 {
1350 idSlice := []int64{}
1351 q := b.Update("artist").Where(db.Cond{"id": 1}, db.Cond{"another_val": idSlice}).Set(db.Cond{"some_column": 10})
1352 assert.Equal(
1353 `UPDATE "artist" SET "some_column" = $1 WHERE ("id" = $2 AND "another_val" IN (NULL))`,
1354 q.String(),
1355 )
1356 assert.Equal(
1357 []interface{}{10, 1},
1358 q.Arguments(),
1359 )
1360 }
1361
1362 assert.Equal(
1363 `UPDATE "artist" SET "name" = $1 WHERE ("id" < $2)`,
1364 b.Update("artist").Set("name = ?", "Artist").Where("id <", 5).String(),

Callers

nothing calls this directly

Calls 9

UpdateMethod · 0.95
newTemplateWithUtilsFunction · 0.85
RawMethod · 0.80
NewMethod · 0.65
StringMethod · 0.65
SetMethod · 0.65
AmendMethod · 0.65
WhereMethod · 0.65
ArgumentsMethod · 0.65

Tested by

no test coverage detected