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

Function TestNamedQueries

named_test.go:134–303  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

132}
133
134func TestNamedQueries(t *testing.T) {
135 RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
136 loadDefaultFixture(db, t)
137 test := Test{t}
138 var ns *NamedStmt
139 var err error
140
141 // Check that invalid preparations fail
142 _, err = db.PrepareNamed("SELECT * FROM person WHERE first_name=:first:name")
143 if err == nil {
144 t.Error("Expected an error with invalid prepared statement.")
145 }
146
147 _, err = db.PrepareNamed("invalid sql")
148 if err == nil {
149 t.Error("Expected an error with invalid prepared statement.")
150 }
151
152 // Check closing works as anticipated
153 ns, err = db.PrepareNamed("SELECT * FROM person WHERE first_name=:first_name")
154 test.Error(err)
155 err = ns.Close()
156 test.Error(err)
157
158 ns, err = db.PrepareNamed(`
159 SELECT first_name, last_name, email
160 FROM person WHERE first_name=:first_name AND email=:email`)
161 test.Error(err)
162
163 // test Queryx w/ uses Query
164 p := Person{FirstName: "Jason", LastName: "Moiron", Email: "jmoiron@jmoiron.net"}
165
166 rows, err := ns.Queryx(p)
167 test.Error(err)
168 for rows.Next() {
169 var p2 Person
170 rows.StructScan(&p2)
171 if p.FirstName != p2.FirstName {
172 t.Errorf("got %s, expected %s", p.FirstName, p2.FirstName)
173 }
174 if p.LastName != p2.LastName {
175 t.Errorf("got %s, expected %s", p.LastName, p2.LastName)
176 }
177 if p.Email != p2.Email {
178 t.Errorf("got %s, expected %s", p.Email, p2.Email)
179 }
180 }
181
182 // test Select
183 people := make([]Person, 0, 5)
184 err = ns.Select(&people, p)
185 test.Error(err)
186
187 if len(people) != 1 {
188 t.Errorf("got %d results, expected %d", len(people), 1)
189 }
190 if p.FirstName != people[0].FirstName {
191 t.Errorf("got %s, expected %s", p.FirstName, people[0].FirstName)

Callers

nothing calls this directly

Calls 15

ErrorMethod · 0.95
CloseMethod · 0.95
QueryxMethod · 0.95
SelectMethod · 0.95
ExecMethod · 0.95
RunWithSchemaFunction · 0.85
loadDefaultFixtureFunction · 0.85
NextMethod · 0.80
ErrorfMethod · 0.80
MustBeginMethod · 0.80
NamedStmtMethod · 0.80
RebindMethod · 0.65

Tested by

no test coverage detected