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