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