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

Function TestNamedContextQueries

named_context_test.go:12–137  ·  named_context_test.go::TestNamedContextQueries
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestNamedContextQueries(t *testing.T) {
13 RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
14 loadDefaultFixture(db, t)
15 test := Test{t}
16 var ns *NamedStmt
17 var err error
18
19 ctx := context.Background()
20
21 // Check that invalid preparations fail
22 _, err = db.PrepareNamedContext(ctx, "SELECT * FROM person WHERE first_name=:first:name")
23 if err == nil {
24 t.Error("Expected an error with invalid prepared statement.")
25 }
26
27 _, err = db.PrepareNamedContext(ctx, "invalid sql")
28 if err == nil {
29 t.Error("Expected an error with invalid prepared statement.")
30 }
31
32 // Check closing works as anticipated
33 ns, err = db.PrepareNamedContext(ctx, "SELECT * FROM person WHERE first_name=:first_name")
34 test.Error(err)
35 err = ns.Close()
36 test.Error(err)
37
38 ns, err = db.PrepareNamedContext(ctx, `
39 SELECT first_name, last_name, email
40 FROM person WHERE first_name=:first_name AND email=:email`)
41 test.Error(err)
42
43 // test Queryx w/ uses Query
44 p := Person{FirstName: "Jason", LastName: "Moiron", Email: "jmoiron@jmoiron.net"}
45
46 rows, err := ns.QueryxContext(ctx, p)
47 test.Error(err)
48 for rows.Next() {
49 var p2 Person
50 rows.StructScan(&p2)
51 if p.FirstName != p2.FirstName {
52 t.Errorf("got %s, expected %s", p.FirstName, p2.FirstName)
53 }
54 if p.LastName != p2.LastName {
55 t.Errorf("got %s, expected %s", p.LastName, p2.LastName)
56 }
57 if p.Email != p2.Email {
58 t.Errorf("got %s, expected %s", p.Email, p2.Email)
59 }
60 }
61
62 // test Select
63 people := make([]Person, 0, 5)
64 err = ns.SelectContext(ctx, &people, p)
65 test.Error(err)
66
67 if len(people) != 1 {
68 t.Errorf("got %d results, expected %d", len(people), 1)
69 }

Callers

nothing calls this directly

Calls 15

ErrorMethod · 0.95
CloseMethod · 0.95
QueryxContextMethod · 0.95
SelectContextMethod · 0.95
ExecContextMethod · 0.95
RunWithSchemaFunction · 0.85
loadDefaultFixtureFunction · 0.85
NextMethod · 0.80
ErrorfMethod · 0.80
MustBeginTxMethod · 0.80
NamedStmtContextMethod · 0.80
RebindMethod · 0.65

Tested by

no test coverage detected