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