Debian Codegraph @

error_test.go

raw
1 // Copyright (C) 2019 Yasuhiro Matsumoto <[email protected]>.
2 //
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
5
6 //go:build cgo
7 // +build cgo
8
9 package sqlite3
10
11 import (
12 "database/sql"
13 "io/ioutil"
14 "os"
15 "path"
16 "testing"
17 )
18
19 func TestSimpleError(t *testing.T) {
20 e := ErrError.Error()
21 if e != "SQL logic error or missing database" && e != "SQL logic error" {
22 t.Error("wrong error code: " + e)
23 }
24 }
25
26 func TestCorruptDbErrors(t *testing.T) {
27 dirName, err := ioutil.TempDir("", "sqlite3")
28 if err != nil {
29 t.Fatal(err)
30 }
31 defer os.RemoveAll(dirName)
32
33 dbFileName := path.Join(dirName, "test.db")
34 f, err := os.Create(dbFileName)
35 if err != nil {
36 t.Error(err)
37 }
38 f.Write([]byte{1, 2, 3, 4, 5})
39 f.Close()
40
41 db, err := sql.Open("sqlite3", dbFileName)
42 if err == nil {
43 _, err = db.Exec("drop table foo")
44 }
45
46 sqliteErr, ok := err.(Error)
47 if !ok {
48 t.Fatal(err)
49 }
50 if sqliteErr.Code != ErrNotADB {
51 t.Error("wrong error code for corrupted DB")
52 }
53 if err.Error() == "" {
54 t.Error("wrong error string for corrupted DB")
55 }
56 db.Close()
57 }
58
59 func TestSqlLogicErrors(t *testing.T) {
60 dirName, err := ioutil.TempDir("", "sqlite3")
61 if err != nil {
62 t.Fatal(err)
63 }
64 defer os.RemoveAll(dirName)
65
66 dbFileName := path.Join(dirName, "test.db")
67 db, err := sql.Open("sqlite3", dbFileName)
68 if err != nil {
69 t.Error(err)
70 }
71 defer db.Close()
72
73 _, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)")
74 if err != nil {
75 t.Error(err)
76 }
77
78 const expectedErr = "table Foo already exists"
79 _, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)")
80 if err.Error() != expectedErr {
81 t.Errorf("Unexpected error: %s, expected %s", err.Error(), expectedErr)
82 }
83
84 }
85
86 func TestExtendedErrorCodes_ForeignKey(t *testing.T) {
87 dirName, err := ioutil.TempDir("", "sqlite3-err")
88 if err != nil {
89 t.Fatal(err)
90 }
91 defer os.RemoveAll(dirName)
92
93 dbFileName := path.Join(dirName, "test.db")
94 db, err := sql.Open("sqlite3", dbFileName)
95 if err != nil {
96 t.Error(err)
97 }
98 defer db.Close()
99
100 _, err = db.Exec("PRAGMA foreign_keys=ON;")
101 if err != nil {
102 t.Errorf("PRAGMA foreign_keys=ON: %v", err)
103 }
104
105 _, err = db.Exec(`CREATE TABLE Foo (
106 id INTEGER PRIMARY KEY AUTOINCREMENT,
107 value INTEGER NOT NULL,
108 ref INTEGER NULL REFERENCES Foo (id),
109 UNIQUE(value)
110 );`)
111 if err != nil {
112 t.Error(err)
113 }
114
115 _, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (100, 100);")
116 if err == nil {
117 t.Error("No error!")
118 } else {
119 sqliteErr := err.(Error)
120 if sqliteErr.Code != ErrConstraint {
121 t.Errorf("Wrong basic error code: %d != %d",
122 sqliteErr.Code, ErrConstraint)
123 }
124 if sqliteErr.ExtendedCode != ErrConstraintForeignKey {
125 t.Errorf("Wrong extended error code: %d != %d",
126 sqliteErr.ExtendedCode, ErrConstraintForeignKey)
127 }
128 }
129
130 }
131
132 func TestExtendedErrorCodes_NotNull(t *testing.T) {
133 dirName, err := ioutil.TempDir("", "sqlite3-err")
134 if err != nil {
135 t.Fatal(err)
136 }
137 defer os.RemoveAll(dirName)
138
139 dbFileName := path.Join(dirName, "test.db")
140 db, err := sql.Open("sqlite3", dbFileName)
141 if err != nil {
142 t.Error(err)
143 }
144 defer db.Close()
145
146 _, err = db.Exec("PRAGMA foreign_keys=ON;")
147 if err != nil {
148 t.Errorf("PRAGMA foreign_keys=ON: %v", err)
149 }
150
151 _, err = db.Exec(`CREATE TABLE Foo (
152 id INTEGER PRIMARY KEY AUTOINCREMENT,
153 value INTEGER NOT NULL,
154 ref INTEGER NULL REFERENCES Foo (id),
155 UNIQUE(value)
156 );`)
157 if err != nil {
158 t.Error(err)
159 }
160
161 res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);")
162 if err != nil {
163 t.Fatalf("Creating first row: %v", err)
164 }
165
166 id, err := res.LastInsertId()
167 if err != nil {
168 t.Fatalf("Retrieving last insert id: %v", err)
169 }
170
171 _, err = db.Exec("INSERT INTO Foo (ref) VALUES (?);", id)
172 if err == nil {
173 t.Error("No error!")
174 } else {
175 sqliteErr := err.(Error)
176 if sqliteErr.Code != ErrConstraint {
177 t.Errorf("Wrong basic error code: %d != %d",
178 sqliteErr.Code, ErrConstraint)
179 }
180 if sqliteErr.ExtendedCode != ErrConstraintNotNull {
181 t.Errorf("Wrong extended error code: %d != %d",
182 sqliteErr.ExtendedCode, ErrConstraintNotNull)
183 }
184 }
185
186 }
187
188 func TestExtendedErrorCodes_Unique(t *testing.T) {
189 dirName, err := ioutil.TempDir("", "sqlite3-err")
190 if err != nil {
191 t.Fatal(err)
192 }
193 defer os.RemoveAll(dirName)
194
195 dbFileName := path.Join(dirName, "test.db")
196 db, err := sql.Open("sqlite3", dbFileName)
197 if err != nil {
198 t.Error(err)
199 }
200 defer db.Close()
201
202 _, err = db.Exec("PRAGMA foreign_keys=ON;")
203 if err != nil {
204 t.Errorf("PRAGMA foreign_keys=ON: %v", err)
205 }
206
207 _, err = db.Exec(`CREATE TABLE Foo (
208 id INTEGER PRIMARY KEY AUTOINCREMENT,
209 value INTEGER NOT NULL,
210 ref INTEGER NULL REFERENCES Foo (id),
211 UNIQUE(value)
212 );`)
213 if err != nil {
214 t.Error(err)
215 }
216
217 res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);")
218 if err != nil {
219 t.Fatalf("Creating first row: %v", err)
220 }
221
222 id, err := res.LastInsertId()
223 if err != nil {
224 t.Fatalf("Retrieving last insert id: %v", err)
225 }
226
227 _, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (?, 100);", id)
228 if err == nil {
229 t.Error("No error!")
230 } else {
231 sqliteErr := err.(Error)
232 if sqliteErr.Code != ErrConstraint {
233 t.Errorf("Wrong basic error code: %d != %d",
234 sqliteErr.Code, ErrConstraint)
235 }
236 if sqliteErr.ExtendedCode != ErrConstraintUnique {
237 t.Errorf("Wrong extended error code: %d != %d",
238 sqliteErr.ExtendedCode, ErrConstraintUnique)
239 }
240 extended := sqliteErr.Code.Extend(3).Error()
241 expected := "constraint failed"
242 if extended != expected {
243 t.Errorf("Wrong basic error code: %q != %q",
244 extended, expected)
245 }
246 }
247 }
248
249 func TestError_SystemErrno(t *testing.T) {
250 _, n, _ := Version()
251 if n < 3012000 {
252 t.Skip("sqlite3_system_errno requires sqlite3 >= 3.12.0")
253 }
254
255 // open a non-existent database in read-only mode so we get an IO error.
256 db, err := sql.Open("sqlite3", "file:nonexistent.db?mode=ro")
257 if err != nil {
258 t.Fatal(err)
259 }
260 defer db.Close()
261
262 err = db.Ping()
263 if err == nil {
264 t.Fatal("expected error pinging read-only non-existent database, but got nil")
265 }
266
267 serr, ok := err.(Error)
268 if !ok {
269 t.Fatalf("expected error to be of type Error, but got %[1]T %[1]v", err)
270 }
271
272 if serr.SystemErrno == 0 {
273 t.Fatal("expected SystemErrno to be set")
274 }
275
276 if !os.IsNotExist(serr.SystemErrno) {
277 t.Errorf("expected SystemErrno to be a not exists error, but got %v", serr.SystemErrno)
278 }
279 }