sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2009 Nov 11 |
| 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 | # TESTRUNNER: shell |
| 12 | # |
| 13 | # The focus of this file is testing the CLI shell tool. |
| 14 | # |
| 15 | # $Id: shell2.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $ |
| 16 | # |
| 17 | |
| 18 | # Test plan: |
| 19 | # |
| 20 | # shell2-1.*: Misc. test of various tickets and reported errors. |
| 21 | # |
| 22 | set testdir [file dirname $argv0] |
| 23 | source $testdir/tester.tcl |
| 24 | set CLI [test_find_cli] |
| 25 | db close |
| 26 | forcedelete test.db test.db-journal test.db-wal |
| 27 | sqlite3 db test.db |
| 28 | |
| 29 | |
| 30 | #---------------------------------------------------------------------------- |
| 31 | # shell2-1.*: Misc. test of various tickets and reported errors. |
| 32 | # |
| 33 | |
| 34 | # Batch mode not creating databases. |
| 35 | # Reported on mailing list by Ken Zalewski. |
| 36 | # Ticket [aeff892c57]. |
| 37 | do_test shell2-1.1.1 { |
| 38 | forcedelete foo.db |
| 39 | set rc [ catchcmd "-batch foo.db" "CREATE TABLE t1(a);" ] |
| 40 | set fexist [file exist foo.db] |
| 41 | list $rc $fexist |
| 42 | } {{0 {}} 1} |
| 43 | |
| 44 | # Shell silently ignores extra parameters. |
| 45 | # Ticket [f5cb008a65]. |
| 46 | do_test shell2-1.2.1 { |
| 47 | catchcmdex {:memory: -list "select+3" "select+4"} |
| 48 | } {0 {3 |
| 49 | 4 |
| 50 | }} |
| 51 | |
| 52 | # Test a problem reported on the mailing list. The shell was at one point |
| 53 | # returning the generic SQLITE_ERROR message ("SQL error or missing database") |
| 54 | # instead of the "triggers nested too deep" message in the test below. |
| 55 | # |
| 56 | do_test shell2-1.3 { |
| 57 | catchcmd "-batch test.db" { |
| 58 | PRAGMA recursive_triggers = ON; |
| 59 | CREATE TABLE t5(a PRIMARY KEY, b, c); |
| 60 | INSERT INTO t5 VALUES(1, 2, 3); |
| 61 | CREATE TRIGGER au_tble AFTER UPDATE ON t5 BEGIN |
| 62 | UPDATE OR IGNORE t5 SET a = new.a, c = 10; |
| 63 | END; |
| 64 | |
| 65 | UPDATE OR REPLACE t5 SET a = 4 WHERE a = 1; |
| 66 | } |
| 67 | } {1 {Error near line 9: triggers nested too deep}} |
| 68 | |
| 69 | |
| 70 | |
| 71 | # Shell not echoing all commands with echo on. |
| 72 | # Ticket [eb620916be]. |
| 73 | |
| 74 | # Test with echo off |
| 75 | # NB. whitespace is important |
| 76 | do_test shell2-1.4.1 { |
| 77 | forcedelete foo.db |
| 78 | catchcmd "foo.db" {.mode batch |
| 79 | CREATE TABLE foo(a); |
| 80 | INSERT INTO foo(a) VALUES(1); |
| 81 | SELECT * FROM foo;} |
| 82 | } {0 1} |
| 83 | |
| 84 | # Test with echo on using command line option |
| 85 | # NB. whitespace is important |
| 86 | do_test shell2-1.4.2 { |
| 87 | forcedelete foo.db |
| 88 | catchcmd "-echo foo.db" {CREATE TABLE foo(a); |
| 89 | INSERT INTO foo(a) VALUES(1); |
| 90 | SELECT * FROM foo;} |
| 91 | } {0 {CREATE TABLE foo(a); |
| 92 | INSERT INTO foo(a) VALUES(1); |
| 93 | SELECT * FROM foo; |
| 94 | 1}} |
| 95 | |
| 96 | # Test with echo on using dot command |
| 97 | # NB. whitespace is important |
| 98 | do_test shell2-1.4.3 { |
| 99 | forcedelete foo.db |
| 100 | catchcmd "foo.db" { |
| 101 | .mode batch |
| 102 | .echo ON |
| 103 | CREATE TABLE foo(a); |
| 104 | INSERT INTO foo(a) VALUES(1); |
| 105 | SELECT * FROM foo;} |
| 106 | } {0 {CREATE TABLE foo(a); |
| 107 | INSERT INTO foo(a) VALUES(1); |
| 108 | SELECT * FROM foo; |
| 109 | 1}} |
| 110 | |
| 111 | # Test with echo on using dot command and |
| 112 | # turning off mid- processing. |
| 113 | # NB. whitespace is important |
| 114 | do_test shell2-1.4.4 { |
| 115 | forcedelete foo.db |
| 116 | catchcmd "foo.db" { |
| 117 | .mode batch |
| 118 | .echo ON |
| 119 | CREATE TABLE foo(a); |
| 120 | .echo OFF |
| 121 | INSERT INTO foo(a) VALUES(1); |
| 122 | SELECT * FROM foo;} |
| 123 | } {0 {CREATE TABLE foo(a); |
| 124 | .echo OFF |
| 125 | 1}} |
| 126 | |
| 127 | # Test with echo on using dot command and |
| 128 | # multiple commands per line. |
| 129 | # NB. whitespace is important |
| 130 | do_test shell2-1.4.5 { |
| 131 | forcedelete foo.db |
| 132 | catchcmdex "foo.db" { |
| 133 | .mode batch |
| 134 | .echo ON |
| 135 | CREATE TABLE foo1(a); |
| 136 | INSERT INTO foo1(a) VALUES(1); |
| 137 | CREATE TABLE foo2(b); |
| 138 | INSERT INTO foo2(b) VALUES(1); |
| 139 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 140 | INSERT INTO foo1(a) VALUES(2); INSERT INTO foo2(b) VALUES(2); |
| 141 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 142 | } |
| 143 | } {0 {CREATE TABLE foo1(a); |
| 144 | INSERT INTO foo1(a) VALUES(1); |
| 145 | CREATE TABLE foo2(b); |
| 146 | INSERT INTO foo2(b) VALUES(1); |
| 147 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 148 | 1 |
| 149 | 1 |
| 150 | INSERT INTO foo1(a) VALUES(2); INSERT INTO foo2(b) VALUES(2); |
| 151 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 152 | 1 |
| 153 | 2 |
| 154 | 1 |
| 155 | 2 |
| 156 | }} |
| 157 | |
| 158 | # Test with echo on and headers on using dot command and |
| 159 | # multiple commands per line. |
| 160 | # NB. whitespace is important |
| 161 | do_test shell2-1.4.6 { |
| 162 | forcedelete foo.db |
| 163 | catchcmdex "foo.db" { |
| 164 | .mode batch |
| 165 | .echo ON |
| 166 | .headers ON |
| 167 | CREATE TABLE foo1(a); |
| 168 | INSERT INTO foo1(a) VALUES(1); |
| 169 | CREATE TABLE foo2(b); |
| 170 | INSERT INTO foo2(b) VALUES(1); |
| 171 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 172 | INSERT INTO foo1(a) VALUES(2); INSERT INTO foo2(b) VALUES(2); |
| 173 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 174 | } |
| 175 | } {0 {.headers ON |
| 176 | CREATE TABLE foo1(a); |
| 177 | INSERT INTO foo1(a) VALUES(1); |
| 178 | CREATE TABLE foo2(b); |
| 179 | INSERT INTO foo2(b) VALUES(1); |
| 180 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 181 | a |
| 182 | 1 |
| 183 | b |
| 184 | 1 |
| 185 | INSERT INTO foo1(a) VALUES(2); INSERT INTO foo2(b) VALUES(2); |
| 186 | SELECT * FROM foo1; SELECT * FROM foo2; |
| 187 | a |
| 188 | 1 |
| 189 | 2 |
| 190 | b |
| 191 | 1 |
| 192 | 2 |
| 193 | }} |
| 194 | |
| 195 | # Test for rejection of incomplete input at EOF. |
| 196 | # Reported at https://sqlite.org/forum/forumpost/718f489a43be3197 |
| 197 | do_test shell2-1.4.7 { |
| 198 | catchcmd ":memory:" { |
| 199 | SELECT 'unclosed;} |
| 200 | } {1 {Parse error near line 2: unrecognized token: "'unclosed;" |
| 201 | SELECT 'unclosed; |
| 202 | ^--- error here}} |
| 203 | |
| 204 | # Verify that safe mode rejects certain UDFs |
| 205 | # Reported at https://sqlite.org/forum/forumpost/07beac8056151b2f |
| 206 | do_test shell2-1.4.8 { |
| 207 | catchcmd "-safe :memory:" { |
| 208 | SELECT edit('DoNotCare');} |
| 209 | } {1 {line 2: cannot use the edit() function in safe mode}} |
| 210 | do_test shell2-1.4.9 { |
| 211 | catchcmd "-safe :memory:" { |
| 212 | SELECT writefile('DoNotCare', x'');} |
| 213 | } {1 {Parse error near line 2: no such function: writefile |
| 214 | SELECT writefile('DoNotCare', x''); |
| 215 | ^--- error here}} |
| 216 | |
| 217 | # Verify that .clone handles sequence table. |
| 218 | # See https://sqlite.org/forum/forumpost/71ff9e6c4c |
| 219 | do_test shell2-1.4.9 { |
| 220 | forcedelete clone.db |
| 221 | set res [catchcmd :memory: [string trim { |
| 222 | .mode batch |
| 223 | CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT); |
| 224 | INSERT INTO t VALUES (1),(2); |
| 225 | .clone clone.db |
| 226 | .open clone.db |
| 227 | SELECT max(seq) FROM sqlite_sequence;}]] |
| 228 | } {0 2} |
| 229 | |
| 230 | ifcapable vtab { |
| 231 | # Verify that generate_series stays sane near 64-bit range boundaries. |
| 232 | # See overflow report at https://sqlite.org/forum/forumpost/5d34ce5280 |
| 233 | do_test shell2-1.4.10 { |
| 234 | set res [catchcmd :memory: [string trim { |
| 235 | .mode batch |
| 236 | SELECT * FROM generate_series(9223372036854775807,9223372036854775807,1); |
| 237 | SELECT * FROM generate_series(9223372036854775807,9223372036854775807,-1); |
| 238 | SELECT avg(value),min(value),max(value) FROM generate_series( |
| 239 | -9223372036854775808,9223372036854775807,1085102592571150095); |
| 240 | SELECT * FROM generate_series(-9223372036854775808,9223372036854775807, |
| 241 | 9223372036854775807); |
| 242 | SELECT value FROM generate_series(-4611686018427387904, |
| 243 | 4611686018427387904, 4611686018427387904) ORDER BY value DESC; |
| 244 | SELECT * FROM generate_series(0,-2,-1); |
| 245 | SELECT * FROM generate_series(0,-2); |
| 246 | SELECT * FROM generate_series(0,2) LIMIT 3;}]] |
| 247 | } {0 {9223372036854775807 |
| 248 | 9223372036854775807 |
| 249 | -0.5|-9223372036854775808|9223372036854775807 |
| 250 | -9223372036854775808 |
| 251 | -1 |
| 252 | 9223372036854775806 |
| 253 | 4611686018427387904 |
| 254 | 0 |
| 255 | -4611686018427387904 |
| 256 | 0 |
| 257 | -1 |
| 258 | -2 |
| 259 | 0 |
| 260 | 1 |
| 261 | 2}} |
| 262 | do_test shell2-1.4.10b { |
| 263 | set res [catchcmd :memory: [string trim { |
| 264 | .mode tty |
| 265 | |
| 266 | SELECT * FROM generate_series(9223372036854775807,9223372036854775807,1); |
| 267 | SELECT * FROM generate_series(9223372036854775807,9223372036854775807,-1); |
| 268 | SELECT avg(value),min(value),max(value) FROM generate_series( |
| 269 | -9223372036854775808,9223372036854775807,1085102592571150095); |
| 270 | SELECT * FROM generate_series(-9223372036854775808,9223372036854775807, |
| 271 | 9223372036854775807); |
| 272 | SELECT value FROM generate_series(-4611686018427387904, |
| 273 | 4611686018427387904, 4611686018427387904) ORDER BY value DESC; |
| 274 | SELECT * FROM generate_series(0,-2,-1); |
| 275 | SELECT * FROM generate_series(0,-2); |
| 276 | SELECT * FROM generate_series(0,2) LIMIT 3;}]] |
| 277 | } {0 { |
| 278 | ╭─────────────────────╮ |
| 279 | │ value │ |
| 280 | ╞═════════════════════╡ |
| 281 | │ 9223372036854775807 │ |
| 282 | ╰─────────────────────╯ |
| 283 | ╭─────────────────────╮ |
| 284 | │ value │ |
| 285 | ╞═════════════════════╡ |
| 286 | │ 9223372036854775807 │ |
| 287 | ╰─────────────────────╯ |
| 288 | ╭────────────┬──────────────────────┬─────────────────────╮ |
| 289 | │ avg(value) │ min(value) │ max(value) │ |
| 290 | ╞════════════╪══════════════════════╪═════════════════════╡ |
| 291 | │ -0.5 │ -9223372036854775808 │ 9223372036854775807 │ |
| 292 | ╰────────────┴──────────────────────┴─────────────────────╯ |
| 293 | ╭──────────────────────╮ |
| 294 | │ value │ |
| 295 | ╞══════════════════════╡ |
| 296 | │ -9223372036854775808 │ |
| 297 | │ -1 │ |
| 298 | │ 9223372036854775806 │ |
| 299 | ╰──────────────────────╯ |
| 300 | ╭──────────────────────╮ |
| 301 | │ value │ |
| 302 | ╞══════════════════════╡ |
| 303 | │ 4611686018427387904 │ |
| 304 | │ 0 │ |
| 305 | │ -4611686018427387904 │ |
| 306 | ╰──────────────────────╯ |
| 307 | ╭───────╮ |
| 308 | │ value │ |
| 309 | ╞═══════╡ |
| 310 | │ 0 │ |
| 311 | │ -1 │ |
| 312 | │ -2 │ |
| 313 | ╰───────╯ |
| 314 | ╭───────╮ |
| 315 | │ value │ |
| 316 | ╘═══════╛ |
| 317 | ╭───────╮ |
| 318 | │ value │ |
| 319 | ╞═══════╡ |
| 320 | │ 0 │ |
| 321 | │ 1 │ |
| 322 | │ 2 │ |
| 323 | ╰───────╯}} |
| 324 | } ;# ifcapable vtab |
| 325 | |
| 326 | ifcapable vtab { |
| 327 | # Bug discovered while messing around, .import hangs with |
| 328 | # bit 7 set in column separator. |
| 329 | do_test shell2-1.4.11 { |
| 330 | forcedelete dummy.csv |
| 331 | set df [open dummy.csv w] |
| 332 | puts $df dog,cat |
| 333 | close $df |
| 334 | set res [catchcmd :memory: [string trim { |
| 335 | CREATE TABLE t(line text); |
| 336 | .mode ascii -colsep "\377" -rowsep "\n" |
| 337 | .import dummy.csv t |
| 338 | SELECT count(*) FROM t;}]] |
| 339 | } {1 {0 |
| 340 | Error: .import column separator must be ASCII}} |
| 341 | } ;# ifcapable vtab |
| 342 | |
| 343 | # Bug from forum post 7cbe081746dd3803 |
| 344 | # Keywords as column names were producing an error message. |
| 345 | do_test shell2-1.4.12 { |
| 346 | set res [catchcmd :memory: [string trim {.mode batch |
| 347 | CREATE TABLE "group"("order" text); |
| 348 | INSERT INTO "group" VALUES ('ABC'); |
| 349 | .sha3sum}]] |
| 350 | } {0 ca08bc02b7e95c7df431a3a4b1cc0f8d8743914793473f55b5558e03} |
| 351 | |
| 352 | #------------------------------------------------------------------------- |
| 353 | |
| 354 | foreach {tn hexdump expect} { |
| 355 | 0 { |
| 356 | | size 8192 pagesize 4096 filename my.db |
| 357 | | page 1 offset 0 |
| 358 | | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. |
| 359 | | 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ |
| 360 | | 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ |
| 361 | | 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ |
| 362 | | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ |
| 363 | | 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ |
| 364 | | 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ |
| 365 | | 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C |
| 366 | | 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) |
| 367 | | page 2 offset 4096 |
| 368 | | 0: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ |
| 369 | | 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ |
| 370 | | 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello |
| 371 | | end my.db |
| 372 | } |
| 373 | {0 {}} |
| 374 | |
| 375 | 1 { |
| 376 | | size 2147483647 pagesize 4096 filename my.db |
| 377 | | page 1 offset 0 |
| 378 | | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. |
| 379 | | 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ |
| 380 | | 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ |
| 381 | | 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ |
| 382 | | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ |
| 383 | | 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ |
| 384 | | 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ |
| 385 | | 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C |
| 386 | | 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) |
| 387 | | page 2 offset 4096 |
| 388 | | 0: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ |
| 389 | | 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ |
| 390 | | 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello |
| 391 | | end my.db |
| 392 | } |
| 393 | {1 {Error: out of memory}} |
| 394 | |
| 395 | 2 { |
| 396 | | size 8192 pagesize 4096 filename my.db |
| 397 | | page 1 offset 2147483647 |
| 398 | | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. |
| 399 | | 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ |
| 400 | | 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ |
| 401 | | 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ |
| 402 | | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ |
| 403 | | 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ |
| 404 | | 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ |
| 405 | | 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C |
| 406 | | 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) |
| 407 | | page 2 offset 4096 |
| 408 | | 0: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ |
| 409 | | 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ |
| 410 | | 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello |
| 411 | | end my.db |
| 412 | } |
| 413 | {0 {}} |
| 414 | |
| 415 | 3 { |
| 416 | | size 8192 pagesize 4096 filename my.db |
| 417 | | page 1 offset 0 |
| 418 | | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. |
| 419 | | 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ |
| 420 | | 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ |
| 421 | | 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ |
| 422 | | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ |
| 423 | | 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ |
| 424 | | 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ |
| 425 | | 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C |
| 426 | | 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) |
| 427 | | page 2 offset 4096 |
| 428 | | 2147483647: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ |
| 429 | | 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ |
| 430 | | 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello |
| 431 | | end my.db |
| 432 | } |
| 433 | {0 {}} |
| 434 | |
| 435 | } { |
| 436 | set fd [open dump.txt w] |
| 437 | puts $fd [string trim $hexdump] |
| 438 | close $fd |
| 439 | do_test shell2-2.$tn.1 { |
| 440 | set rc [ catchcmd "" ".open --hexdb dump.txt"] |
| 441 | } $expect |
| 442 | } |
| 443 | |
| 444 | |
| 445 | finish_test |