2013-06-21 00:52:38 +04:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-11-19 18:05:48 +04:00
|
|
|
func TestCorruptDbErrors(t *testing.T) {
|
2013-06-21 00:52:38 +04:00
|
|
|
dirName, err := ioutil.TempDir("", "sqlite3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dirName)
|
|
|
|
|
|
|
|
dbFileName := path.Join(dirName, "test.db")
|
|
|
|
f, err := os.Create(dbFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
f.Write([]byte{1, 2, 3, 4, 5})
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
db, err := sql.Open("sqlite3", dbFileName)
|
|
|
|
if err == nil {
|
|
|
|
_, err = db.Exec("drop table foo")
|
|
|
|
}
|
2013-11-19 17:53:24 +04:00
|
|
|
|
|
|
|
sqliteErr := err.(Error)
|
|
|
|
if sqliteErr.Code != ErrNotADB {
|
2013-06-21 00:52:38 +04:00
|
|
|
t.Error("wrong error code for corrupted DB")
|
|
|
|
}
|
2013-09-03 14:36:33 +04:00
|
|
|
if err.Error() == "" {
|
|
|
|
t.Error("wrong error string for corrupted DB")
|
|
|
|
}
|
2013-06-21 00:52:38 +04:00
|
|
|
db.Close()
|
|
|
|
}
|
2013-11-19 18:05:48 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|