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