shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 1 | # 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 | # |
drh | 8df9185 | 2012-04-24 12:46:05 +0000 | [diff] [blame] | 22 | set testdir [file dirname $argv0] |
| 23 | source $testdir/tester.tcl |
| 24 | if {$tcl_platform(platform)=="windows"} { |
| 25 | set CLI "sqlite3.exe" |
| 26 | } else { |
| 27 | set CLI "./sqlite3" |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 28 | } |
drh | 8df9185 | 2012-04-24 12:46:05 +0000 | [diff] [blame] | 29 | if {![file executable $CLI]} { |
| 30 | finish_test |
| 31 | return |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 32 | } |
drh | 8df9185 | 2012-04-24 12:46:05 +0000 | [diff] [blame] | 33 | db close |
| 34 | forcedelete test.db test.db-journal test.db-wal |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 35 | |
| 36 | #---------------------------------------------------------------------------- |
| 37 | # Test cases shell5-1.*: Basic handling of the .import and .separator commands. |
| 38 | # |
| 39 | |
| 40 | # .import FILE TABLE Import data from FILE into TABLE |
| 41 | do_test shell5-1.1.1 { |
| 42 | catchcmd "test.db" ".import" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame^] | 43 | } {1 {Usage: .import FILE TABLE}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 44 | do_test shell5-1.1.2 { |
| 45 | catchcmd "test.db" ".import FOO" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame^] | 46 | } {1 {Usage: .import FILE TABLE}} |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 47 | #do_test shell5-1.1.2 { |
| 48 | # catchcmd "test.db" ".import FOO BAR" |
| 49 | #} {1 {Error: no such table: BAR}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 50 | do_test shell5-1.1.3 { |
| 51 | # too many arguments |
| 52 | catchcmd "test.db" ".import FOO BAR BAD" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame^] | 53 | } {1 {Usage: .import FILE TABLE}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 54 | |
| 55 | # .separator STRING Change separator used by output mode and .import |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame^] | 56 | do_test shell5-1.2.1 { |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 57 | catchcmd "test.db" ".separator" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame^] | 58 | } {1 {Usage: .separator STRING}} |
| 59 | do_test shell5-1.2.2 { |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 60 | catchcmd "test.db" ".separator FOO" |
| 61 | } {0 {}} |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame^] | 62 | do_test shell5-1.2.3 { |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 63 | # too many arguments |
| 64 | catchcmd "test.db" ".separator FOO BAD" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame^] | 65 | } {1 {Usage: .separator STRING}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 66 | |
| 67 | # separator should default to "|" |
| 68 | do_test shell5-1.3.1 { |
| 69 | set res [catchcmd "test.db" ".show"] |
| 70 | list [regexp {separator: \"\|\"} $res] |
| 71 | } {1} |
| 72 | |
| 73 | # set separator to different value. |
| 74 | # check that .show reports new value |
| 75 | do_test shell5-1.3.2 { |
| 76 | set res [catchcmd "test.db" {.separator , |
| 77 | .show}] |
| 78 | list [regexp {separator: \",\"} $res] |
| 79 | } {1} |
| 80 | |
| 81 | # import file doesn't exist |
| 82 | do_test shell5-1.4.1 { |
mistachkin | 9ac9931 | 2013-09-13 23:26:47 +0000 | [diff] [blame] | 83 | forcedelete FOO |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 84 | set res [catchcmd "test.db" {CREATE TABLE t1(a, b); |
| 85 | .import FOO t1}] |
| 86 | } {1 {Error: cannot open "FOO"}} |
| 87 | |
| 88 | # empty import file |
| 89 | do_test shell5-1.4.2 { |
mistachkin | 9ac9931 | 2013-09-13 23:26:47 +0000 | [diff] [blame] | 90 | forcedelete shell5.csv |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 91 | set in [open shell5.csv w] |
| 92 | close $in |
| 93 | set res [catchcmd "test.db" {.import shell5.csv t1 |
| 94 | SELECT COUNT(*) FROM t1;}] |
| 95 | } {0 0} |
| 96 | |
| 97 | # import file with 1 row, 1 column (expecting 2 cols) |
| 98 | do_test shell5-1.4.3 { |
| 99 | set in [open shell5.csv w] |
| 100 | puts $in "1" |
| 101 | close $in |
| 102 | set res [catchcmd "test.db" {.import shell5.csv t1}] |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 103 | } {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 104 | |
| 105 | # import file with 1 row, 3 columns (expecting 2 cols) |
| 106 | do_test shell5-1.4.4 { |
| 107 | set in [open shell5.csv w] |
| 108 | puts $in "1|2|3" |
| 109 | close $in |
| 110 | set res [catchcmd "test.db" {.import shell5.csv t1}] |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 111 | } {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 112 | |
| 113 | # import file with 1 row, 2 columns |
| 114 | do_test shell5-1.4.5 { |
| 115 | set in [open shell5.csv w] |
| 116 | puts $in "1|2" |
| 117 | close $in |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 118 | set res [catchcmd "test.db" {DELETE FROM t1; |
| 119 | .import shell5.csv t1 |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 120 | SELECT 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. |
| 126 | do_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 |
| 132 | SELECT COUNT(*) FROM t1;}] |
| 133 | } {0 3} |
| 134 | |
| 135 | # import file with 1 row, 2 columns, using a comma |
| 136 | do_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 |
| 142 | SELECT COUNT(*) FROM t1;}] |
| 143 | } {0 4} |
| 144 | |
| 145 | # import file with 1 row, 2 columns, text data |
| 146 | do_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 |
| 151 | SELECT COUNT(*) FROM t1;}] |
| 152 | } {0 5} |
| 153 | |
| 154 | do_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. |
| 161 | do_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 |
| 166 | SELECT COUNT(*) FROM t1;}] |
| 167 | } {0 6} |
| 168 | |
| 169 | do_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 |
| 174 | do_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 |
| 179 | SELECT COUNT(*) FROM t1;}] |
| 180 | } {0 7} |
| 181 | |
| 182 | do_test shell5-1.4.10.2 { |
| 183 | catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';} |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 184 | } {0 {Now is the time for all good men to come to the aid of their country.}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 185 | |
| 186 | # check importing very long field |
| 187 | do_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 |
| 193 | SELECT 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. |
| 198 | set cols 999 |
| 199 | do_test shell5-1.6.1 { |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 200 | set data {} |
| 201 | for {set i 1} {$i<$cols} {incr i} { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 202 | append data "c$i|" |
| 203 | } |
| 204 | append data "c$cols\n"; |
| 205 | for {set i 1} {$i<$cols} {incr i} { |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 206 | append data "$i|" |
| 207 | } |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 208 | append data "$cols" |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 209 | set in [open shell5.csv w] |
| 210 | puts $in $data |
| 211 | close $in |
| 212 | set res [catchcmd "test.db" {.import shell5.csv t2 |
| 213 | SELECT COUNT(*) FROM t2;}] |
| 214 | } {0 1} |
| 215 | |
| 216 | # try importing a large number of rows |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 217 | set rows 9999 |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 218 | do_test shell5-1.7.1 { |
| 219 | set in [open shell5.csv w] |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 220 | puts $in a |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 221 | for {set i 1} {$i<=$rows} {incr i} { |
| 222 | puts $in $i |
| 223 | } |
| 224 | close $in |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 225 | set res [catchcmd "test.db" {.mode csv |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 226 | .import shell5.csv t3 |
| 227 | SELECT COUNT(*) FROM t3;}] |
| 228 | } [list 0 $rows] |
| 229 | |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 230 | # Inport from a pipe. (Unix only, as it requires "awk") |
| 231 | if {$tcl_platform(platform)=="unix"} { |
| 232 | do_test shell5-1.8 { |
mistachkin | 9ac9931 | 2013-09-13 23:26:47 +0000 | [diff] [blame] | 233 | forcedelete test.db |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 234 | catchcmd test.db {.mode csv |
| 235 | .import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1 |
| 236 | SELECT * FROM t1;} |
| 237 | } {0 {1,"this is 1" |
| 238 | 2,"this is 2" |
| 239 | 3,"this is 3" |
| 240 | 4,"this is 4" |
| 241 | 5,"this is 5"}} |
| 242 | } |
| 243 | |
drh | f7f8de5 | 2013-08-28 11:57:52 +0000 | [diff] [blame] | 244 | # Import columns containing quoted strings |
| 245 | do_test shell5-1.9 { |
| 246 | set out [open shell5.csv w] |
mistachkin | 9ac9931 | 2013-09-13 23:26:47 +0000 | [diff] [blame] | 247 | fconfigure $out -translation lf |
drh | f7f8de5 | 2013-08-28 11:57:52 +0000 | [diff] [blame] | 248 | puts $out {1,"",11} |
| 249 | puts $out {2,"x",22} |
| 250 | puts $out {3,"""",33} |
| 251 | puts $out {4,"hello",44} |
drh | 868ccf2 | 2013-08-28 13:33:40 +0000 | [diff] [blame] | 252 | puts $out "5,55,\"\"\r" |
drh | f7f8de5 | 2013-08-28 11:57:52 +0000 | [diff] [blame] | 253 | puts $out {6,66,"x"} |
| 254 | puts $out {7,77,""""} |
| 255 | puts $out {8,88,"hello"} |
| 256 | puts $out {"",9,99} |
| 257 | puts $out {"x",10,110} |
| 258 | puts $out {"""",11,121} |
| 259 | puts $out {"hello",12,132} |
| 260 | close $out |
mistachkin | 9ac9931 | 2013-09-13 23:26:47 +0000 | [diff] [blame] | 261 | forcedelete test.db |
drh | f7f8de5 | 2013-08-28 11:57:52 +0000 | [diff] [blame] | 262 | catchcmd test.db {.mode csv |
| 263 | CREATE TABLE t1(a,b,c); |
| 264 | .import shell5.csv t1 |
| 265 | } |
| 266 | sqlite3 db test.db |
| 267 | db eval {SELECT *, '|' FROM t1 ORDER BY rowid} |
| 268 | } {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 |} |
| 269 | db close |
| 270 | |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 271 | # Import columns containing quoted strings |
| 272 | do_test shell5-1.10 { |
| 273 | set out [open shell5.csv w] |
| 274 | fconfigure $out -translation lf |
| 275 | puts $out {column1,column2,column3,column4} |
| 276 | puts $out "field1,field2,\"x3 \"\"\r\ndata\"\" 3\",field4" |
| 277 | puts $out "x1,x2,\"x3 \"\"\ndata\"\" 3\",x4" |
| 278 | close $out |
| 279 | forcedelete test.db |
| 280 | catchcmd test.db {.mode csv |
| 281 | CREATE TABLE t1(a,b,c,d); |
| 282 | .import shell5.csv t1 |
| 283 | } |
| 284 | sqlite3 db test.db |
| 285 | db eval {SELECT hex(c) FROM t1 ORDER BY rowid} |
| 286 | } {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033} |
| 287 | |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 288 | # Blank last column with \r\n line endings. |
| 289 | do_test shell5-1.11 { |
| 290 | set out [open shell5.csv w] |
| 291 | fconfigure $out -translation binary |
| 292 | puts $out "column1,column2,column3\r" |
| 293 | puts $out "a,b, \r" |
| 294 | puts $out "x,y,\r" |
| 295 | puts $out "p,q,r\r" |
| 296 | close $out |
| 297 | catch {db close} |
| 298 | forcedelete test.db |
| 299 | catchcmd test.db {.mode csv |
| 300 | .import shell5.csv t1 |
| 301 | } |
| 302 | sqlite3 db test.db |
| 303 | db eval {SELECT *, '|' FROM t1} |
| 304 | } {a b { } | x y {} | p q r |} |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 305 | db close |
| 306 | |
dan | 6a8ac85 | 2014-05-26 18:27:12 +0000 | [diff] [blame] | 307 | #---------------------------------------------------------------------------- |
| 308 | # |
| 309 | reset_db |
| 310 | sqlite3 db test.db |
| 311 | do_test shell5-2.1 { |
| 312 | set fd [open shell5.csv w] |
| 313 | puts $fd ",hello" |
| 314 | close $fd |
| 315 | catchcmd test.db [string trim { |
| 316 | .mode csv |
| 317 | CREATE TABLE t1(a, b); |
| 318 | .import shell5.csv t1 |
| 319 | }] |
| 320 | db eval { SELECT * FROM t1 } |
| 321 | } {{} hello} |
| 322 | |
| 323 | do_test shell5-2.2 { |
| 324 | set fd [open shell5.csv w] |
| 325 | puts $fd {"",hello} |
| 326 | close $fd |
| 327 | catchcmd test.db [string trim { |
| 328 | .mode csv |
| 329 | CREATE TABLE t2(a, b); |
| 330 | .import shell5.csv t2 |
| 331 | }] |
| 332 | db eval { SELECT * FROM t2 } |
| 333 | } {{} hello} |
| 334 | |
| 335 | do_test shell5-2.3 { |
| 336 | set fd [open shell5.csv w] |
| 337 | puts $fd {"x""y",hello} |
| 338 | close $fd |
| 339 | catchcmd test.db [string trim { |
| 340 | .mode csv |
| 341 | CREATE TABLE t3(a, b); |
| 342 | .import shell5.csv t3 |
| 343 | }] |
| 344 | db eval { SELECT * FROM t3 } |
| 345 | } {x\"y hello} |
| 346 | |
| 347 | do_test shell5-2.4 { |
| 348 | set fd [open shell5.csv w] |
| 349 | puts $fd {"xy""",hello} |
| 350 | close $fd |
| 351 | catchcmd test.db [string trim { |
| 352 | .mode csv |
| 353 | CREATE TABLE t4(a, b); |
| 354 | .import shell5.csv t4 |
| 355 | }] |
| 356 | db eval { SELECT * FROM t4 } |
| 357 | } {xy\" hello} |
| 358 | |
| 359 | |
| 360 | |
drh | 8df9185 | 2012-04-24 12:46:05 +0000 | [diff] [blame] | 361 | finish_test |