interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / shell5.test

669 lines · 16755 bytes Raw Blame History
1# 2010 August 4
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# These tests are specific to the .import command.
15#
16# $Id: shell5.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $
17#
18
19# Test plan:
20#
21# shell5-1.*: Basic tests specific to the ".import" command.
22#
23set testdir [file dirname $argv0]
24source $testdir/tester.tcl
25set CLI [test_cli_invocation]
26db close
27forcedelete test.db test.db-journal test.db-wal
28
29#----------------------------------------------------------------------------
30# Test cases shell5-1.*: Basic handling of the .import and .separator commands.
31#
32
33# .import FILE TABLE Import data from FILE into TABLE
34do_test shell5-1.1.1 {
35 catchcmd "test.db" ".import"
36} {/1 .line 1: Missing FILE argument.*/}
37do_test shell5-1.1.2 {
38 catchcmd "test.db" ".import FOO"
39} {/1 .line 1: Missing TABLE argument.*/}
40do_test shell5-1.1.3 {
41 # too many arguments
42 catchcmd "test.db" ".import FOO BAR BAD"
43} {1 {line 1: .import FOO BAR BAD
44line 1: ^--- unknown argument}}
45
46# .separator STRING Change separator used by output mode and .import
47do_test shell5-1.2.1 {
48 catchcmd "test.db" ".separator"
49} {1 {Usage: .separator COL ?ROW?}}
50do_test shell5-1.2.2 {
51 catchcmd "test.db" ".separator ONE"
52} {0 {}}
53do_test shell5-1.2.3 {
54 catchcmd "test.db" ".separator ONE TWO"
55} {0 {}}
56do_test shell5-1.2.4 {
57 # too many arguments
58 catchcmd "test.db" ".separator ONE TWO THREE"
59} {1 {Usage: .separator COL ?ROW?}}
60
61# column separator should default to "|"
62do_test shell5-1.3.1.1 {
63 set res [catchcmd "test.db" ".mode list\n.show"]
64 list [regexp {colseparator: \"\|\"} $res]
65} {1}
66
67# row separator should default to "\n"
68do_test shell5-1.3.1.2 {
69 set res [catchcmd "test.db" ".mode list\n.show"]
70 list [regexp {rowseparator: \"\\n\"} $res]
71} {1}
72
73# set separator to different value.
74# check that .show reports new value
75do_test shell5-1.3.2 {
76 set res [catchcmd "test.db" {.separator ,
77.show}]
78 list [regexp {separator: \",\"} $res]
79} {1}
80
81# import file doesn't exist
82do_test shell5-1.4.1 {
83 forcedelete FOO
84 set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
85.import FOO t1}]
86} {1 {line 2: cannot open "FOO"}}
87
88# the remainder of these test cases require virtual tables.
89#
90ifcapable !vtab {
91 puts "Skipping subsequent tests due to SQLITE_OMIT_VIRTUALTABLE"
92 finish_test
93 return
94}
95
96# empty import file
97do_test shell5-1.4.2 {
98 forcedelete shell5.csv
99 set in [open shell5.csv w]
100 close $in
101 set res [catchcmd ":memory:" {
102.mode list
103ATTACH 'test.db' AS test;
104.import -schema test shell5.csv t1
105SELECT COUNT(*) FROM test.t1;}]
106} {0 0}
107
108# import file with 1 row, 1 column (expecting 2 cols)
109do_test shell5-1.4.3 {
110 set in [open shell5.csv w]
111 puts $in "1"
112 close $in
113 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
114.import -schema test shell5.csv t1}]
115} {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}}
116
117# import file with 1 row, 3 columns (expecting 2 cols)
118do_test shell5-1.4.4 {
119 set in [open shell5.csv w]
120 puts $in "1|2|3"
121 close $in
122 set res [catchcmd ":memory: --list" {ATTACH 'test.db' AS test;
123.import --schema test shell5.csv t1}]
124} {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}}
125
126# import file with 1 row, 2 columns
127do_test shell5-1.4.5 {
128 set in [open shell5.csv w]
129 puts $in "1|2"
130 close $in
131 set res [catchcmd "test.db -list" {DELETE FROM t1;
132.import shell5.csv t1
133SELECT COUNT(*) FROM t1;}]
134} {0 1}
135
136# import file with 2 rows, 2 columns
137# note we end up with 3 rows because of the 1 row
138# imported above.
139do_test shell5-1.4.6 {
140 set in [open shell5.csv w]
141 puts $in "2|3"
142 puts $in "3|4"
143 close $in
144 set res [catchcmd ":memory:" {
145.mode list
146ATTACH 'test.db' AS test;
147.import -schema test shell5.csv t1
148SELECT COUNT(*) FROM test.t1;}]
149} {0 3}
150
151# import file with 1 row, 2 columns, using a comma
152do_test shell5-1.4.7 {
153 set in [open shell5.csv w]
154 puts $in "4,5"
155 close $in
156 set res [catchcmd ":memory:" {
157.mode list
158ATTACH 'test.db' AS test;
159.separator ,
160.import --schema test shell5.csv t1
161SELECT COUNT(*) FROM test.t1;}]
162} {0 4}
163
164# import file with 1 row, 2 columns, text data
165do_test shell5-1.4.8.1 {
166 set in [open shell5.csv w]
167 puts $in "5|Now is the time for all good men to come to the aid of their country."
168 close $in
169 set res [catchcmd "test.db" {.mode list
170.import shell5.csv t1
171SELECT COUNT(*) FROM t1;}]
172} {0 5}
173
174do_test shell5-1.4.8.2 {
175 catchcmd "test.db -list" {SELECT b FROM t1 WHERE a='5';}
176} {0 {Now is the time for all good men to come to the aid of their country.}}
177
178# import file with 1 row, 2 columns, quoted text data
179# note that currently sqlite doesn't support quoted fields, and
180# imports the entire field, quotes and all.
181do_test shell5-1.4.9.1 {
182 set in [open shell5.csv w]
183 puts $in "6|'Now is the time for all good men to come to the aid of their country.'"
184 close $in
185 set res [catchcmd "test.db -list" {.import shell5.csv t1
186SELECT COUNT(*) FROM t1;}]
187} {0 6}
188
189do_test shell5-1.4.9.2 {
190 catchcmd "test.db -list" {SELECT b FROM t1 WHERE a='6';}
191} {0 {'Now is the time for all good men to come to the aid of their country.'}}
192
193# import file with 1 row, 2 columns, quoted text data
194do_test shell5-1.4.10.1 {
195 set in [open shell5.csv w]
196 puts $in "7|\"Now is the time for all good men to come to the aid of their country.\""
197 close $in
198 set res [catchcmd "test.db -list" {.import shell5.csv t1
199SELECT COUNT(*) FROM t1;}]
200} {0 7}
201
202do_test shell5-1.4.10.2 {
203 catchcmd "test.db -list" {SELECT b FROM t1 WHERE a='7';}
204} {0 {Now is the time for all good men to come to the aid of their country.}}
205
206# import file with 2 rows, 2 columns and an initial BOM
207#
208do_test shell5-1.4.11 {
209 set in [open shell5.csv wb]
210 puts -nonewline $in "\xef\xbb\xbf"
211 puts $in "2|3"
212 puts $in "4|5"
213 close $in
214 set res [catchcmd "test.db -list" {CREATE TABLE t2(x INT, y INT);
215.import shell5.csv t2
216.mode quote
217.header on
218SELECT * FROM t2;}]
219 string map {\n | \n\r |} $res
220} {0 {'x','y'|2,3|4,5}}
221
222# import file with 2 rows, 2 columns or text with an initial BOM
223#
224do_test shell5-1.4.12 {
225 set in [open shell5.csv wb]
226 puts $in "\xef\xbb\xbf\"two\"|3"
227 puts $in "4|5"
228 close $in
229 set res [catchcmd "test.db -list" {DELETE FROM t2;
230.import shell5.csv t2
231.mode quote
232.header on
233SELECT * FROM t2;}]
234 string map {\n | \n\r |} $res
235} {0 {'x','y'|'two',3|4,5}}
236
237# check importing very long field
238do_test shell5-1.5.1 {
239 set str [string repeat X 999]
240 set in [open shell5.csv w]
241 puts $in "8|$str"
242 close $in
243 set res [catchcmd "test.db -list" {.import shell5.csv t1
244SELECT length(b) FROM t1 WHERE a='8';}]
245} {0 999}
246
247# try importing into a table with a large number of columns.
248# This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
249set cols 999
250do_test shell5-1.6.1 {
251 set data {}
252 for {set i 1} {$i<$cols} {incr i} {
253 append data "c$i|"
254 }
255 append data "c$cols\n";
256 for {set i 1} {$i<$cols} {incr i} {
257 append data "$i|"
258 }
259 append data "$cols"
260 set in [open shell5.csv w]
261 puts $in $data
262 close $in
263 set res [catchcmd "test.db -list" {DROP TABLE IF EXISTS t2;
264.import shell5.csv t2
265SELECT COUNT(*) FROM t2;}]
266} {0 1}
267
268# try importing a large number of rows
269set rows 9999
270do_test shell5-1.7.1 {
271 set in [open shell5.csv w]
272 puts $in a
273 for {set i 1} {$i<=$rows} {incr i} {
274 puts $in $i
275 }
276 close $in
277 set res [catchcmd "test.db" {.mode csv
278.import shell5.csv t3
279.mode quote
280SELECT COUNT(*) FROM t3;}]
281} [list 0 $rows]
282
283# Import from a pipe. (Unix only, as it requires "awk")
284if {$tcl_platform(platform)=="unix"} {
285 do_test shell5-1.8 {
286 forcedelete test.db
287 catchcmd test.db {.mode csv
288.import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1
289SELECT * FROM t1;}
290 } {0 {1,"this is 1"
2912,"this is 2"
2923,"this is 3"
2934,"this is 4"
2945,"this is 5"}}
295}
296
297# Import columns containing quoted strings
298do_test shell5-1.9 {
299 set out [open shell5.csv w]
300 fconfigure $out -translation lf
301 puts $out {1,"",11}
302 puts $out {2,"x",22}
303 puts $out {3,"""",33}
304 puts $out {4,"hello",44}
305 puts $out "5,55,\"\"\r"
306 puts $out {6,66,"x"}
307 puts $out {7,77,""""}
308 puts $out {8,88,"hello"}
309 puts $out {"",9,99}
310 puts $out {"x",10,110}
311 puts $out {"""",11,121}
312 puts $out {"hello",12,132}
313 close $out
314 catch {db close}
315 forcedelete test.db
316 catchcmd test.db {.mode csv
317 CREATE TABLE t1(a,b,c);
318.import shell5.csv t1
319 }
320 sqlite3 db test.db
321 db eval {SELECT *, '|' FROM t1 ORDER BY rowid}
322} {1 {} 11 | 2 x 22 | 3 {"} 33 | 4 hello 44 | 5 55 {} | 6 66 x | 7 77 {"} | 8 88 hello | {} 9 99 | x 10 110 | {"} 11 121 | hello 12 132 |}
323db close
324
325# Import columns containing quoted strings
326do_test shell5-1.10 {
327 set out [open shell5.csv w]
328 fconfigure $out -translation lf
329 puts $out {column1,column2,column3,column4}
330 puts $out "field1,field2,\"x3 \"\"\r\ndata\"\" 3\",field4"
331 puts $out "x1,x2,\"x3 \"\"\ndata\"\" 3\",x4"
332 close $out
333 forcedelete test.db
334 catchcmd test.db {.mode csv
335 CREATE TABLE t1(a,b,c,d);
336.import shell5.csv t1
337 }
338 sqlite3 db test.db
339 db eval {SELECT hex(c) FROM t1 ORDER BY rowid}
340} {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033}
341
342# The --escape option
343#
344do_test shell5-1.10.1 {
345 set out [open shell5.csv w]
346 fconfigure $out -translation lf
347 puts $out {column1,column2,column3,column4}
348 puts $out "x1,x2%\"x3,\"x3\\\"data\\\"3\",x4"
349 close $out
350 db close
351 forcedelete test.db
352 catchcmd test.db {
353 CREATE TABLE t1(a,b,c,d);
354.import --csv --qesc \\ --esc % shell5.csv t1
355 }
356 sqlite3 db test.db
357 db eval {SELECT b, c FROM t1 ORDER BY rowid}
358} {column2 column3 x2\"x3 x3\"data\"3}
359
360# Blank last column with \r\n line endings.
361do_test shell5-1.11 {
362 set out [open shell5.csv w]
363 fconfigure $out -translation binary
364 puts $out "column1,column2,column3\r"
365 puts $out "a,b, \r"
366 puts $out "x,y,\r"
367 puts $out "p,q,r\r"
368 close $out
369 catch {db close}
370 forcedelete test.db
371 catchcmd test.db {.mode csv
372.import shell5.csv t1
373 }
374 sqlite3 db test.db
375 db eval {SELECT *, '|' FROM t1}
376} {a b { } | x y {} | p q r |}
377db close
378
379#----------------------------------------------------------------------------
380#
381reset_db
382sqlite3 db test.db
383do_test shell5-2.1 {
384 set fd [open shell5.csv w]
385 puts $fd ",hello"
386 close $fd
387 catchcmd test.db [string trim {
388.mode csv
389CREATE TABLE t1(a, b);
390.import shell5.csv t1
391 }]
392 db eval { SELECT * FROM t1 }
393} {{} hello}
394
395do_test shell5-2.2 {
396 set fd [open shell5.csv w]
397 puts $fd {"",hello}
398 close $fd
399 catchcmd test.db [string trim {
400.mode csv
401CREATE TABLE t2(a, b);
402.import shell5.csv t2
403 }]
404 db eval { SELECT * FROM t2 }
405} {{} hello}
406
407do_test shell5-2.3 {
408 set fd [open shell5.csv w]
409 puts $fd {"x""y",hello}
410 close $fd
411 catchcmd test.db [string trim {
412.mode csv
413CREATE TABLE t3(a, b);
414.import shell5.csv t3
415 }]
416 db eval { SELECT * FROM t3 }
417} {x\"y hello}
418
419do_test shell5-2.4 {
420 set fd [open shell5.csv w]
421 puts $fd {"xy""",hello}
422 close $fd
423 catchcmd test.db [string trim {
424.mode csv
425CREATE TABLE t4(a, b);
426.import shell5.csv t4
427 }]
428 db eval { SELECT * FROM t4 }
429} {xy\" hello}
430
431do_test shell5-2.5 {
432 set fd [open shell5.csv w]
433 puts $fd {"one","2"}
434 puts $fd {}
435 close $fd
436 catchcmd test.db [string trim {
437.mode csv
438CREATE TABLE t4(a, b);
439.import shell5.csv t4
440 }]
441 db eval { SELECT * FROM t4 }
442} {xy\" hello one 2 {} {}}
443
444#----------------------------------------------------------------------------
445# Tests for the shell "ascii" import/export mode.
446#
447do_test shell5-3.1 {
448 set fd [open shell5.csv w]
449 fconfigure $fd -translation binary
450 puts -nonewline $fd "\"test 1\"\x1F,test 2\r\n\x1E"
451 puts -nonewline $fd "test 3\x1Ftest 4\n"
452 close $fd
453 catchcmd test.db {
454.mode ascii
455CREATE TABLE t5(a, b);
456.import shell5.csv t5
457 }
458 db eval { SELECT * FROM t5 }
459} "\{\"test 1\"} \{,test 2\r\n\} \{test 3\} \{test 4\n\}"
460
461do_test shell5-3.2 {
462 set x [catchcmd test.db {
463.mode ascii
464SELECT * FROM t5;
465 }]
466 # Handle platform end-of-line differences
467 regsub -all {[\n\r]?\n} $x <EOL> x
468 set x
469} "0 \{\"test 1\"\x1F,test 2<EOL>\x1Etest 3\x1Ftest 4<EOL>\x1E\}"
470
471do_test shell5-4.1 {
472 forcedelete shell5.csv
473 set fd [open shell5.csv w]
474 puts $fd "1,2,3"
475 puts $fd "4,5"
476 puts $fd "6,7,8"
477 close $fd
478 catchcmd test.db [string trim {
479.mode csv
480CREATE TABLE t6(a, b, c);
481.import shell5.csv t6
482 }]
483 db eval { SELECT * FROM t6 ORDER BY a }
484} {1 2 3 4 5 {} 6 7 8}
485
486do_test shell5-4.2 {
487 forcedelete shell5.csv
488 set fd [open shell5.csv w]
489 puts $fd "1,2,3"
490 puts $fd "4,5"
491 puts $fd "6,7,8,9"
492 close $fd
493 catchcmd test.db [string trim {
494.mode csv
495CREATE TABLE t7(a, b, c);
496.import shell5.csv t7
497 }]
498 db eval { SELECT * FROM t7 ORDER BY a }
499} {1 2 3 4 5 {} 6 7 8}
500
501do_test shell5-4.3 {
502 forcedelete shell5.csv
503 set fd [open shell5.csv w]
504 puts $fd ",,"
505 puts $fd "1,2,3"
506 close $fd
507 catchcmd test.db [string trim {
508.mode csv
509CREATE TABLE t8(a, b, c);
510.import -skip 1 shell5.csv t8
511.nullvalue #
512 }]
513 db eval { SELECT * FROM t8 }
514} {1 2 3}
515
516do_test shell5-4.4 {
517 forcedelete shell5.csv
518 set fd [open shell5.csv w]
519 puts $fd "1,2,3"
520 close $fd
521 catchcmd test.db [string trim {
522.mode csv
523CREATE TEMP TABLE t8(a, b, c);
524.import shell5.csv t8
525.nullvalue #
526.mode quote
527SELECT * FROM temp.t8
528 }]
529} {0 '1','2','3'}
530
531#----------------------------------------------------------------------------
532# Tests for the shell automatic column rename.
533#
534db close
535
536# Import columns containing duplicates
537do_test shell5-5.1 {
538 set out [open shell5.csv w]
539 fconfigure $out -translation lf
540 puts $out {"","x","x","y","z","z_0","z_5","z"}
541 puts $out {0,"x2","x3","y4","z5","z6","z7","z8"}
542 close $out
543 forcedelete test.db
544 catchcmd test.db {.import -csv shell5.csv t1
545.mode line --colsep ' = '
546SELECT * FROM t1;}
547} {1 { ? = 0
548 x_02 = x2
549 x_03 = x3
550 y = y4
551 z_05 = z5
552 z_0 = z6
553 z_5 = z7
554 z_08 = z8
555Columns renamed during .import shell5.csv due to duplicates:
556"x" to "x_02",
557"x" to "x_03",
558"z" to "z_05",
559"z" to "z_08"}}
560
561do_test shell5-5.1b {
562 set out [open shell5.csv w]
563 fconfigure $out -translation lf
564 puts $out {"COW","cow","CoW","cOw"}
565 puts $out {"uuu","lll","ulu","lul"}
566 close $out
567 forcedelete test.db
568 catchcmd test.db {.import -csv shell5.csv t1
569.mode line
570SELECT * FROM t1;}
571} {1 {COW_1: uuu
572cow_2: lll
573CoW_3: ulu
574cOw_4: lul
575Columns renamed during .import shell5.csv due to duplicates:
576"COW" to "COW_1",
577"cow" to "cow_2",
578"CoW" to "CoW_3",
579"cOw" to "cOw_4"}}
580
581#----------------------------------------------------------------------------
582# Tests for preserving utf-8 that is not also ASCII.
583#
584
585do_test_with_ansi_output shell5-6.1 {
586 set out [open shell5.csv w]
587 fconfigure $out -translation lf
588 puts $out {あい,うえお}
589 puts $out {1,2}
590 close $out
591 forcedelete test.db
592 catchcmd test.db {.import -csv shell5.csv t1
593.mode line --colsep " = "
594SELECT * FROM t1;}
595} {0 { あい = 1
596うえお = 2}}
597
598do_test_with_ansi_output shell5-6.2 {
599 set out [open shell5.csv w]
600 fconfigure $out -translation lf
601 puts $out {1,2}
602 puts $out {あい,うえお}
603 close $out
604 forcedelete test.db
605 catchcmd test.db {.import -csv shell5.csv t1
606.mode line
607SELECT * FROM t1;}
608} {0 {1: あい
6092: うえお}}
610
611# 2024-03-11 https://sqlite.org/forum/forumpost/ca014d7358
612# Import into a table that contains computed columns.
613#
614do_test shell5-7.1 {
615 set out [open shell5.csv w]
616 fconfigure $out -translation lf
617 puts $out {aaa|bbb}
618 close $out
619 forcedelete test.db
620 catchcmd ":memory: -list" {CREATE TABLE t1(a TEXT, b TEXT, c AS (a||b));
621.import shell5.csv t1
622SELECT * FROM t1;}
623} {0 aaa|bbb|aaabbb}
624
625#-------------------------------------------------------------------------
626
627do_test shell5-8.1 {
628
629 set out [open shell5.csv w]
630 fconfigure $out -translation lf
631 puts $out x
632 close $out
633
634 catchcmd :memory: {.import --csv shell5.csv '""""""""""""""""""""""""""""""""""""""""""""""'}
635} {0 {}}
636
637
638# 2025-12-29 https://sqlite.org/forum/forumpost/6c1c0e213d
639# .import honor .bail
640#
641do_test shell5-9.1 {
642 catchcmd ":memory:" {
643 CREATE TABLE t1(a,b,c INT CHECK(c<>5));
644.bail on
645.import -csv <<END t1
6461,2,3
647"a","b","c"
6483,4,5
649"q","r","s"
650END
651SELECT * FROM t1;}
652} {1 {<stdin>:7: INSERT failed: CHECK constraint failed: c<>5}}
653do_test shell5-9.2 {
654 catchcmd ":memory:" {
655 CREATE TABLE t1(a,b,c INT CHECK(c<>5));
656.bail off
657.import -csv <<END t1
6581,2,3
659"a","b","c"
6603,4,5
661"q","r","s"
662END
663SELECT * FROM t1;}
664} {1 {1|2|3
665a|b|c
666q|r|s
667<stdin>:7: INSERT failed: CHECK constraint failed: c<>5}}
668
669finish_test