MCPcopy
hub / github.com/jackc/pgx / TestConnSendBatch

Function TestConnSendBatch

batch_test.go:22–158  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

20)
21
22func TestConnSendBatch(t *testing.T) {
23 t.Parallel()
24
25 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
26 defer cancel()
27
28 pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
29 pgxtest.SkipCockroachDB(t, conn, "Server serial type is incompatible with test")
30
31 sql := `create temporary table ledger(
32 id serial primary key,
33 description varchar not null,
34 amount int not null
35 );`
36 mustExec(t, conn, sql)
37
38 batch := &pgx.Batch{}
39 batch.Queue("insert into ledger(description, amount) values($1, $2)", "q1", 1)
40 batch.Queue("insert into ledger(description, amount) values($1, $2)", "q2", 2)
41 batch.Queue("insert into ledger(description, amount) values($1, $2)", "q3", 3)
42 batch.Queue("select id, description, amount from ledger order by id")
43 batch.Queue("select id, description, amount from ledger order by id")
44 batch.Queue("select * from ledger where false")
45 batch.Queue("select sum(amount) from ledger")
46
47 br := conn.SendBatch(ctx, batch)
48
49 ct, err := br.Exec()
50 if err != nil {
51 t.Error(err)
52 }
53 if ct.RowsAffected() != 1 {
54 t.Errorf("ct.RowsAffected() => %v, want %v", ct.RowsAffected(), 1)
55 }
56
57 ct, err = br.Exec()
58 if err != nil {
59 t.Error(err)
60 }
61 if ct.RowsAffected() != 1 {
62 t.Errorf("ct.RowsAffected() => %v, want %v", ct.RowsAffected(), 1)
63 }
64
65 ct, err = br.Exec()
66 if err != nil {
67 t.Error(err)
68 }
69 if ct.RowsAffected() != 1 {
70 t.Errorf("ct.RowsAffected() => %v, want %v", ct.RowsAffected(), 1)
71 }
72
73 selectFromLedgerExpectedRows := []struct {
74 id int32
75 description string
76 amount int32
77 }{
78 {1, "q1", 1},
79 {2, "q2", 2},

Callers

nothing calls this directly

Calls 14

QueueMethod · 0.95
RunWithQueryExecModesFunction · 0.92
SkipCockroachDBFunction · 0.92
mustExecFunction · 0.85
RowsAffectedMethod · 0.80
SendBatchMethod · 0.65
ExecMethod · 0.65
QueryMethod · 0.65
NextMethod · 0.65
ScanMethod · 0.65
ErrMethod · 0.65
QueryRowMethod · 0.65

Tested by

no test coverage detected