2019-11-18 09:03:31 +00:00
|
|
|
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
2014-08-18 07:56:31 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-08-18 08:00:59 +00:00
|
|
|
|
2019-11-18 09:03:31 +00:00
|
|
|
// +build cgo
|
|
|
|
|
2013-06-20 20:52:38 +00:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-01-29 00:02:22 +00:00
|
|
|
func TestSimpleError(t *testing.T) {
|
2014-01-29 00:17:47 +00:00
|
|
|
e := ErrError.Error()
|
2017-11-18 15:09:25 +00:00
|
|
|
if e != "SQL logic error or missing database" && e != "SQL logic error" {
|
|
|
|
t.Error("wrong error code: " + e)
|
2014-01-29 00:02:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 14:05:48 +00:00
|
|
|
func TestCorruptDbErrors(t *testing.T) {
|
2013-06-20 20:52:38 +00: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 13:53:24 +00:00
|
|
|
|
2017-03-21 00:14:48 +00:00
|
|
|
sqliteErr := err.(Error)
|
2013-11-19 13:53:24 +00:00
|
|
|
if sqliteErr.Code != ErrNotADB {
|
2013-06-20 20:52:38 +00:00
|
|
|
t.Error("wrong error code for corrupted DB")
|
|
|
|
}
|
2013-09-03 10:36:33 +00:00
|
|
|
if err.Error() == "" {
|
|
|
|
t.Error("wrong error string for corrupted DB")
|
|
|
|
}
|
2013-06-20 20:52:38 +00:00
|
|
|
db.Close()
|
|
|
|
}
|
2013-11-19 14:05:48 +00: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)
|
|
|
|
}
|
2014-04-01 12:01:19 +00:00
|
|
|
defer db.Close()
|
2013-11-19 14:05:48 +00:00
|
|
|
|
2014-04-01 12:01:19 +00:00
|
|
|
_, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)")
|
2013-11-19 14:05:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const expectedErr = "table Foo already exists"
|
2014-04-01 12:01:19 +00:00
|
|
|
_, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)")
|
2013-11-19 14:05:48 +00:00
|
|
|
if err.Error() != expectedErr {
|
|
|
|
t.Errorf("Unexpected error: %s, expected %s", err.Error(), expectedErr)
|
|
|
|
}
|
2014-04-01 12:01:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtendedErrorCodes_ForeignKey(t *testing.T) {
|
|
|
|
dirName, err := ioutil.TempDir("", "sqlite3-err")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("PRAGMA foreign_keys=ON;")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("PRAGMA foreign_keys=ON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(`CREATE TABLE Foo (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
value INTEGER NOT NULL,
|
|
|
|
ref INTEGER NULL REFERENCES Foo (id),
|
|
|
|
UNIQUE(value)
|
|
|
|
);`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (100, 100);")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("No error!")
|
|
|
|
} else {
|
2017-03-21 00:14:48 +00:00
|
|
|
sqliteErr := err.(Error)
|
2014-04-01 12:01:19 +00:00
|
|
|
if sqliteErr.Code != ErrConstraint {
|
|
|
|
t.Errorf("Wrong basic error code: %d != %d",
|
|
|
|
sqliteErr.Code, ErrConstraint)
|
|
|
|
}
|
|
|
|
if sqliteErr.ExtendedCode != ErrConstraintForeignKey {
|
|
|
|
t.Errorf("Wrong extended error code: %d != %d",
|
|
|
|
sqliteErr.ExtendedCode, ErrConstraintForeignKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtendedErrorCodes_NotNull(t *testing.T) {
|
|
|
|
dirName, err := ioutil.TempDir("", "sqlite3-err")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("PRAGMA foreign_keys=ON;")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("PRAGMA foreign_keys=ON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(`CREATE TABLE Foo (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
value INTEGER NOT NULL,
|
|
|
|
ref INTEGER NULL REFERENCES Foo (id),
|
|
|
|
UNIQUE(value)
|
|
|
|
);`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Creating first row: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Retrieving last insert id: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO Foo (ref) VALUES (?);", id)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("No error!")
|
|
|
|
} else {
|
2017-03-21 00:14:48 +00:00
|
|
|
sqliteErr := err.(Error)
|
2014-04-01 12:01:19 +00:00
|
|
|
if sqliteErr.Code != ErrConstraint {
|
|
|
|
t.Errorf("Wrong basic error code: %d != %d",
|
|
|
|
sqliteErr.Code, ErrConstraint)
|
|
|
|
}
|
|
|
|
if sqliteErr.ExtendedCode != ErrConstraintNotNull {
|
|
|
|
t.Errorf("Wrong extended error code: %d != %d",
|
|
|
|
sqliteErr.ExtendedCode, ErrConstraintNotNull)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtendedErrorCodes_Unique(t *testing.T) {
|
|
|
|
dirName, err := ioutil.TempDir("", "sqlite3-err")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec("PRAGMA foreign_keys=ON;")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("PRAGMA foreign_keys=ON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(`CREATE TABLE Foo (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
value INTEGER NOT NULL,
|
|
|
|
ref INTEGER NULL REFERENCES Foo (id),
|
|
|
|
UNIQUE(value)
|
|
|
|
);`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Creating first row: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Retrieving last insert id: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (?, 100);", id)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("No error!")
|
|
|
|
} else {
|
2017-03-21 00:14:48 +00:00
|
|
|
sqliteErr := err.(Error)
|
2014-04-01 12:01:19 +00:00
|
|
|
if sqliteErr.Code != ErrConstraint {
|
|
|
|
t.Errorf("Wrong basic error code: %d != %d",
|
|
|
|
sqliteErr.Code, ErrConstraint)
|
|
|
|
}
|
|
|
|
if sqliteErr.ExtendedCode != ErrConstraintUnique {
|
|
|
|
t.Errorf("Wrong extended error code: %d != %d",
|
|
|
|
sqliteErr.ExtendedCode, ErrConstraintUnique)
|
|
|
|
}
|
2015-01-26 09:36:21 +00:00
|
|
|
extended := sqliteErr.Code.Extend(3).Error()
|
|
|
|
expected := "constraint failed"
|
|
|
|
if extended != expected {
|
|
|
|
t.Errorf("Wrong basic error code: %q != %q",
|
|
|
|
extended, expected)
|
|
|
|
}
|
2014-04-01 12:01:19 +00:00
|
|
|
}
|
2019-12-17 06:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestError_SystemErrno(t *testing.T) {
|
|
|
|
_, n, _ := Version()
|
|
|
|
if n < 3012000 {
|
|
|
|
t.Skip("sqlite3_system_errno requires sqlite3 >= 3.12.0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// open a non-existent database in read-only mode so we get an IO error.
|
|
|
|
db, err := sql.Open("sqlite3", "file:nonexistent.db?mode=ro")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
2014-04-01 12:01:19 +00:00
|
|
|
|
2019-12-17 06:58:28 +00:00
|
|
|
err = db.Ping()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error pinging read-only non-existent database, but got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
serr, ok := err.(Error)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected error to be of type Error, but got %[1]T %[1]v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if serr.SystemErrno == 0 {
|
|
|
|
t.Fatal("expected SystemErrno to be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !os.IsNotExist(serr.SystemErrno) {
|
|
|
|
t.Errorf("expected SystemErrno to be a not exists error, but got %v", serr.SystemErrno)
|
|
|
|
}
|
2013-11-19 14:05:48 +00:00
|
|
|
}
|