blob: d63a0a20d3edd86086b4dc5eebd450757fa1532c [file] [log] [blame]
shaneh1b8f78c2010-08-18 17:16:26 +00001# 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#
12# The focus of this file is testing the CLI shell tool.
13# These tests are specific to the .import command.
14#
15# $Id: shell5.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $
16#
17
18# Test plan:
19#
20# shell5-1.*: Basic tests specific to the ".import" command.
21#
drh8df91852012-04-24 12:46:05 +000022set testdir [file dirname $argv0]
23source $testdir/tester.tcl
dan089555c2016-03-15 09:55:44 +000024set CLI [test_find_cli]
drh8df91852012-04-24 12:46:05 +000025db close
26forcedelete test.db test.db-journal test.db-wal
shaneh1b8f78c2010-08-18 17:16:26 +000027
28#----------------------------------------------------------------------------
29# Test cases shell5-1.*: Basic handling of the .import and .separator commands.
30#
31
32# .import FILE TABLE Import data from FILE into TABLE
33do_test shell5-1.1.1 {
34 catchcmd "test.db" ".import"
drhccb37812020-03-09 15:39:39 +000035} {/1 .ERROR: missing FILE argument.*/}
shaneh1b8f78c2010-08-18 17:16:26 +000036do_test shell5-1.1.2 {
37 catchcmd "test.db" ".import FOO"
drhccb37812020-03-09 15:39:39 +000038} {/1 .ERROR: missing TABLE argument.*/}
shaneh1b8f78c2010-08-18 17:16:26 +000039do_test shell5-1.1.3 {
40 # too many arguments
41 catchcmd "test.db" ".import FOO BAR BAD"
drhccb37812020-03-09 15:39:39 +000042} {/1 .ERROR: extra argument.*/}
shaneh1b8f78c2010-08-18 17:16:26 +000043
44# .separator STRING Change separator used by output mode and .import
drhc2ce0be2014-05-29 12:36:14 +000045do_test shell5-1.2.1 {
shaneh1b8f78c2010-08-18 17:16:26 +000046 catchcmd "test.db" ".separator"
mistachkine0d68852014-12-11 03:12:33 +000047} {1 {Usage: .separator COL ?ROW?}}
drhc2ce0be2014-05-29 12:36:14 +000048do_test shell5-1.2.2 {
drh6976c212014-07-24 12:09:47 +000049 catchcmd "test.db" ".separator ONE"
shaneh1b8f78c2010-08-18 17:16:26 +000050} {0 {}}
drhc2ce0be2014-05-29 12:36:14 +000051do_test shell5-1.2.3 {
drh6976c212014-07-24 12:09:47 +000052 catchcmd "test.db" ".separator ONE TWO"
53} {0 {}}
54do_test shell5-1.2.4 {
shaneh1b8f78c2010-08-18 17:16:26 +000055 # too many arguments
drh6976c212014-07-24 12:09:47 +000056 catchcmd "test.db" ".separator ONE TWO THREE"
mistachkine0d68852014-12-11 03:12:33 +000057} {1 {Usage: .separator COL ?ROW?}}
shaneh1b8f78c2010-08-18 17:16:26 +000058
mistachkin636bf9f2014-07-19 20:15:16 +000059# column separator should default to "|"
60do_test shell5-1.3.1.1 {
shaneh1b8f78c2010-08-18 17:16:26 +000061 set res [catchcmd "test.db" ".show"]
mistachkin636bf9f2014-07-19 20:15:16 +000062 list [regexp {colseparator: \"\|\"} $res]
63} {1}
64
65# row separator should default to "\n"
66do_test shell5-1.3.1.2 {
67 set res [catchcmd "test.db" ".show"]
68 list [regexp {rowseparator: \"\\n\"} $res]
shaneh1b8f78c2010-08-18 17:16:26 +000069} {1}
70
71# set separator to different value.
72# check that .show reports new value
73do_test shell5-1.3.2 {
74 set res [catchcmd "test.db" {.separator ,
75.show}]
76 list [regexp {separator: \",\"} $res]
77} {1}
78
79# import file doesn't exist
80do_test shell5-1.4.1 {
mistachkin9ac99312013-09-13 23:26:47 +000081 forcedelete FOO
shaneh1b8f78c2010-08-18 17:16:26 +000082 set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
83.import FOO t1}]
84} {1 {Error: cannot open "FOO"}}
85
86# empty import file
87do_test shell5-1.4.2 {
mistachkin9ac99312013-09-13 23:26:47 +000088 forcedelete shell5.csv
shaneh1b8f78c2010-08-18 17:16:26 +000089 set in [open shell5.csv w]
90 close $in
larrybr738d7b92022-01-13 21:22:54 +000091 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
92.import -schema test shell5.csv t1
93SELECT COUNT(*) FROM test.t1;}]
shaneh1b8f78c2010-08-18 17:16:26 +000094} {0 0}
95
96# import file with 1 row, 1 column (expecting 2 cols)
97do_test shell5-1.4.3 {
98 set in [open shell5.csv w]
99 puts $in "1"
100 close $in
larrybr738d7b92022-01-13 21:22:54 +0000101 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
102.import -schema test shell5.csv t1}]
drhdb95f682013-06-26 22:46:00 +0000103} {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}}
shaneh1b8f78c2010-08-18 17:16:26 +0000104
105# import file with 1 row, 3 columns (expecting 2 cols)
106do_test shell5-1.4.4 {
107 set in [open shell5.csv w]
108 puts $in "1|2|3"
109 close $in
larrybr738d7b92022-01-13 21:22:54 +0000110 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
111.import --schema test shell5.csv t1}]
drhdb95f682013-06-26 22:46:00 +0000112} {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}}
shaneh1b8f78c2010-08-18 17:16:26 +0000113
114# import file with 1 row, 2 columns
115do_test shell5-1.4.5 {
116 set in [open shell5.csv w]
117 puts $in "1|2"
118 close $in
drhdb95f682013-06-26 22:46:00 +0000119 set res [catchcmd "test.db" {DELETE FROM t1;
120.import shell5.csv t1
shaneh1b8f78c2010-08-18 17:16:26 +0000121SELECT COUNT(*) FROM t1;}]
122} {0 1}
123
124# import file with 2 rows, 2 columns
125# note we end up with 3 rows because of the 1 row
126# imported above.
127do_test shell5-1.4.6 {
128 set in [open shell5.csv w]
129 puts $in "2|3"
130 puts $in "3|4"
131 close $in
larrybr738d7b92022-01-13 21:22:54 +0000132 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
133.import -schema test shell5.csv t1
134SELECT COUNT(*) FROM test.t1;}]
shaneh1b8f78c2010-08-18 17:16:26 +0000135} {0 3}
136
137# import file with 1 row, 2 columns, using a comma
138do_test shell5-1.4.7 {
139 set in [open shell5.csv w]
140 puts $in "4,5"
141 close $in
larrybr738d7b92022-01-13 21:22:54 +0000142 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
143.separator ,
144.import --schema test shell5.csv t1
145SELECT COUNT(*) FROM test.t1;}]
shaneh1b8f78c2010-08-18 17:16:26 +0000146} {0 4}
147
148# import file with 1 row, 2 columns, text data
149do_test shell5-1.4.8.1 {
150 set in [open shell5.csv w]
151 puts $in "5|Now is the time for all good men to come to the aid of their country."
152 close $in
153 set res [catchcmd "test.db" {.import shell5.csv t1
154SELECT COUNT(*) FROM t1;}]
155} {0 5}
156
157do_test shell5-1.4.8.2 {
158 catchcmd "test.db" {SELECT b FROM t1 WHERE a='5';}
159} {0 {Now is the time for all good men to come to the aid of their country.}}
160
161# import file with 1 row, 2 columns, quoted text data
162# note that currently sqlite doesn't support quoted fields, and
163# imports the entire field, quotes and all.
164do_test shell5-1.4.9.1 {
165 set in [open shell5.csv w]
166 puts $in "6|'Now is the time for all good men to come to the aid of their country.'"
167 close $in
168 set res [catchcmd "test.db" {.import shell5.csv t1
169SELECT COUNT(*) FROM t1;}]
170} {0 6}
171
172do_test shell5-1.4.9.2 {
173 catchcmd "test.db" {SELECT b FROM t1 WHERE a='6';}
174} {0 {'Now is the time for all good men to come to the aid of their country.'}}
175
176# import file with 1 row, 2 columns, quoted text data
177do_test shell5-1.4.10.1 {
178 set in [open shell5.csv w]
179 puts $in "7|\"Now is the time for all good men to come to the aid of their country.\""
180 close $in
181 set res [catchcmd "test.db" {.import shell5.csv t1
182SELECT COUNT(*) FROM t1;}]
183} {0 7}
184
185do_test shell5-1.4.10.2 {
186 catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
drh18f52e02012-01-16 16:56:31 +0000187} {0 {Now is the time for all good men to come to the aid of their country.}}
shaneh1b8f78c2010-08-18 17:16:26 +0000188
drhd5fbde82017-06-26 18:42:23 +0000189# import file with 2 rows, 2 columns and an initial BOM
190#
191do_test shell5-1.4.11 {
192 set in [open shell5.csv wb]
drha4d770c2017-07-15 13:49:56 +0000193 puts -nonewline $in "\xef\xbb\xbf"
194 puts $in "2|3"
drhd5fbde82017-06-26 18:42:23 +0000195 puts $in "4|5"
196 close $in
197 set res [catchcmd "test.db" {CREATE TABLE t2(x INT, y INT);
198.import shell5.csv t2
199.mode quote
200.header on
201SELECT * FROM t2;}]
202 string map {\n | \n\r |} $res
203} {0 {'x','y'|2,3|4,5}}
204
205# import file with 2 rows, 2 columns or text with an initial BOM
206#
207do_test shell5-1.4.12 {
208 set in [open shell5.csv wb]
209 puts $in "\xef\xbb\xbf\"two\"|3"
210 puts $in "4|5"
211 close $in
212 set res [catchcmd "test.db" {DELETE FROM t2;
213.import shell5.csv t2
214.mode quote
215.header on
216SELECT * FROM t2;}]
217 string map {\n | \n\r |} $res
218} {0 {'x','y'|'two',3|4,5}}
219
shaneh1b8f78c2010-08-18 17:16:26 +0000220# check importing very long field
221do_test shell5-1.5.1 {
222 set str [string repeat X 999]
223 set in [open shell5.csv w]
224 puts $in "8|$str"
225 close $in
226 set res [catchcmd "test.db" {.import shell5.csv t1
227SELECT length(b) FROM t1 WHERE a='8';}]
228} {0 999}
229
230# try importing into a table with a large number of columns.
231# This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
232set cols 999
233do_test shell5-1.6.1 {
shaneh1b8f78c2010-08-18 17:16:26 +0000234 set data {}
235 for {set i 1} {$i<$cols} {incr i} {
drhdb95f682013-06-26 22:46:00 +0000236 append data "c$i|"
237 }
238 append data "c$cols\n";
239 for {set i 1} {$i<$cols} {incr i} {
shaneh1b8f78c2010-08-18 17:16:26 +0000240 append data "$i|"
241 }
shaneh1b8f78c2010-08-18 17:16:26 +0000242 append data "$cols"
shaneh1b8f78c2010-08-18 17:16:26 +0000243 set in [open shell5.csv w]
244 puts $in $data
245 close $in
drhd5fbde82017-06-26 18:42:23 +0000246 set res [catchcmd "test.db" {DROP TABLE IF EXISTS t2;
247.import shell5.csv t2
shaneh1b8f78c2010-08-18 17:16:26 +0000248SELECT COUNT(*) FROM t2;}]
249} {0 1}
250
251# try importing a large number of rows
drh5bde8162013-06-27 14:07:53 +0000252set rows 9999
shaneh1b8f78c2010-08-18 17:16:26 +0000253do_test shell5-1.7.1 {
254 set in [open shell5.csv w]
drhdb95f682013-06-26 22:46:00 +0000255 puts $in a
shaneh1b8f78c2010-08-18 17:16:26 +0000256 for {set i 1} {$i<=$rows} {incr i} {
257 puts $in $i
258 }
259 close $in
drhdb95f682013-06-26 22:46:00 +0000260 set res [catchcmd "test.db" {.mode csv
shaneh1b8f78c2010-08-18 17:16:26 +0000261.import shell5.csv t3
262SELECT COUNT(*) FROM t3;}]
263} [list 0 $rows]
264
larrybr53e11862022-03-06 23:41:21 +0000265# Import from a pipe. (Unix only, as it requires "awk")
drh5bde8162013-06-27 14:07:53 +0000266if {$tcl_platform(platform)=="unix"} {
267 do_test shell5-1.8 {
mistachkin9ac99312013-09-13 23:26:47 +0000268 forcedelete test.db
drh5bde8162013-06-27 14:07:53 +0000269 catchcmd test.db {.mode csv
270.import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1
271SELECT * FROM t1;}
272 } {0 {1,"this is 1"
2732,"this is 2"
2743,"this is 3"
2754,"this is 4"
2765,"this is 5"}}
277}
278
drhf7f8de52013-08-28 11:57:52 +0000279# Import columns containing quoted strings
280do_test shell5-1.9 {
281 set out [open shell5.csv w]
mistachkin9ac99312013-09-13 23:26:47 +0000282 fconfigure $out -translation lf
drhf7f8de52013-08-28 11:57:52 +0000283 puts $out {1,"",11}
284 puts $out {2,"x",22}
285 puts $out {3,"""",33}
286 puts $out {4,"hello",44}
drh868ccf22013-08-28 13:33:40 +0000287 puts $out "5,55,\"\"\r"
drhf7f8de52013-08-28 11:57:52 +0000288 puts $out {6,66,"x"}
289 puts $out {7,77,""""}
290 puts $out {8,88,"hello"}
291 puts $out {"",9,99}
292 puts $out {"x",10,110}
293 puts $out {"""",11,121}
294 puts $out {"hello",12,132}
295 close $out
mistachkin9ac99312013-09-13 23:26:47 +0000296 forcedelete test.db
drhf7f8de52013-08-28 11:57:52 +0000297 catchcmd test.db {.mode csv
298 CREATE TABLE t1(a,b,c);
299.import shell5.csv t1
300 }
301 sqlite3 db test.db
302 db eval {SELECT *, '|' FROM t1 ORDER BY rowid}
303} {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 |}
304db close
305
drha81ad172013-12-11 14:00:04 +0000306# Import columns containing quoted strings
307do_test shell5-1.10 {
308 set out [open shell5.csv w]
309 fconfigure $out -translation lf
310 puts $out {column1,column2,column3,column4}
311 puts $out "field1,field2,\"x3 \"\"\r\ndata\"\" 3\",field4"
312 puts $out "x1,x2,\"x3 \"\"\ndata\"\" 3\",x4"
313 close $out
314 forcedelete test.db
315 catchcmd test.db {.mode csv
316 CREATE TABLE t1(a,b,c,d);
317.import shell5.csv t1
318 }
319 sqlite3 db test.db
320 db eval {SELECT hex(c) FROM t1 ORDER BY rowid}
321} {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033}
322
drh3852b682014-02-26 13:53:34 +0000323# Blank last column with \r\n line endings.
324do_test shell5-1.11 {
325 set out [open shell5.csv w]
326 fconfigure $out -translation binary
327 puts $out "column1,column2,column3\r"
328 puts $out "a,b, \r"
329 puts $out "x,y,\r"
330 puts $out "p,q,r\r"
331 close $out
332 catch {db close}
333 forcedelete test.db
334 catchcmd test.db {.mode csv
335.import shell5.csv t1
336 }
337 sqlite3 db test.db
338 db eval {SELECT *, '|' FROM t1}
339} {a b { } | x y {} | p q r |}
drha81ad172013-12-11 14:00:04 +0000340db close
341
dan6a8ac852014-05-26 18:27:12 +0000342#----------------------------------------------------------------------------
343#
344reset_db
345sqlite3 db test.db
346do_test shell5-2.1 {
347 set fd [open shell5.csv w]
348 puts $fd ",hello"
349 close $fd
350 catchcmd test.db [string trim {
351.mode csv
352CREATE TABLE t1(a, b);
353.import shell5.csv t1
354 }]
355 db eval { SELECT * FROM t1 }
356} {{} hello}
357
358do_test shell5-2.2 {
359 set fd [open shell5.csv w]
360 puts $fd {"",hello}
361 close $fd
362 catchcmd test.db [string trim {
363.mode csv
364CREATE TABLE t2(a, b);
365.import shell5.csv t2
366 }]
367 db eval { SELECT * FROM t2 }
368} {{} hello}
369
370do_test shell5-2.3 {
371 set fd [open shell5.csv w]
372 puts $fd {"x""y",hello}
373 close $fd
374 catchcmd test.db [string trim {
375.mode csv
376CREATE TABLE t3(a, b);
377.import shell5.csv t3
378 }]
379 db eval { SELECT * FROM t3 }
380} {x\"y hello}
381
382do_test shell5-2.4 {
383 set fd [open shell5.csv w]
384 puts $fd {"xy""",hello}
385 close $fd
386 catchcmd test.db [string trim {
387.mode csv
388CREATE TABLE t4(a, b);
389.import shell5.csv t4
390 }]
391 db eval { SELECT * FROM t4 }
392} {xy\" hello}
393
mistachkin6fe03382014-06-16 22:45:28 +0000394do_test shell5-2.5 {
395 set fd [open shell5.csv w]
396 puts $fd {"one","2"}
397 puts $fd {}
398 close $fd
399 catchcmd test.db [string trim {
400.mode csv
401CREATE TABLE t4(a, b);
402.import shell5.csv t4
403 }]
404 db eval { SELECT * FROM t4 }
405} {xy\" hello one 2 {} {}}
dan6a8ac852014-05-26 18:27:12 +0000406
mistachkin636bf9f2014-07-19 20:15:16 +0000407#----------------------------------------------------------------------------
408# Tests for the shell "ascii" import/export mode.
409#
410do_test shell5-3.1 {
411 set fd [open shell5.csv w]
412 fconfigure $fd -encoding binary -translation binary
413 puts -nonewline $fd "\"test 1\"\x1F,test 2\r\n\x1E"
414 puts -nonewline $fd "test 3\x1Ftest 4\n"
415 close $fd
416 catchcmd test.db {
417.mode ascii
418CREATE TABLE t5(a, b);
419.import shell5.csv t5
420 }
421 db eval { SELECT * FROM t5 }
422} "\{\"test 1\"} \{,test 2\r\n\} \{test 3\} \{test 4\n\}"
423
mistachkin636bf9f2014-07-19 20:15:16 +0000424do_test shell5-3.2 {
drhfa5ed022015-01-09 00:38:06 +0000425 set x [catchcmd test.db {
mistachkin636bf9f2014-07-19 20:15:16 +0000426.mode ascii
427SELECT * FROM t5;
drhfa5ed022015-01-09 00:38:06 +0000428 }]
429 # Handle platform end-of-line differences
430 regsub -all {[\n\r]?\n} $x <EOL> x
431 set x
432} "0 \{\"test 1\"\x1F,test 2<EOL>\x1Etest 3\x1Ftest 4<EOL>\x1E\}"
dan6a8ac852014-05-26 18:27:12 +0000433
mistachkina0efb1a2015-02-12 22:45:25 +0000434do_test shell5-4.1 {
435 forcedelete shell5.csv
436 set fd [open shell5.csv w]
437 puts $fd "1,2,3"
438 puts $fd "4,5"
439 puts $fd "6,7,8"
440 close $fd
441 catchcmd test.db [string trim {
442.mode csv
443CREATE TABLE t6(a, b, c);
444.import shell5.csv t6
445 }]
446 db eval { SELECT * FROM t6 ORDER BY a }
447} {1 2 3 4 5 {} 6 7 8}
448
449do_test shell5-4.2 {
450 forcedelete shell5.csv
451 set fd [open shell5.csv w]
452 puts $fd "1,2,3"
453 puts $fd "4,5"
454 puts $fd "6,7,8,9"
455 close $fd
456 catchcmd test.db [string trim {
457.mode csv
458CREATE TABLE t7(a, b, c);
459.import shell5.csv t7
460 }]
461 db eval { SELECT * FROM t7 ORDER BY a }
462} {1 2 3 4 5 {} 6 7 8}
463
larrybr6ac9a5c2021-10-28 19:49:23 +0000464do_test shell5-4.3 {
465 forcedelete shell5.csv
466 set fd [open shell5.csv w]
467 puts $fd ",,"
468 puts $fd "1,2,3"
469 close $fd
470 catchcmd test.db [string trim {
471.mode csv
472CREATE TABLE t8(a, b, c);
473.import -skip 1 shell5.csv t8
474.nullvalue #
475 }]
476 db eval { SELECT * FROM t8 }
477} {1 2 3}
478
larrybr53e11862022-03-06 23:41:21 +0000479do_test shell5-4.4 {
480 forcedelete shell5.csv
481 set fd [open shell5.csv w]
482 puts $fd "1,2,3"
483 close $fd
484 catchcmd test.db [string trim {
485.mode csv
486CREATE TEMP TABLE t8(a, b, c);
487.import shell5.csv t8
488.nullvalue #
489SELECT * FROM temp.t8
490 }]
491} {0 1,2,3}
492
larrybra0337272022-02-11 13:40:25 +0000493#----------------------------------------------------------------------------
494# Tests for the shell automatic column rename.
495#
drh24ea5fb2022-02-17 14:29:39 +0000496db close
larrybra0337272022-02-11 13:40:25 +0000497
498# Import columns containing duplicates
499do_test shell5-5.1 {
500 set out [open shell5.csv w]
501 fconfigure $out -translation lf
larrybr33633862022-02-14 01:12:46 +0000502 puts $out {"","x","x","y","z","z_0","z_5","z"}
503 puts $out {0,"x2","x3","y4","z5","z6","z7","z8"}
larrybra0337272022-02-11 13:40:25 +0000504 close $out
505 forcedelete test.db
506 catchcmd test.db {.import -csv shell5.csv t1
507.mode line
508SELECT * FROM t1;}
509} {1 { ? = 0
larrybr33633862022-02-14 01:12:46 +0000510 x_02 = x2
511 x_03 = x3
512 y = y4
513 z_05 = z5
514 z_0 = z6
515 z_5 = z7
516 z_08 = z8
517Columns renamed during .import shell5.csv due to duplicates:
518"x" to "x_02",
519"x" to "x_03",
520"z" to "z_05",
521"z" to "z_08"}}
larrybra0337272022-02-11 13:40:25 +0000522
larrybr0a704c32022-02-12 10:48:42 +0000523do_test shell5-5.1 {
524 set out [open shell5.csv w]
525 fconfigure $out -translation lf
526 puts $out {"COW","cow","CoW","cOw"}
527 puts $out {"uuu","lll","ulu","lul"}
528 close $out
529 forcedelete test.db
530 catchcmd test.db {.import -csv shell5.csv t1
531.mode line
532SELECT * FROM t1;}
533} {1 {COW_1 = uuu
534cow_2 = lll
535CoW_3 = ulu
536cOw_4 = lul
larrybr33633862022-02-14 01:12:46 +0000537Columns renamed during .import shell5.csv due to duplicates:
538"COW" to "COW_1",
539"cow" to "cow_2",
540"CoW" to "CoW_3",
541"cOw" to "cOw_4"}}
larrybr0a704c32022-02-12 10:48:42 +0000542
drh8df91852012-04-24 12:46:05 +0000543finish_test