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 #***********************************************************************
12 # The focus of this file is testing the CLI shell tool.
13 # These tests are specific to the .import command.
15 # $Id: shell5.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $
20 # shell5-1.*: Basic tests specific to the ".import" command.
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
24 if {$tcl_platform(platform)=="windows"} {
29 if {![file executable $CLI]} {
34 forcedelete test.db test.db-journal test.db-wal
36 #----------------------------------------------------------------------------
37 # Test cases shell5-1.*: Basic handling of the .import and .separator commands.
40 # .import FILE TABLE Import data from FILE into TABLE
41 do_test shell5-1.1.1 {
42 catchcmd "test.db" ".import"
43 } {1 {Usage: .import FILE TABLE}}
44 do_test shell5-1.1.2 {
45 catchcmd "test.db" ".import FOO"
46 } {1 {Usage: .import FILE TABLE}}
47 #do_test shell5-1.1.2 {
48 # catchcmd "test.db" ".import FOO BAR"
49 #} {1 {Error: no such table: BAR}}
50 do_test shell5-1.1.3 {
52 catchcmd "test.db" ".import FOO BAR BAD"
53 } {1 {Usage: .import FILE TABLE}}
55 # .separator STRING Change separator used by output mode and .import
56 do_test shell5-1.2.1 {
57 catchcmd "test.db" ".separator"
58 } {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
59 do_test shell5-1.2.2 {
60 catchcmd "test.db" ".separator ONE"
62 do_test shell5-1.2.3 {
63 catchcmd "test.db" ".separator ONE TWO"
65 do_test shell5-1.2.4 {
67 catchcmd "test.db" ".separator ONE TWO THREE"
68 } {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
70 # separator should default to "|"
71 do_test shell5-1.3.1 {
72 set res [catchcmd "test.db" ".show"]
73 list [regexp {separator: \"\|\"} $res]
76 # set separator to different value.
77 # check that .show reports new value
78 do_test shell5-1.3.2 {
79 set res [catchcmd "test.db" {.separator ,
81 list [regexp {separator: \",\"} $res]
84 # import file doesn't exist
85 do_test shell5-1.4.1 {
87 set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
89 } {1 {Error: cannot open "FOO"}}
92 do_test shell5-1.4.2 {
93 forcedelete shell5.csv
94 set in [open shell5.csv w]
96 set res [catchcmd "test.db" {.import shell5.csv t1
97 SELECT COUNT(*) FROM t1;}]
100 # import file with 1 row, 1 column (expecting 2 cols)
101 do_test shell5-1.4.3 {
102 set in [open shell5.csv w]
105 set res [catchcmd "test.db" {.import shell5.csv t1}]
106 } {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}}
108 # import file with 1 row, 3 columns (expecting 2 cols)
109 do_test shell5-1.4.4 {
110 set in [open shell5.csv w]
113 set res [catchcmd "test.db" {.import shell5.csv t1}]
114 } {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}}
116 # import file with 1 row, 2 columns
117 do_test shell5-1.4.5 {
118 set in [open shell5.csv w]
121 set res [catchcmd "test.db" {DELETE FROM t1;
122 .import shell5.csv t1
123 SELECT COUNT(*) FROM t1;}]
126 # import file with 2 rows, 2 columns
127 # note we end up with 3 rows because of the 1 row
129 do_test shell5-1.4.6 {
130 set in [open shell5.csv w]
134 set res [catchcmd "test.db" {.import shell5.csv t1
135 SELECT COUNT(*) FROM t1;}]
138 # import file with 1 row, 2 columns, using a comma
139 do_test shell5-1.4.7 {
140 set in [open shell5.csv w]
143 set res [catchcmd "test.db" {.separator ,
144 .import shell5.csv t1
145 SELECT COUNT(*) FROM t1;}]
148 # import file with 1 row, 2 columns, text data
149 do_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."
153 set res [catchcmd "test.db" {.import shell5.csv t1
154 SELECT COUNT(*) FROM t1;}]
157 do_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.}}
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.
164 do_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.'"
168 set res [catchcmd "test.db" {.import shell5.csv t1
169 SELECT COUNT(*) FROM t1;}]
172 do_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.'}}
176 # import file with 1 row, 2 columns, quoted text data
177 do_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.\""
181 set res [catchcmd "test.db" {.import shell5.csv t1
182 SELECT COUNT(*) FROM t1;}]
185 do_test shell5-1.4.10.2 {
186 catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
187 } {0 {Now is the time for all good men to come to the aid of their country.}}
189 # check importing very long field
190 do_test shell5-1.5.1 {
191 set str [string repeat X 999]
192 set in [open shell5.csv w]
195 set res [catchcmd "test.db" {.import shell5.csv t1
196 SELECT length(b) FROM t1 WHERE a='8';}]
199 # try importing into a table with a large number of columns.
200 # This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
202 do_test shell5-1.6.1 {
204 for {set i 1} {$i<$cols} {incr i} {
207 append data "c$cols\n";
208 for {set i 1} {$i<$cols} {incr i} {
212 set in [open shell5.csv w]
215 set res [catchcmd "test.db" {.import shell5.csv t2
216 SELECT COUNT(*) FROM t2;}]
219 # try importing a large number of rows
221 do_test shell5-1.7.1 {
222 set in [open shell5.csv w]
224 for {set i 1} {$i<=$rows} {incr i} {
228 set res [catchcmd "test.db" {.mode csv
229 .import shell5.csv t3
230 SELECT COUNT(*) FROM t3;}]
233 # Inport from a pipe. (Unix only, as it requires "awk")
234 if {$tcl_platform(platform)=="unix"} {
237 catchcmd test.db {.mode csv
238 .import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1
247 # Import columns containing quoted strings
249 set out [open shell5.csv w]
250 fconfigure $out -translation lf
253 puts $out {3,"""",33}
254 puts $out {4,"hello",44}
255 puts $out "5,55,\"\"\r"
257 puts $out {7,77,""""}
258 puts $out {8,88,"hello"}
260 puts $out {"x",10,110}
261 puts $out {"""",11,121}
262 puts $out {"hello",12,132}
265 catchcmd test.db {.mode csv
266 CREATE TABLE t1(a,b,c);
267 .import shell5.csv t1
270 db eval {SELECT *, '|' FROM t1 ORDER BY rowid}
271 } {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 |}
274 # Import columns containing quoted strings
275 do_test shell5-1.10 {
276 set out [open shell5.csv w]
277 fconfigure $out -translation lf
278 puts $out {column1,column2,column3,column4}
279 puts $out "field1,field2,\"x3 \"\"\r\ndata\"\" 3\",field4"
280 puts $out "x1,x2,\"x3 \"\"\ndata\"\" 3\",x4"
283 catchcmd test.db {.mode csv
284 CREATE TABLE t1(a,b,c,d);
285 .import shell5.csv t1
288 db eval {SELECT hex(c) FROM t1 ORDER BY rowid}
289 } {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033}
291 # Blank last column with \r\n line endings.
292 do_test shell5-1.11 {
293 set out [open shell5.csv w]
294 fconfigure $out -translation binary
295 puts $out "column1,column2,column3\r"
302 catchcmd test.db {.mode csv
303 .import shell5.csv t1
306 db eval {SELECT *, '|' FROM t1}
307 } {a b { } | x y {} | p q r |}
310 #----------------------------------------------------------------------------
315 set fd [open shell5.csv w]
318 catchcmd test.db [string trim {
320 CREATE TABLE t1(a, b);
321 .import shell5.csv t1
323 db eval { SELECT * FROM t1 }
327 set fd [open shell5.csv w]
330 catchcmd test.db [string trim {
332 CREATE TABLE t2(a, b);
333 .import shell5.csv t2
335 db eval { SELECT * FROM t2 }
339 set fd [open shell5.csv w]
340 puts $fd {"x""y",hello}
342 catchcmd test.db [string trim {
344 CREATE TABLE t3(a, b);
345 .import shell5.csv t3
347 db eval { SELECT * FROM t3 }
351 set fd [open shell5.csv w]
352 puts $fd {"xy""",hello}
354 catchcmd test.db [string trim {
356 CREATE TABLE t4(a, b);
357 .import shell5.csv t4
359 db eval { SELECT * FROM t4 }
363 set fd [open shell5.csv w]
367 catchcmd test.db [string trim {
369 CREATE TABLE t4(a, b);
370 .import shell5.csv t4
372 db eval { SELECT * FROM t4 }
373 } {xy\" hello one 2 {} {}}