drh | 8ce10ba | 2003-06-22 01:41:49 +0000 | [diff] [blame] | 1 | # 2003 June 21 |
| 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 SQLite library. |
| 12 | # |
| 13 | # This file implements tests for miscellanous features that were |
| 14 | # left out of other test files. |
| 15 | # |
danielk1977 | 4489f9b | 2005-01-20 02:17:01 +0000 | [diff] [blame^] | 16 | # $Id: misc2.test,v 1.19 2005/01/20 02:17:02 danielk1977 Exp $ |
drh | 8ce10ba | 2003-06-22 01:41:49 +0000 | [diff] [blame] | 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 21 | ifcapable {trigger} { |
drh | 8ce10ba | 2003-06-22 01:41:49 +0000 | [diff] [blame] | 22 | # Test for ticket #360 |
| 23 | # |
| 24 | do_test misc2-1.1 { |
| 25 | catchsql { |
| 26 | CREATE TABLE FOO(bar integer); |
| 27 | CREATE TRIGGER foo_insert BEFORE INSERT ON foo BEGIN |
| 28 | SELECT CASE WHEN (NOT new.bar BETWEEN 0 AND 20) |
| 29 | THEN raise(rollback, 'aiieee') END; |
| 30 | END; |
| 31 | INSERT INTO foo(bar) VALUES (1); |
| 32 | } |
drh | dc339ee | 2003-06-29 20:25:08 +0000 | [diff] [blame] | 33 | } {0 {}} |
| 34 | do_test misc2-1.2 { |
| 35 | catchsql { |
| 36 | INSERT INTO foo(bar) VALUES (111); |
| 37 | } |
drh | 8ce10ba | 2003-06-22 01:41:49 +0000 | [diff] [blame] | 38 | } {1 aiieee} |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 39 | } ;# endif trigger |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 40 | |
| 41 | # Make sure ROWID works on a view and a subquery. Ticket #364 |
| 42 | # |
| 43 | do_test misc2-2.1 { |
| 44 | execsql { |
| 45 | CREATE TABLE t1(a,b,c); |
| 46 | INSERT INTO t1 VALUES(1,2,3); |
drh | da808d5 | 2003-07-09 16:34:56 +0000 | [diff] [blame] | 47 | CREATE TABLE t2(a,b,c); |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 48 | INSERT INTO t2 VALUES(7,8,9); |
danielk1977 | d8702b4 | 2004-11-22 15:05:58 +0000 | [diff] [blame] | 49 | } |
| 50 | } {} |
| 51 | ifcapable view { |
| 52 | do_test misc2-2.2 { |
| 53 | execsql { |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 54 | SELECT rowid, * FROM (SELECT * FROM t1, t2); |
| 55 | } |
| 56 | } {{} 1 2 3 7 8 9} |
danielk1977 | d8702b4 | 2004-11-22 15:05:58 +0000 | [diff] [blame] | 57 | do_test misc2-2.3 { |
drh | d60ccc6 | 2003-06-24 10:39:46 +0000 | [diff] [blame] | 58 | execsql { |
| 59 | CREATE VIEW v1 AS SELECT * FROM t1, t2; |
| 60 | SELECT rowid, * FROM v1; |
| 61 | } |
| 62 | } {{} 1 2 3 7 8 9} |
danielk1977 | 0fa8ddb | 2004-11-22 08:43:32 +0000 | [diff] [blame] | 63 | } ;# ifcapable view |
drh | da808d5 | 2003-07-09 16:34:56 +0000 | [diff] [blame] | 64 | |
| 65 | # Check name binding precedence. Ticket #387 |
| 66 | # |
| 67 | do_test misc2-3.1 { |
| 68 | catchsql { |
| 69 | SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10 |
| 70 | } |
| 71 | } {1 {ambiguous column name: a}} |
drh | dc2d94d | 2003-07-27 17:16:06 +0000 | [diff] [blame] | 72 | |
| 73 | # Make sure 32-bit integer overflow is handled properly in queries. |
| 74 | # ticket #408 |
| 75 | # |
| 76 | do_test misc2-4.1 { |
| 77 | execsql { |
| 78 | INSERT INTO t1 VALUES(4000000000,'a','b'); |
| 79 | SELECT a FROM t1 WHERE a>1; |
| 80 | } |
| 81 | } {4000000000} |
| 82 | do_test misc2-4.2 { |
| 83 | execsql { |
| 84 | INSERT INTO t1 VALUES(2147483648,'b2','c2'); |
| 85 | INSERT INTO t1 VALUES(2147483647,'b3','c3'); |
| 86 | SELECT a FROM t1 WHERE a>2147483647; |
| 87 | } |
| 88 | } {4000000000 2147483648} |
| 89 | do_test misc2-4.3 { |
| 90 | execsql { |
| 91 | SELECT a FROM t1 WHERE a<2147483648; |
| 92 | } |
| 93 | } {1 2147483647} |
| 94 | do_test misc2-4.4 { |
| 95 | execsql { |
| 96 | SELECT a FROM t1 WHERE a<=2147483648; |
| 97 | } |
| 98 | } {1 2147483648 2147483647} |
| 99 | do_test misc2-4.5 { |
| 100 | execsql { |
| 101 | SELECT a FROM t1 WHERE a<10000000000; |
| 102 | } |
| 103 | } {1 4000000000 2147483648 2147483647} |
| 104 | do_test misc2-4.6 { |
| 105 | execsql { |
| 106 | SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1; |
| 107 | } |
| 108 | } {1 2147483647 2147483648 4000000000} |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 109 | |
| 110 | # There were some issues with expanding a SrcList object using a call |
| 111 | # to sqliteSrcListAppend() if the SrcList had previously been duplicated |
| 112 | # using a call to sqliteSrcListDup(). Ticket #416. The following test |
| 113 | # makes sure the problem has been fixed. |
| 114 | # |
danielk1977 | 0fa8ddb | 2004-11-22 08:43:32 +0000 | [diff] [blame] | 115 | ifcapable view { |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 116 | do_test misc2-5.1 { |
| 117 | execsql { |
| 118 | CREATE TABLE x(a,b); |
| 119 | CREATE VIEW y AS |
| 120 | SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a; |
| 121 | CREATE VIEW z AS |
| 122 | SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q; |
| 123 | SELECT * from z; |
| 124 | } |
| 125 | } {} |
danielk1977 | 0fa8ddb | 2004-11-22 08:43:32 +0000 | [diff] [blame] | 126 | } |
drh | 901afd4 | 2003-08-26 11:25:58 +0000 | [diff] [blame] | 127 | |
| 128 | # Make sure we can open a database with an empty filename. What this |
| 129 | # does is store the database in a temporary file that is deleted when |
| 130 | # the database is closed. Ticket #432. |
| 131 | # |
| 132 | do_test misc2-6.1 { |
| 133 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 134 | sqlite3 db {} |
drh | 901afd4 | 2003-08-26 11:25:58 +0000 | [diff] [blame] | 135 | execsql { |
| 136 | CREATE TABLE t1(a,b); |
| 137 | INSERT INTO t1 VALUES(1,2); |
| 138 | SELECT * FROM t1; |
| 139 | } |
| 140 | } {1 2} |
drh | e4c6169 | 2003-08-27 22:54:31 +0000 | [diff] [blame] | 141 | |
danielk1977 | 12b1300 | 2004-11-22 10:02:21 +0000 | [diff] [blame] | 142 | # Make sure we get an error message (not a segfault) on an attempt to |
| 143 | # update a table from within the callback of a select on that same |
| 144 | # table. |
| 145 | # |
| 146 | do_test misc2-7.1 { |
| 147 | db close |
| 148 | file delete -force test.db |
| 149 | sqlite3 db test.db |
| 150 | execsql { |
| 151 | CREATE TABLE t1(x); |
| 152 | INSERT INTO t1 VALUES(1); |
| 153 | } |
| 154 | set rc [catch { |
| 155 | db eval {SELECT rowid FROM t1} {} { |
| 156 | db eval "DELETE FROM t1 WHERE rowid=$rowid" |
| 157 | } |
| 158 | } msg] |
| 159 | lappend rc $msg |
| 160 | } {1 {database table is locked}} |
| 161 | do_test misc2-7.2 { |
| 162 | set rc [catch { |
| 163 | db eval {SELECT rowid FROM t1} {} { |
| 164 | db eval "INSERT INTO t1 VALUES(3)" |
| 165 | } |
| 166 | } msg] |
| 167 | lappend rc $msg |
| 168 | } {1 {database table is locked}} |
danielk1977 | 4489f9b | 2005-01-20 02:17:01 +0000 | [diff] [blame^] | 169 | ifcapable memorydb { |
| 170 | do_test misc2-7.3 { |
| 171 | sqlite3 db :memory: |
| 172 | execsql { |
| 173 | CREATE TABLE t1(x); |
| 174 | INSERT INTO t1 VALUES(1); |
danielk1977 | 12b1300 | 2004-11-22 10:02:21 +0000 | [diff] [blame] | 175 | } |
danielk1977 | 4489f9b | 2005-01-20 02:17:01 +0000 | [diff] [blame^] | 176 | set rc [catch { |
| 177 | db eval {SELECT rowid FROM t1} {} { |
| 178 | db eval "DELETE FROM t1 WHERE rowid=$rowid" |
| 179 | } |
| 180 | } msg] |
| 181 | lappend rc $msg |
| 182 | } {1 {database table is locked}} |
| 183 | do_test misc2-7.4 { |
| 184 | set rc [catch { |
| 185 | db eval {SELECT rowid FROM t1} {} { |
| 186 | db eval "INSERT INTO t1 VALUES(3)" |
| 187 | } |
| 188 | } msg] |
| 189 | lappend rc $msg |
| 190 | } {1 {database table is locked}} |
| 191 | } |
| 192 | |
| 193 | db close |
| 194 | file delete -force test.db |
| 195 | sqlite3 db test.db |
danielk1977 | 12b1300 | 2004-11-22 10:02:21 +0000 | [diff] [blame] | 196 | |
drh | 61b487d | 2003-09-12 02:08:14 +0000 | [diff] [blame] | 197 | # Ticket #453. If the SQL ended with "-", the tokenizer was calling that |
| 198 | # an incomplete token, which caused problem. The solution was to just call |
| 199 | # it a minus sign. |
| 200 | # |
| 201 | do_test misc2-8.1 { |
| 202 | catchsql {-} |
| 203 | } {1 {near "-": syntax error}} |
drh | 0f18bfa | 2003-12-10 01:31:21 +0000 | [diff] [blame] | 204 | |
| 205 | # Ticket #513. Make sure the VDBE stack does not grow on a 3-way join. |
| 206 | # |
| 207 | do_test misc2-9.1 { |
| 208 | execsql { |
| 209 | BEGIN; |
| 210 | CREATE TABLE counts(n INTEGER PRIMARY KEY); |
| 211 | INSERT INTO counts VALUES(0); |
| 212 | INSERT INTO counts VALUES(1); |
| 213 | INSERT INTO counts SELECT n+2 FROM counts; |
| 214 | INSERT INTO counts SELECT n+4 FROM counts; |
| 215 | INSERT INTO counts SELECT n+8 FROM counts; |
| 216 | COMMIT; |
| 217 | |
| 218 | CREATE TEMP TABLE x AS |
| 219 | SELECT dim1.n, dim2.n, dim3.n |
| 220 | FROM counts AS dim1, counts AS dim2, counts AS dim3 |
| 221 | WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10; |
| 222 | |
| 223 | SELECT count(*) FROM x; |
| 224 | } |
| 225 | } {1000} |
| 226 | do_test misc2-9.2 { |
| 227 | execsql { |
| 228 | DROP TABLE x; |
| 229 | CREATE TEMP TABLE x AS |
| 230 | SELECT dim1.n, dim2.n, dim3.n |
| 231 | FROM counts AS dim1, counts AS dim2, counts AS dim3 |
| 232 | WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6; |
| 233 | |
| 234 | SELECT count(*) FROM x; |
| 235 | } |
| 236 | } {1000} |
| 237 | do_test misc2-9.3 { |
| 238 | execsql { |
| 239 | DROP TABLE x; |
| 240 | CREATE TEMP TABLE x AS |
| 241 | SELECT dim1.n, dim2.n, dim3.n, dim4.n |
| 242 | FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4 |
| 243 | WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5; |
| 244 | |
| 245 | SELECT count(*) FROM x; |
| 246 | } |
| 247 | } [expr 5*5*5*5] |
drh | acf4ac9 | 2003-12-17 23:57:34 +0000 | [diff] [blame] | 248 | |
| 249 | finish_test |