sl@0
|
1 |
# 2004 Jan 14
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
# This file implements regression tests for TCL interface to the
|
sl@0
|
12 |
# SQLite library.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# The focus of the tests in this file is the following interface:
|
sl@0
|
15 |
#
|
sl@0
|
16 |
# sqlite_commit_hook (tests hook-1..hook-3 inclusive)
|
sl@0
|
17 |
# sqlite_update_hook (tests hook-4-*)
|
sl@0
|
18 |
# sqlite_rollback_hook (tests hook-5.*)
|
sl@0
|
19 |
#
|
sl@0
|
20 |
# $Id: hook.test,v 1.13 2008/01/19 20:11:26 drh Exp $
|
sl@0
|
21 |
|
sl@0
|
22 |
set testdir [file dirname $argv0]
|
sl@0
|
23 |
source $testdir/tester.tcl
|
sl@0
|
24 |
|
sl@0
|
25 |
do_test hook-1.2 {
|
sl@0
|
26 |
db commit_hook
|
sl@0
|
27 |
} {}
|
sl@0
|
28 |
|
sl@0
|
29 |
|
sl@0
|
30 |
do_test hook-3.1 {
|
sl@0
|
31 |
set commit_cnt 0
|
sl@0
|
32 |
proc commit_hook {} {
|
sl@0
|
33 |
incr ::commit_cnt
|
sl@0
|
34 |
return 0
|
sl@0
|
35 |
}
|
sl@0
|
36 |
db commit_hook ::commit_hook
|
sl@0
|
37 |
db commit_hook
|
sl@0
|
38 |
} {::commit_hook}
|
sl@0
|
39 |
do_test hook-3.2 {
|
sl@0
|
40 |
set commit_cnt
|
sl@0
|
41 |
} {0}
|
sl@0
|
42 |
do_test hook-3.3 {
|
sl@0
|
43 |
execsql {
|
sl@0
|
44 |
CREATE TABLE t2(a,b);
|
sl@0
|
45 |
}
|
sl@0
|
46 |
set commit_cnt
|
sl@0
|
47 |
} {1}
|
sl@0
|
48 |
do_test hook-3.4 {
|
sl@0
|
49 |
execsql {
|
sl@0
|
50 |
INSERT INTO t2 VALUES(1,2);
|
sl@0
|
51 |
INSERT INTO t2 SELECT a+1, b+1 FROM t2;
|
sl@0
|
52 |
INSERT INTO t2 SELECT a+2, b+2 FROM t2;
|
sl@0
|
53 |
}
|
sl@0
|
54 |
set commit_cnt
|
sl@0
|
55 |
} {4}
|
sl@0
|
56 |
do_test hook-3.5 {
|
sl@0
|
57 |
set commit_cnt {}
|
sl@0
|
58 |
proc commit_hook {} {
|
sl@0
|
59 |
set ::commit_cnt [execsql {SELECT * FROM t2}]
|
sl@0
|
60 |
return 0
|
sl@0
|
61 |
}
|
sl@0
|
62 |
execsql {
|
sl@0
|
63 |
INSERT INTO t2 VALUES(5,6);
|
sl@0
|
64 |
}
|
sl@0
|
65 |
set commit_cnt
|
sl@0
|
66 |
} {1 2 2 3 3 4 4 5 5 6}
|
sl@0
|
67 |
do_test hook-3.6 {
|
sl@0
|
68 |
set commit_cnt {}
|
sl@0
|
69 |
proc commit_hook {} {
|
sl@0
|
70 |
set ::commit_cnt [execsql {SELECT * FROM t2}]
|
sl@0
|
71 |
return 1
|
sl@0
|
72 |
}
|
sl@0
|
73 |
catchsql {
|
sl@0
|
74 |
INSERT INTO t2 VALUES(6,7);
|
sl@0
|
75 |
}
|
sl@0
|
76 |
} {1 {constraint failed}}
|
sl@0
|
77 |
do_test hook-3.7 {
|
sl@0
|
78 |
set ::commit_cnt
|
sl@0
|
79 |
} {1 2 2 3 3 4 4 5 5 6 6 7}
|
sl@0
|
80 |
do_test hook-3.8 {
|
sl@0
|
81 |
execsql {SELECT * FROM t2}
|
sl@0
|
82 |
} {1 2 2 3 3 4 4 5 5 6}
|
sl@0
|
83 |
|
sl@0
|
84 |
# Test turnning off the commit hook
|
sl@0
|
85 |
#
|
sl@0
|
86 |
do_test hook-3.9 {
|
sl@0
|
87 |
db commit_hook {}
|
sl@0
|
88 |
set ::commit_cnt {}
|
sl@0
|
89 |
execsql {
|
sl@0
|
90 |
INSERT INTO t2 VALUES(7,8);
|
sl@0
|
91 |
}
|
sl@0
|
92 |
set ::commit_cnt
|
sl@0
|
93 |
} {}
|
sl@0
|
94 |
|
sl@0
|
95 |
#----------------------------------------------------------------------------
|
sl@0
|
96 |
# Tests for the update-hook.
|
sl@0
|
97 |
#
|
sl@0
|
98 |
# 4.1.* - Very simple tests. Test that the update hook is invoked correctly
|
sl@0
|
99 |
# for INSERT, DELETE and UPDATE statements, including DELETE
|
sl@0
|
100 |
# statements with no WHERE clause.
|
sl@0
|
101 |
# 4.2.* - Check that the update-hook is invoked for rows modified by trigger
|
sl@0
|
102 |
# bodies. Also that the database name is correctly reported when
|
sl@0
|
103 |
# an attached database is modified.
|
sl@0
|
104 |
# 4.3.* - Do some sorting, grouping, compound queries, population and
|
sl@0
|
105 |
# depopulation of indices, to make sure the update-hook is not
|
sl@0
|
106 |
# invoked incorrectly.
|
sl@0
|
107 |
#
|
sl@0
|
108 |
|
sl@0
|
109 |
# Simple tests
|
sl@0
|
110 |
do_test hook-4.1.1 {
|
sl@0
|
111 |
catchsql {
|
sl@0
|
112 |
DROP TABLE t1;
|
sl@0
|
113 |
}
|
sl@0
|
114 |
execsql {
|
sl@0
|
115 |
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
|
sl@0
|
116 |
INSERT INTO t1 VALUES(1, 'one');
|
sl@0
|
117 |
INSERT INTO t1 VALUES(2, 'two');
|
sl@0
|
118 |
INSERT INTO t1 VALUES(3, 'three');
|
sl@0
|
119 |
}
|
sl@0
|
120 |
db update_hook [list lappend ::update_hook]
|
sl@0
|
121 |
} {}
|
sl@0
|
122 |
do_test hook-4.1.2 {
|
sl@0
|
123 |
execsql {
|
sl@0
|
124 |
INSERT INTO t1 VALUES(4, 'four');
|
sl@0
|
125 |
DELETE FROM t1 WHERE b = 'two';
|
sl@0
|
126 |
UPDATE t1 SET b = '' WHERE a = 1 OR a = 3;
|
sl@0
|
127 |
DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now)
|
sl@0
|
128 |
}
|
sl@0
|
129 |
set ::update_hook
|
sl@0
|
130 |
} [list \
|
sl@0
|
131 |
INSERT main t1 4 \
|
sl@0
|
132 |
DELETE main t1 2 \
|
sl@0
|
133 |
UPDATE main t1 1 \
|
sl@0
|
134 |
UPDATE main t1 3 \
|
sl@0
|
135 |
DELETE main t1 1 \
|
sl@0
|
136 |
DELETE main t1 3 \
|
sl@0
|
137 |
DELETE main t1 4 \
|
sl@0
|
138 |
]
|
sl@0
|
139 |
|
sl@0
|
140 |
# Update hook is not invoked for changes to sqlite_master
|
sl@0
|
141 |
#
|
sl@0
|
142 |
do_test hook-4.1.3 {
|
sl@0
|
143 |
set ::update_hook {}
|
sl@0
|
144 |
execsql {
|
sl@0
|
145 |
CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT RAISE(IGNORE); END;
|
sl@0
|
146 |
}
|
sl@0
|
147 |
set ::update_hook
|
sl@0
|
148 |
} {}
|
sl@0
|
149 |
do_test hook-4.1.4 {
|
sl@0
|
150 |
set ::update_hook {}
|
sl@0
|
151 |
execsql {
|
sl@0
|
152 |
DROP TRIGGER r1;
|
sl@0
|
153 |
}
|
sl@0
|
154 |
set ::update_hook
|
sl@0
|
155 |
} {}
|
sl@0
|
156 |
|
sl@0
|
157 |
|
sl@0
|
158 |
set ::update_hook {}
|
sl@0
|
159 |
ifcapable trigger {
|
sl@0
|
160 |
do_test hook-4.2.1 {
|
sl@0
|
161 |
catchsql {
|
sl@0
|
162 |
DROP TABLE t2;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
execsql {
|
sl@0
|
165 |
CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
|
sl@0
|
166 |
CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN
|
sl@0
|
167 |
INSERT INTO t2 VALUES(new.a, new.b);
|
sl@0
|
168 |
UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c;
|
sl@0
|
169 |
DELETE FROM t2 WHERE new.a = c;
|
sl@0
|
170 |
END;
|
sl@0
|
171 |
}
|
sl@0
|
172 |
} {}
|
sl@0
|
173 |
do_test hook-4.2.2 {
|
sl@0
|
174 |
execsql {
|
sl@0
|
175 |
INSERT INTO t1 VALUES(1, 'one');
|
sl@0
|
176 |
INSERT INTO t1 VALUES(2, 'two');
|
sl@0
|
177 |
}
|
sl@0
|
178 |
set ::update_hook
|
sl@0
|
179 |
} [list \
|
sl@0
|
180 |
INSERT main t1 1 \
|
sl@0
|
181 |
INSERT main t2 1 \
|
sl@0
|
182 |
UPDATE main t2 1 \
|
sl@0
|
183 |
DELETE main t2 1 \
|
sl@0
|
184 |
INSERT main t1 2 \
|
sl@0
|
185 |
INSERT main t2 2 \
|
sl@0
|
186 |
UPDATE main t2 2 \
|
sl@0
|
187 |
DELETE main t2 2 \
|
sl@0
|
188 |
]
|
sl@0
|
189 |
} else {
|
sl@0
|
190 |
execsql {
|
sl@0
|
191 |
INSERT INTO t1 VALUES(1, 'one');
|
sl@0
|
192 |
INSERT INTO t1 VALUES(2, 'two');
|
sl@0
|
193 |
}
|
sl@0
|
194 |
}
|
sl@0
|
195 |
|
sl@0
|
196 |
# Update-hook + ATTACH
|
sl@0
|
197 |
set ::update_hook {}
|
sl@0
|
198 |
ifcapable attach {
|
sl@0
|
199 |
do_test hook-4.2.3 {
|
sl@0
|
200 |
file delete -force test2.db
|
sl@0
|
201 |
execsql {
|
sl@0
|
202 |
ATTACH 'test2.db' AS aux;
|
sl@0
|
203 |
CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b);
|
sl@0
|
204 |
INSERT INTO aux.t3 SELECT * FROM t1;
|
sl@0
|
205 |
UPDATE t3 SET b = 'two or so' WHERE a = 2;
|
sl@0
|
206 |
DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now)
|
sl@0
|
207 |
}
|
sl@0
|
208 |
set ::update_hook
|
sl@0
|
209 |
} [list \
|
sl@0
|
210 |
INSERT aux t3 1 \
|
sl@0
|
211 |
INSERT aux t3 2 \
|
sl@0
|
212 |
UPDATE aux t3 2 \
|
sl@0
|
213 |
DELETE aux t3 1 \
|
sl@0
|
214 |
DELETE aux t3 2 \
|
sl@0
|
215 |
]
|
sl@0
|
216 |
}
|
sl@0
|
217 |
|
sl@0
|
218 |
ifcapable trigger {
|
sl@0
|
219 |
execsql {
|
sl@0
|
220 |
DROP TRIGGER t1_trigger;
|
sl@0
|
221 |
}
|
sl@0
|
222 |
}
|
sl@0
|
223 |
|
sl@0
|
224 |
# Test that other vdbe operations involving btree structures do not
|
sl@0
|
225 |
# incorrectly invoke the update-hook.
|
sl@0
|
226 |
set ::update_hook {}
|
sl@0
|
227 |
do_test hook-4.3.1 {
|
sl@0
|
228 |
execsql {
|
sl@0
|
229 |
CREATE INDEX t1_i ON t1(b);
|
sl@0
|
230 |
INSERT INTO t1 VALUES(3, 'three');
|
sl@0
|
231 |
UPDATE t1 SET b = '';
|
sl@0
|
232 |
DELETE FROM t1 WHERE a > 1;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
set ::update_hook
|
sl@0
|
235 |
} [list \
|
sl@0
|
236 |
INSERT main t1 3 \
|
sl@0
|
237 |
UPDATE main t1 1 \
|
sl@0
|
238 |
UPDATE main t1 2 \
|
sl@0
|
239 |
UPDATE main t1 3 \
|
sl@0
|
240 |
DELETE main t1 2 \
|
sl@0
|
241 |
DELETE main t1 3 \
|
sl@0
|
242 |
]
|
sl@0
|
243 |
set ::update_hook {}
|
sl@0
|
244 |
ifcapable compound&&attach {
|
sl@0
|
245 |
do_test hook-4.3.2 {
|
sl@0
|
246 |
execsql {
|
sl@0
|
247 |
SELECT * FROM t1 UNION SELECT * FROM t3;
|
sl@0
|
248 |
SELECT * FROM t1 UNION ALL SELECT * FROM t3;
|
sl@0
|
249 |
SELECT * FROM t1 INTERSECT SELECT * FROM t3;
|
sl@0
|
250 |
SELECT * FROM t1 EXCEPT SELECT * FROM t3;
|
sl@0
|
251 |
SELECT * FROM t1 ORDER BY b;
|
sl@0
|
252 |
SELECT * FROM t1 GROUP BY b;
|
sl@0
|
253 |
}
|
sl@0
|
254 |
set ::update_hook
|
sl@0
|
255 |
} [list]
|
sl@0
|
256 |
}
|
sl@0
|
257 |
db update_hook {}
|
sl@0
|
258 |
#
|
sl@0
|
259 |
#----------------------------------------------------------------------------
|
sl@0
|
260 |
|
sl@0
|
261 |
#----------------------------------------------------------------------------
|
sl@0
|
262 |
# Test the rollback-hook. The rollback-hook is a bit more complicated than
|
sl@0
|
263 |
# either the commit or update hooks because a rollback can happen
|
sl@0
|
264 |
# explicitly (an sql ROLLBACK statement) or implicitly (a constraint or
|
sl@0
|
265 |
# error condition).
|
sl@0
|
266 |
#
|
sl@0
|
267 |
# hook-5.1.* - Test explicit rollbacks.
|
sl@0
|
268 |
# hook-5.2.* - Test implicit rollbacks caused by constraint failure.
|
sl@0
|
269 |
#
|
sl@0
|
270 |
# hook-5.3.* - Test implicit rollbacks caused by IO errors.
|
sl@0
|
271 |
# hook-5.4.* - Test implicit rollbacks caused by malloc() failure.
|
sl@0
|
272 |
# hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook
|
sl@0
|
273 |
# not be called for these?
|
sl@0
|
274 |
#
|
sl@0
|
275 |
|
sl@0
|
276 |
do_test hook-5.0 {
|
sl@0
|
277 |
# Configure the rollback hook to increment global variable
|
sl@0
|
278 |
# $::rollback_hook each time it is invoked.
|
sl@0
|
279 |
set ::rollback_hook 0
|
sl@0
|
280 |
db rollback_hook [list incr ::rollback_hook]
|
sl@0
|
281 |
} {}
|
sl@0
|
282 |
|
sl@0
|
283 |
# Test explicit rollbacks. Not much can really go wrong here.
|
sl@0
|
284 |
#
|
sl@0
|
285 |
do_test hook-5.1.1 {
|
sl@0
|
286 |
set ::rollback_hook 0
|
sl@0
|
287 |
execsql {
|
sl@0
|
288 |
BEGIN;
|
sl@0
|
289 |
ROLLBACK;
|
sl@0
|
290 |
}
|
sl@0
|
291 |
set ::rollback_hook
|
sl@0
|
292 |
} {1}
|
sl@0
|
293 |
|
sl@0
|
294 |
# Test implicit rollbacks caused by constraints.
|
sl@0
|
295 |
#
|
sl@0
|
296 |
do_test hook-5.2.1 {
|
sl@0
|
297 |
set ::rollback_hook 0
|
sl@0
|
298 |
catchsql {
|
sl@0
|
299 |
DROP TABLE t1;
|
sl@0
|
300 |
CREATE TABLE t1(a PRIMARY KEY, b);
|
sl@0
|
301 |
INSERT INTO t1 VALUES('one', 'I');
|
sl@0
|
302 |
INSERT INTO t1 VALUES('one', 'I');
|
sl@0
|
303 |
}
|
sl@0
|
304 |
set ::rollback_hook
|
sl@0
|
305 |
} {1}
|
sl@0
|
306 |
do_test hook-5.2.2 {
|
sl@0
|
307 |
# Check that the INSERT transaction above really was rolled back.
|
sl@0
|
308 |
execsql {
|
sl@0
|
309 |
SELECT count(*) FROM t1;
|
sl@0
|
310 |
}
|
sl@0
|
311 |
} {1}
|
sl@0
|
312 |
|
sl@0
|
313 |
#
|
sl@0
|
314 |
# End rollback-hook testing.
|
sl@0
|
315 |
#----------------------------------------------------------------------------
|
sl@0
|
316 |
|
sl@0
|
317 |
finish_test
|