forked from jmoiron/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add NamedStmt support, lots more testing around it... still no Map su…
…pport
- Loading branch information
Showing
3 changed files
with
339 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package sqlx | ||
|
||
import "testing" | ||
import ( | ||
"database/sql" | ||
"testing" | ||
) | ||
|
||
func TestCompileQuery(t *testing.T) { | ||
table := []struct { | ||
|
@@ -56,3 +59,148 @@ func TestCompileQuery(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
type Test struct { | ||
t *testing.T | ||
} | ||
|
||
func (t Test) Error(err error, msg ...interface{}) { | ||
if err != nil { | ||
if len(msg) == 0 { | ||
t.t.Error(err) | ||
} else { | ||
t.t.Error(msg...) | ||
} | ||
} | ||
} | ||
|
||
func (t Test) Errorf(err error, format string, args ...interface{}) { | ||
if err != nil { | ||
t.t.Errorf(format, args...) | ||
} | ||
} | ||
|
||
func TestNamedQueries(t *testing.T) { | ||
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T) { | ||
loadDefaultFixture(db, t) | ||
test := Test{t} | ||
var ns *NamedStmt | ||
var err error | ||
|
||
// Check that invalid preparations fail | ||
ns, err = db.PrepareNamed("SELECT * FROM person WHERE first_name=:first:name") | ||
if err == nil { | ||
t.Error("Expected an error with invalid prepared statement.") | ||
} | ||
|
||
ns, err = db.PrepareNamed("invalid sql") | ||
if err == nil { | ||
t.Error("Expected an error with invalid prepared statement.") | ||
} | ||
|
||
// Check closing works as anticipated | ||
ns, err = db.PrepareNamed("SELECT * FROM person WHERE first_name=:first_name") | ||
test.Error(err) | ||
err = ns.Close() | ||
test.Error(err) | ||
|
||
ns, err = db.PrepareNamed(` | ||
SELECT first_name, last_name, email | ||
FROM person WHERE first_name=:first_name AND email=:email`) | ||
test.Error(err) | ||
|
||
// test Queryx w/ uses Query | ||
p := Person{FirstName: "Jason", LastName: "Moiron", Email: "[email protected]"} | ||
|
||
rows, err := ns.Queryx(p) | ||
test.Error(err) | ||
for rows.Next() { | ||
var p2 Person | ||
rows.StructScan(&p2) | ||
if p.FirstName != p2.FirstName { | ||
t.Error("got %s, expected %s", p.FirstName, p2.FirstName) | ||
} | ||
if p.LastName != p2.LastName { | ||
t.Error("got %s, expected %s", p.LastName, p2.LastName) | ||
} | ||
if p.Email != p2.Email { | ||
t.Error("got %s, expected %s", p.Email, p2.Email) | ||
} | ||
} | ||
|
||
// test Select | ||
people := make([]Person, 0, 5) | ||
err = ns.Select(&people, p) | ||
test.Error(err) | ||
|
||
if len(people) != 1 { | ||
t.Errorf("got %d results, expected %d", len(people), 1) | ||
} | ||
if p.FirstName != people[0].FirstName { | ||
t.Error("got %s, expected %s", p.FirstName, people[0].FirstName) | ||
} | ||
if p.LastName != people[0].LastName { | ||
t.Error("got %s, expected %s", p.LastName, people[0].LastName) | ||
} | ||
if p.Email != people[0].Email { | ||
t.Error("got %s, expected %s", p.Email, people[0].Email) | ||
} | ||
|
||
// test Exec | ||
ns, err = db.PrepareNamed(` | ||
INSERT INTO person (first_name, last_name, email) | ||
VALUES (:first_name, :last_name, :email)`) | ||
test.Error(err) | ||
|
||
js := Person{ | ||
FirstName: "Julien", | ||
LastName: "Savea", | ||
Email: "[email protected]", | ||
} | ||
_, err = ns.Exec(js) | ||
test.Error(err) | ||
|
||
// Make sure we can pull him out again | ||
p2 := Person{} | ||
db.Get(&p2, db.Rebind("SELECT * FROM person WHERE email=?"), js.Email) | ||
if p2.Email != js.Email { | ||
t.Errorf("expected %s, got %s", js.Email, p2.Email) | ||
} | ||
|
||
// test Txn NamedStmts | ||
tx := db.MustBegin() | ||
txns := tx.NamedStmt(ns) | ||
|
||
// We're going to add Steven in this txn | ||
sl := Person{ | ||
FirstName: "Steven", | ||
LastName: "Luatua", | ||
Email: "[email protected]", | ||
} | ||
|
||
_, err = txns.Exec(sl) | ||
test.Error(err) | ||
// then rollback... | ||
tx.Rollback() | ||
// looking for Steven after a rollback should fail | ||
err = db.Get(&p2, db.Rebind("SELECT * FROM person WHERE email=?"), sl.Email) | ||
if err != sql.ErrNoRows { | ||
t.Errorf("expected no rows error, got %v", err) | ||
} | ||
|
||
// now do the same, but commit | ||
tx = db.MustBegin() | ||
txns = tx.NamedStmt(ns) | ||
_, err = txns.Exec(sl) | ||
test.Error(err) | ||
tx.Commit() | ||
|
||
// looking for Steven after a Commit should succeed | ||
err = db.Get(&p2, db.Rebind("SELECT * FROM person WHERE email=?"), sl.Email) | ||
test.Error(err) | ||
if p2.Email != sl.Email { | ||
t.Errorf("expected %s, got %s", sl.Email, p2.Email) | ||
} | ||
|
||
}) | ||
} |
Oops, something went wrong.