blob: d90cedf949ef834d9b41cf19c453c164925dd8dd [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
24if {$tcl_platform(platform)=="windows"} {
25 set CLI "sqlite3.exe"
26} else {
27 set CLI "./sqlite3"
shaneh1b8f78c2010-08-18 17:16:26 +000028}
drh8df91852012-04-24 12:46:05 +000029if {![file executable $CLI]} {
30 finish_test
31 return
shaneh1b8f78c2010-08-18 17:16:26 +000032}
drh8df91852012-04-24 12:46:05 +000033db close
34forcedelete test.db test.db-journal test.db-wal
35sqlite3 db test.db
shaneh1b8f78c2010-08-18 17:16:26 +000036
37#----------------------------------------------------------------------------
38# Test cases shell5-1.*: Basic handling of the .import and .separator commands.
39#
40
41# .import FILE TABLE Import data from FILE into TABLE
42do_test shell5-1.1.1 {
43 catchcmd "test.db" ".import"
44} {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
45do_test shell5-1.1.2 {
46 catchcmd "test.db" ".import FOO"
47} {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
48do_test shell5-1.1.2 {
49 catchcmd "test.db" ".import FOO BAR"
50} {1 {Error: no such table: BAR}}
51do_test shell5-1.1.3 {
52 # too many arguments
53 catchcmd "test.db" ".import FOO BAR BAD"
54} {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
55
56# .separator STRING Change separator used by output mode and .import
57do_test shell1-1.2.1 {
58 catchcmd "test.db" ".separator"
59} {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
60do_test shell1-1.2.2 {
61 catchcmd "test.db" ".separator FOO"
62} {0 {}}
63do_test shell1-1.2.3 {
64 # too many arguments
65 catchcmd "test.db" ".separator FOO BAD"
66} {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
67
68# separator should default to "|"
69do_test shell5-1.3.1 {
70 set res [catchcmd "test.db" ".show"]
71 list [regexp {separator: \"\|\"} $res]
72} {1}
73
74# set separator to different value.
75# check that .show reports new value
76do_test shell5-1.3.2 {
77 set res [catchcmd "test.db" {.separator ,
78.show}]
79 list [regexp {separator: \",\"} $res]
80} {1}
81
82# import file doesn't exist
83do_test shell5-1.4.1 {
84 file delete -force FOO
85 set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
86.import FOO t1}]
87} {1 {Error: cannot open "FOO"}}
88
89# empty import file
90do_test shell5-1.4.2 {
91 file delete -force shell5.csv
92 set in [open shell5.csv w]
93 close $in
94 set res [catchcmd "test.db" {.import shell5.csv t1
95SELECT COUNT(*) FROM t1;}]
96} {0 0}
97
98# import file with 1 row, 1 column (expecting 2 cols)
99do_test shell5-1.4.3 {
100 set in [open shell5.csv w]
101 puts $in "1"
102 close $in
103 set res [catchcmd "test.db" {.import shell5.csv t1}]
104} {1 {Error: shell5.csv line 1: expected 2 columns of data but found 1}}
105
106# import file with 1 row, 3 columns (expecting 2 cols)
107do_test shell5-1.4.4 {
108 set in [open shell5.csv w]
109 puts $in "1|2|3"
110 close $in
111 set res [catchcmd "test.db" {.import shell5.csv t1}]
112} {1 {Error: shell5.csv line 1: expected 2 columns of data but found 3}}
113
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
119 set res [catchcmd "test.db" {.import shell5.csv t1
120SELECT COUNT(*) FROM t1;}]
121} {0 1}
122
123# import file with 2 rows, 2 columns
124# note we end up with 3 rows because of the 1 row
125# imported above.
126do_test shell5-1.4.6 {
127 set in [open shell5.csv w]
128 puts $in "2|3"
129 puts $in "3|4"
130 close $in
131 set res [catchcmd "test.db" {.import shell5.csv t1
132SELECT COUNT(*) FROM t1;}]
133} {0 3}
134
135# import file with 1 row, 2 columns, using a comma
136do_test shell5-1.4.7 {
137 set in [open shell5.csv w]
138 puts $in "4,5"
139 close $in
140 set res [catchcmd "test.db" {.separator ,
141.import shell5.csv t1
142SELECT COUNT(*) FROM t1;}]
143} {0 4}
144
145# import file with 1 row, 2 columns, text data
146do_test shell5-1.4.8.1 {
147 set in [open shell5.csv w]
148 puts $in "5|Now is the time for all good men to come to the aid of their country."
149 close $in
150 set res [catchcmd "test.db" {.import shell5.csv t1
151SELECT COUNT(*) FROM t1;}]
152} {0 5}
153
154do_test shell5-1.4.8.2 {
155 catchcmd "test.db" {SELECT b FROM t1 WHERE a='5';}
156} {0 {Now is the time for all good men to come to the aid of their country.}}
157
158# import file with 1 row, 2 columns, quoted text data
159# note that currently sqlite doesn't support quoted fields, and
160# imports the entire field, quotes and all.
161do_test shell5-1.4.9.1 {
162 set in [open shell5.csv w]
163 puts $in "6|'Now is the time for all good men to come to the aid of their country.'"
164 close $in
165 set res [catchcmd "test.db" {.import shell5.csv t1
166SELECT COUNT(*) FROM t1;}]
167} {0 6}
168
169do_test shell5-1.4.9.2 {
170 catchcmd "test.db" {SELECT b FROM t1 WHERE a='6';}
171} {0 {'Now is the time for all good men to come to the aid of their country.'}}
172
173# import file with 1 row, 2 columns, quoted text data
174do_test shell5-1.4.10.1 {
175 set in [open shell5.csv w]
176 puts $in "7|\"Now is the time for all good men to come to the aid of their country.\""
177 close $in
178 set res [catchcmd "test.db" {.import shell5.csv t1
179SELECT COUNT(*) FROM t1;}]
180} {0 7}
181
182do_test shell5-1.4.10.2 {
183 catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
drh18f52e02012-01-16 16:56:31 +0000184} {0 {Now is the time for all good men to come to the aid of their country.}}
shaneh1b8f78c2010-08-18 17:16:26 +0000185
186# check importing very long field
187do_test shell5-1.5.1 {
188 set str [string repeat X 999]
189 set in [open shell5.csv w]
190 puts $in "8|$str"
191 close $in
192 set res [catchcmd "test.db" {.import shell5.csv t1
193SELECT length(b) FROM t1 WHERE a='8';}]
194} {0 999}
195
196# try importing into a table with a large number of columns.
197# This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
198set cols 999
199do_test shell5-1.6.1 {
200 set sql {CREATE TABLE t2(}
201 set data {}
202 for {set i 1} {$i<$cols} {incr i} {
203 append sql "c$i,"
204 append data "$i|"
205 }
206 append sql "c$cols);"
207 append data "$cols"
208 catchcmd "test.db" $sql
209 set in [open shell5.csv w]
210 puts $in $data
211 close $in
212 set res [catchcmd "test.db" {.import shell5.csv t2
213SELECT COUNT(*) FROM t2;}]
214} {0 1}
215
216# try importing a large number of rows
217set rows 999999
218do_test shell5-1.7.1 {
219 set in [open shell5.csv w]
220 for {set i 1} {$i<=$rows} {incr i} {
221 puts $in $i
222 }
223 close $in
224 set res [catchcmd "test.db" {CREATE TABLE t3(a);
225.import shell5.csv t3
226SELECT COUNT(*) FROM t3;}]
227} [list 0 $rows]
228
drh8df91852012-04-24 12:46:05 +0000229finish_test