MCPcopy
hub / github.com/jmoiron/sqlx / TestConn

Function TestConn

sqlx_context_test.go:1347–1427  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1345}
1346
1347func TestConn(t *testing.T) {
1348 var schema = Schema{
1349 create: `
1350 CREATE TABLE tt_conn (
1351 id integer,
1352 value text NULL DEFAULT NULL
1353 );`,
1354 drop: "drop table tt_conn;",
1355 }
1356
1357 RunWithSchemaContext(context.Background(), schema, t, func(ctx context.Context, db *DB, t *testing.T) {
1358 conn, err := db.Connx(ctx)
1359 defer conn.Close() //lint:ignore SA5001 it's OK to ignore this here.
1360 if err != nil {
1361 t.Fatal(err)
1362 }
1363
1364 _, err = conn.ExecContext(ctx, conn.Rebind(`INSERT INTO tt_conn (id, value) VALUES (?, ?), (?, ?)`), 1, "a", 2, "b")
1365 if err != nil {
1366 t.Fatal(err)
1367 }
1368
1369 type s struct {
1370 ID int `db:"id"`
1371 Value string `db:"value"`
1372 }
1373
1374 v := []s{}
1375
1376 err = conn.SelectContext(ctx, &v, "SELECT * FROM tt_conn ORDER BY id ASC")
1377 if err != nil {
1378 t.Fatal(err)
1379 }
1380
1381 if v[0].ID != 1 {
1382 t.Errorf("Expecting ID of 1, got %d", v[0].ID)
1383 }
1384
1385 v1 := s{}
1386 err = conn.GetContext(ctx, &v1, conn.Rebind("SELECT * FROM tt_conn WHERE id=?"), 1)
1387
1388 if err != nil {
1389 t.Fatal(err)
1390 }
1391 if v1.ID != 1 {
1392 t.Errorf("Expecting to get back 1, but got %v\n", v1.ID)
1393 }
1394
1395 stmt, err := conn.PreparexContext(ctx, conn.Rebind("SELECT * FROM tt_conn WHERE id=?"))
1396 if err != nil {
1397 t.Fatal(err)
1398 }
1399 v1 = s{}
1400 tx, err := conn.BeginTxx(ctx, nil)
1401 if err != nil {
1402 t.Fatal(err)
1403 }
1404 tstmt := tx.Stmtx(stmt)

Callers

nothing calls this directly

Calls 15

RunWithSchemaContextFunction · 0.85
ConnxMethod · 0.80
ErrorfMethod · 0.80
StmtxMethod · 0.80
ErrorMethod · 0.80
NextMethod · 0.80
CloseMethod · 0.65
ExecContextMethod · 0.65
RebindMethod · 0.65
QueryRowxMethod · 0.65
QueryxContextMethod · 0.65
SelectContextMethod · 0.45

Tested by

no test coverage detected