From 72bb737cf97755013eafb2a2ab56d09cdeee3e8b Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 19 Nov 2013 14:05:48 +0000 Subject: [PATCH] Add unit test for enhanced error reporting Add a test to check for a useful response for a SQL query that cannot be executed due to a constraint failure --- error_test.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/error_test.go b/error_test.go index 0f92881..bf7bb3c 100644 --- a/error_test.go +++ b/error_test.go @@ -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) @@ -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) + } +}