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.
25 proc do_test {name cmd expected} {
26 puts -nonewline "$name ..."
27 set res [uplevel $cmd]
28 if {$res eq $expected} {
33 puts " Expected: $expected"
38 proc catchcmd {db {cmd ""}} {
40 set out [open cmds.txt w]
43 set line "exec $CLI $db < cmds.txt"
44 set rc [catch { eval $line } msg]
48 file delete -force test.db test.db.journal
50 #----------------------------------------------------------------------------
51 # Test cases shell5-1.*: Basic handling of the .import and .separator commands.
54 # .import FILE TABLE Import data from FILE into TABLE
55 do_test shell5-1.1.1 {
56 catchcmd "test.db" ".import"
57 } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
58 do_test shell5-1.1.2 {
59 catchcmd "test.db" ".import FOO"
60 } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
61 do_test shell5-1.1.2 {
62 catchcmd "test.db" ".import FOO BAR"
63 } {1 {Error: no such table: BAR}}
64 do_test shell5-1.1.3 {
66 catchcmd "test.db" ".import FOO BAR BAD"
67 } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
69 # .separator STRING Change separator used by output mode and .import
70 do_test shell1-1.2.1 {
71 catchcmd "test.db" ".separator"
72 } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
73 do_test shell1-1.2.2 {
74 catchcmd "test.db" ".separator FOO"
76 do_test shell1-1.2.3 {
78 catchcmd "test.db" ".separator FOO BAD"
79 } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
81 # separator should default to "|"
82 do_test shell5-1.3.1 {
83 set res [catchcmd "test.db" ".show"]
84 list [regexp {separator: \"\|\"} $res]
87 # set separator to different value.
88 # check that .show reports new value
89 do_test shell5-1.3.2 {
90 set res [catchcmd "test.db" {.separator ,
92 list [regexp {separator: \",\"} $res]
95 # import file doesn't exist
96 do_test shell5-1.4.1 {
97 file delete -force FOO
98 set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
100 } {1 {Error: cannot open "FOO"}}
103 do_test shell5-1.4.2 {
104 file delete -force shell5.csv
105 set in [open shell5.csv w]
107 set res [catchcmd "test.db" {.import shell5.csv t1
108 SELECT COUNT(*) FROM t1;}]
111 # import file with 1 row, 1 column (expecting 2 cols)
112 do_test shell5-1.4.3 {
113 set in [open shell5.csv w]
116 set res [catchcmd "test.db" {.import shell5.csv t1}]
117 } {1 {Error: shell5.csv line 1: expected 2 columns of data but found 1}}
119 # import file with 1 row, 3 columns (expecting 2 cols)
120 do_test shell5-1.4.4 {
121 set in [open shell5.csv w]
124 set res [catchcmd "test.db" {.import shell5.csv t1}]
125 } {1 {Error: shell5.csv line 1: expected 2 columns of data but found 3}}
127 # import file with 1 row, 2 columns
128 do_test shell5-1.4.5 {
129 set in [open shell5.csv w]
132 set res [catchcmd "test.db" {.import shell5.csv t1
133 SELECT COUNT(*) FROM t1;}]
136 # import file with 2 rows, 2 columns
137 # note we end up with 3 rows because of the 1 row
139 do_test shell5-1.4.6 {
140 set in [open shell5.csv w]
144 set res [catchcmd "test.db" {.import shell5.csv t1
145 SELECT COUNT(*) FROM t1;}]
148 # import file with 1 row, 2 columns, using a comma
149 do_test shell5-1.4.7 {
150 set in [open shell5.csv w]
153 set res [catchcmd "test.db" {.separator ,
154 .import shell5.csv t1
155 SELECT COUNT(*) FROM t1;}]
158 # import file with 1 row, 2 columns, text data
159 do_test shell5-1.4.8.1 {
160 set in [open shell5.csv w]
161 puts $in "5|Now is the time for all good men to come to the aid of their country."
163 set res [catchcmd "test.db" {.import shell5.csv t1
164 SELECT COUNT(*) FROM t1;}]
167 do_test shell5-1.4.8.2 {
168 catchcmd "test.db" {SELECT b FROM t1 WHERE a='5';}
169 } {0 {Now is the time for all good men to come to the aid of their country.}}
171 # import file with 1 row, 2 columns, quoted text data
172 # note that currently sqlite doesn't support quoted fields, and
173 # imports the entire field, quotes and all.
174 do_test shell5-1.4.9.1 {
175 set in [open shell5.csv w]
176 puts $in "6|'Now is the time for all good men to come to the aid of their country.'"
178 set res [catchcmd "test.db" {.import shell5.csv t1
179 SELECT COUNT(*) FROM t1;}]
182 do_test shell5-1.4.9.2 {
183 catchcmd "test.db" {SELECT b FROM t1 WHERE a='6';}
184 } {0 {'Now is the time for all good men to come to the aid of their country.'}}
186 # import file with 1 row, 2 columns, quoted text data
187 do_test shell5-1.4.10.1 {
188 set in [open shell5.csv w]
189 puts $in "7|\"Now is the time for all good men to come to the aid of their country.\""
191 set res [catchcmd "test.db" {.import shell5.csv t1
192 SELECT COUNT(*) FROM t1;}]
195 do_test shell5-1.4.10.2 {
196 catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
197 } {0 {"Now is the time for all good men to come to the aid of their country."}}
199 # check importing very long field
200 do_test shell5-1.5.1 {
201 set str [string repeat X 999]
202 set in [open shell5.csv w]
205 set res [catchcmd "test.db" {.import shell5.csv t1
206 SELECT length(b) FROM t1 WHERE a='8';}]
209 # try importing into a table with a large number of columns.
210 # This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
212 do_test shell5-1.6.1 {
213 set sql {CREATE TABLE t2(}
215 for {set i 1} {$i<$cols} {incr i} {
219 append sql "c$cols);"
221 catchcmd "test.db" $sql
222 set in [open shell5.csv w]
225 set res [catchcmd "test.db" {.import shell5.csv t2
226 SELECT COUNT(*) FROM t2;}]
229 # try importing a large number of rows
231 do_test shell5-1.7.1 {
232 set in [open shell5.csv w]
233 for {set i 1} {$i<=$rows} {incr i} {
237 set res [catchcmd "test.db" {CREATE TABLE t3(a);
238 .import shell5.csv t3
239 SELECT COUNT(*) FROM t3;}]
243 puts "CLI tests completed successfully"