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

Method TestBatchInsertReturningKeys

internal/testsuite/sql_suite.go:1354–1404  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1352}
1353
1354func (s *SQLTestSuite) TestBatchInsertReturningKeys() {
1355 switch s.Adapter() {
1356 case "postgresql", "cockroachdb":
1357 // pass
1358 default:
1359 s.T().Skip("Currently not supported.")
1360 return
1361 }
1362
1363 sess := s.Session()
1364
1365 err := sess.Collection("artist").Truncate()
1366 s.NoError(err)
1367
1368 batchSize, totalItems := 7, 12
1369
1370 batch := sess.SQL().InsertInto("artist").Columns("name").Returning("id").Batch(batchSize)
1371
1372 go func() {
1373 defer batch.Done()
1374 for i := 0; i < totalItems; i++ {
1375 batch.Values(fmt.Sprintf("artist-%d", i))
1376 }
1377 }()
1378
1379 var keyMap []struct {
1380 ID int `db:"id"`
1381 }
1382 for batch.NextResult(&keyMap) {
1383 // Each insertion must produce new keys.
1384 s.True(len(keyMap) > 0)
1385 s.True(len(keyMap) <= batchSize)
1386
1387 // Find the elements we've just inserted
1388 keys := make([]int, 0, len(keyMap))
1389 for i := range keyMap {
1390 keys = append(keys, keyMap[i].ID)
1391 }
1392
1393 // Make sure count matches.
1394 c, err := sess.Collection("artist").Find(db.Cond{"id": keys}).Count()
1395 s.NoError(err)
1396 s.Equal(uint64(len(keyMap)), c)
1397 }
1398 s.NoError(batch.Err())
1399
1400 // Count all new elements
1401 c, err := sess.Collection("artist").Find().Count()
1402 s.NoError(err)
1403 s.Equal(uint64(totalItems), c)
1404}
1405
1406func (s *SQLTestSuite) TestPaginator() {
1407 sess := s.Session()

Callers

nothing calls this directly

Calls 15

AdapterMethod · 0.65
SessionMethod · 0.65
TruncateMethod · 0.65
CollectionMethod · 0.65
BatchMethod · 0.65
ReturningMethod · 0.65
ColumnsMethod · 0.65
InsertIntoMethod · 0.65
SQLMethod · 0.65
DoneMethod · 0.65
ValuesMethod · 0.65
NextResultMethod · 0.65

Tested by

no test coverage detected