Skip to content

Commit

Permalink
Add unit test for enhanced error reporting
Browse files Browse the repository at this point in the history
Add a test to check for a useful response for a SQL
query that cannot be executed due to a constraint failure
  • Loading branch information
robertknight committed Nov 19, 2013
1 parent e072975 commit 72bb737
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion error_test.go
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

func TestFailures(t *testing.T) {
func TestCorruptDbErrors(t *testing.T) {
dirName, err := ioutil.TempDir("", "sqlite3")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -37,3 +37,28 @@ func TestFailures(t *testing.T) {
}
db.Close()
}

func TestSqlLogicErrors(t *testing.T) {
dirName, err := ioutil.TempDir("", "sqlite3")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dirName)

dbFileName := path.Join(dirName, "test.db")
db, err := sql.Open("sqlite3", dbFileName)
if err != nil {
t.Error(err)
}

_, err = db.Exec("CREATE TABLE Foo (id INT PRIMARY KEY)")
if err != nil {
t.Error(err)
}

const expectedErr = "table Foo already exists"
_, err = db.Exec("CREATE TABLE Foo (id INT PRIMARY KEY)")
if err.Error() != expectedErr {
t.Errorf("Unexpected error: %s, expected %s", err.Error(), expectedErr)
}
}

0 comments on commit 72bb737

Please sign in to comment.