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 | d78901d | 2006-01-05 23:42:50 +0000 | [diff] [blame^] | 20 | # $Id: hook.test,v 1.9 2006/01/05 23:42:52 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 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 95 | #---------------------------------------------------------------------------- |
| 96 | # Tests for the update-hook. |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 97 | # |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 98 | # 4.1.* - Very simple tests. Test that the update hook is invoked correctly |
| 99 | # for INSERT, DELETE and UPDATE statements, including DELETE |
| 100 | # statements with no WHERE clause. |
| 101 | # 4.2.* - Check that the update-hook is invoked for rows modified by trigger |
| 102 | # bodies. Also that the database name is correctly reported when |
| 103 | # an attached database is modified. |
| 104 | # 4.3.* - Do some sorting, grouping, compound queries, population and |
| 105 | # depopulation of indices, to make sure the update-hook is not |
| 106 | # invoked incorrectly. |
| 107 | # |
| 108 | |
| 109 | # Simple tests |
| 110 | do_test hook-4.1.1 { |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 111 | catchsql { |
| 112 | DROP TABLE t1; |
| 113 | } |
| 114 | execsql { |
| 115 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
| 116 | INSERT INTO t1 VALUES(1, 'one'); |
| 117 | INSERT INTO t1 VALUES(2, 'two'); |
| 118 | INSERT INTO t1 VALUES(3, 'three'); |
| 119 | } |
| 120 | db update_hook [list lappend ::update_hook] |
| 121 | } {} |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 122 | do_test hook-4.1.2 { |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 123 | execsql { |
| 124 | INSERT INTO t1 VALUES(4, 'four'); |
| 125 | DELETE FROM t1 WHERE b = 'two'; |
| 126 | UPDATE t1 SET b = '' WHERE a = 1 OR a = 3; |
drh | d78901d | 2006-01-05 23:42:50 +0000 | [diff] [blame^] | 127 | DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now) |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 128 | } |
| 129 | set ::update_hook |
| 130 | } [list \ |
| 131 | INSERT main t1 4 \ |
| 132 | DELETE main t1 2 \ |
| 133 | UPDATE main t1 1 \ |
| 134 | UPDATE main t1 3 \ |
| 135 | DELETE main t1 1 \ |
| 136 | DELETE main t1 3 \ |
| 137 | DELETE main t1 4 \ |
| 138 | ] |
| 139 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 140 | set ::update_hook {} |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 141 | do_test hook-4.2.1 { |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 142 | catchsql { |
| 143 | DROP TABLE t2; |
| 144 | } |
| 145 | execsql { |
| 146 | CREATE TABLE t2(c INTEGER PRIMARY KEY, d); |
| 147 | CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN |
| 148 | INSERT INTO t2 VALUES(new.a, new.b); |
| 149 | UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c; |
| 150 | DELETE FROM t2 WHERE new.a = c; |
| 151 | END; |
| 152 | } |
| 153 | } {} |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 154 | do_test hook-4.2.2 { |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 155 | execsql { |
| 156 | INSERT INTO t1 VALUES(1, 'one'); |
| 157 | INSERT INTO t1 VALUES(2, 'two'); |
| 158 | } |
| 159 | set ::update_hook |
| 160 | } [list \ |
| 161 | INSERT main t1 1 \ |
| 162 | INSERT main t2 1 \ |
| 163 | UPDATE main t2 1 \ |
| 164 | DELETE main t2 1 \ |
| 165 | INSERT main t1 2 \ |
| 166 | INSERT main t2 2 \ |
| 167 | UPDATE main t2 2 \ |
| 168 | DELETE main t2 2 \ |
| 169 | ] |
| 170 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 171 | # Triggers + ATTACH |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 172 | set ::update_hook {} |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 173 | do_test hook-4.2.3 { |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 174 | file delete -force test2.db |
| 175 | execsql { |
| 176 | ATTACH 'test2.db' AS aux; |
| 177 | CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b); |
| 178 | INSERT INTO aux.t3 SELECT * FROM t1; |
| 179 | UPDATE t3 SET b = 'two or so' WHERE a = 2; |
drh | d78901d | 2006-01-05 23:42:50 +0000 | [diff] [blame^] | 180 | DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now) |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 181 | } |
| 182 | set ::update_hook |
| 183 | } [list \ |
| 184 | INSERT aux t3 1 \ |
| 185 | INSERT aux t3 2 \ |
| 186 | UPDATE aux t3 2 \ |
| 187 | DELETE aux t3 1 \ |
| 188 | DELETE aux t3 2 \ |
| 189 | ] |
| 190 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 191 | # Test that other vdbe operations involving btree structures do not |
| 192 | # incorrectly invoke the update-hook. |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 193 | set ::update_hook {} |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 194 | do_test hook-4.3.1 { |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 195 | execsql { |
| 196 | DROP TRIGGER t1_trigger; |
| 197 | CREATE INDEX t1_i ON t1(b); |
| 198 | INSERT INTO t1 VALUES(3, 'three'); |
| 199 | UPDATE t1 SET b = ''; |
| 200 | DELETE FROM t1 WHERE a > 1; |
| 201 | } |
| 202 | set ::update_hook |
| 203 | } [list \ |
| 204 | INSERT main t1 3 \ |
| 205 | UPDATE main t1 1 \ |
| 206 | UPDATE main t1 2 \ |
| 207 | UPDATE main t1 3 \ |
| 208 | DELETE main t1 2 \ |
| 209 | DELETE main t1 3 \ |
| 210 | ] |
| 211 | set ::update_hook {} |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 212 | do_test hook-4.3.2 { |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 213 | execsql { |
| 214 | SELECT * FROM t1 UNION SELECT * FROM t3; |
| 215 | SELECT * FROM t1 UNION ALL SELECT * FROM t3; |
| 216 | SELECT * FROM t1 INTERSECT SELECT * FROM t3; |
| 217 | SELECT * FROM t1 EXCEPT SELECT * FROM t3; |
| 218 | SELECT * FROM t1 ORDER BY b; |
| 219 | SELECT * FROM t1 GROUP BY b; |
| 220 | } |
| 221 | set ::update_hook |
| 222 | } [list] |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 223 | db update_hook {} |
| 224 | # |
| 225 | #---------------------------------------------------------------------------- |
| 226 | |
| 227 | #---------------------------------------------------------------------------- |
| 228 | # Test the rollback-hook. The rollback-hook is a bit more complicated than |
| 229 | # either the commit or update hooks because a rollback can happen |
| 230 | # explicitly (an sql ROLLBACK statement) or implicitly (a constraint or |
| 231 | # error condition). |
| 232 | # |
| 233 | # hook-5.1.* - Test explicit rollbacks. |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 234 | # hook-5.2.* - Test implicit rollbacks caused by constraint failure. |
| 235 | # |
| 236 | # hook-5.3.* - Test implicit rollbacks caused by IO errors. |
| 237 | # hook-5.4.* - Test implicit rollbacks caused by malloc() failure. |
| 238 | # hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook |
| 239 | # not be called for these? |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 240 | # |
| 241 | |
| 242 | do_test hook-5.0 { |
| 243 | # Configure the rollback hook to increment global variable |
| 244 | # $::rollback_hook each time it is invoked. |
| 245 | set ::rollback_hook 0 |
| 246 | db rollback_hook [list incr ::rollback_hook] |
| 247 | } {} |
| 248 | |
| 249 | # Test explicit rollbacks. Not much can really go wrong here. |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 250 | # |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 251 | do_test hook-5.1.1 { |
| 252 | set ::rollback_hook 0 |
| 253 | execsql { |
| 254 | BEGIN; |
| 255 | ROLLBACK; |
| 256 | } |
| 257 | set ::rollback_hook |
| 258 | } {1} |
| 259 | |
| 260 | # Test implicit rollbacks caused by constraints. |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 261 | # |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 262 | do_test hook-5.2.1 { |
| 263 | set ::rollback_hook 0 |
| 264 | catchsql { |
| 265 | DROP TABLE t1; |
| 266 | CREATE TABLE t1(a PRIMARY KEY, b); |
| 267 | INSERT INTO t1 VALUES('one', 'I'); |
| 268 | INSERT INTO t1 VALUES('one', 'I'); |
| 269 | } |
| 270 | set ::rollback_hook |
| 271 | } {1} |
| 272 | do_test hook-5.2.2 { |
| 273 | # Check that the INSERT transaction above really was rolled back. |
| 274 | execsql { |
| 275 | SELECT count(*) FROM t1; |
| 276 | } |
| 277 | } {1} |
| 278 | |
| 279 | # |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 280 | # End rollback-hook testing. |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 281 | #---------------------------------------------------------------------------- |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 282 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 283 | finish_test |