(t *testing.T)
| 10 | ) |
| 11 | |
| 12 | func 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 | } |
nothing calls this directly
no test coverage detected