| OLD | NEW |
| 1 # 2010 August 4 | 1 # 2010 August 4 |
| 2 # | 2 # |
| 3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
| 4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
| 5 # | 5 # |
| 6 # May you do good and not evil. | 6 # May you do good and not evil. |
| 7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
| 8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
| 9 # | 9 # |
| 10 #*********************************************************************** | 10 #*********************************************************************** |
| 11 # | 11 # |
| 12 # The focus of this file is testing the CLI shell tool. | 12 # The focus of this file is testing the CLI shell tool. |
| 13 # These tests are specific to the .import command. | 13 # These tests are specific to the .import command. |
| 14 # | 14 # |
| 15 # $Id: shell5.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $ | 15 # $Id: shell5.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $ |
| 16 # | 16 # |
| 17 | 17 |
| 18 # Test plan: | 18 # Test plan: |
| 19 # | 19 # |
| 20 # shell5-1.*: Basic tests specific to the ".import" command. | 20 # shell5-1.*: Basic tests specific to the ".import" command. |
| 21 # | 21 # |
| 22 | 22 set testdir [file dirname $argv0] |
| 23 set CLI "./sqlite3" | 23 source $testdir/tester.tcl |
| 24 | 24 if {$tcl_platform(platform)=="windows"} { |
| 25 proc do_test {name cmd expected} { | 25 set CLI "sqlite3.exe" |
| 26 puts -nonewline "$name ..." | 26 } else { |
| 27 set res [uplevel $cmd] | 27 set CLI "./sqlite3" |
| 28 if {$res eq $expected} { | |
| 29 puts Ok | |
| 30 } else { | |
| 31 puts Error | |
| 32 puts " Got: $res" | |
| 33 puts " Expected: $expected" | |
| 34 exit | |
| 35 } | |
| 36 } | 28 } |
| 37 | 29 if {![file executable $CLI]} { |
| 38 proc catchcmd {db {cmd ""}} { | 30 finish_test |
| 39 global CLI | 31 return |
| 40 set out [open cmds.txt w] | |
| 41 puts $out $cmd | |
| 42 close $out | |
| 43 set line "exec $CLI $db < cmds.txt" | |
| 44 set rc [catch { eval $line } msg] | |
| 45 list $rc $msg | |
| 46 } | 32 } |
| 47 | 33 db close |
| 48 file delete -force test.db test.db.journal | 34 forcedelete test.db test.db-journal test.db-wal |
| 49 | 35 |
| 50 #---------------------------------------------------------------------------- | 36 #---------------------------------------------------------------------------- |
| 51 # Test cases shell5-1.*: Basic handling of the .import and .separator commands. | 37 # Test cases shell5-1.*: Basic handling of the .import and .separator commands. |
| 52 # | 38 # |
| 53 | 39 |
| 54 # .import FILE TABLE Import data from FILE into TABLE | 40 # .import FILE TABLE Import data from FILE into TABLE |
| 55 do_test shell5-1.1.1 { | 41 do_test shell5-1.1.1 { |
| 56 catchcmd "test.db" ".import" | 42 catchcmd "test.db" ".import" |
| 57 } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for
help}} | 43 } {1 {Usage: .import FILE TABLE}} |
| 58 do_test shell5-1.1.2 { | 44 do_test shell5-1.1.2 { |
| 59 catchcmd "test.db" ".import FOO" | 45 catchcmd "test.db" ".import FOO" |
| 60 } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for
help}} | 46 } {1 {Usage: .import FILE TABLE}} |
| 61 do_test shell5-1.1.2 { | 47 #do_test shell5-1.1.2 { |
| 62 catchcmd "test.db" ".import FOO BAR" | 48 # catchcmd "test.db" ".import FOO BAR" |
| 63 } {1 {Error: no such table: BAR}} | 49 #} {1 {Error: no such table: BAR}} |
| 64 do_test shell5-1.1.3 { | 50 do_test shell5-1.1.3 { |
| 65 # too many arguments | 51 # too many arguments |
| 66 catchcmd "test.db" ".import FOO BAR BAD" | 52 catchcmd "test.db" ".import FOO BAR BAD" |
| 67 } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for
help}} | 53 } {1 {Usage: .import FILE TABLE}} |
| 68 | 54 |
| 69 # .separator STRING Change separator used by output mode and .import | 55 # .separator STRING Change separator used by output mode and .import |
| 70 do_test shell1-1.2.1 { | 56 do_test shell5-1.2.1 { |
| 71 catchcmd "test.db" ".separator" | 57 catchcmd "test.db" ".separator" |
| 72 } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" f
or help}} | 58 } {1 {Usage: .separator SEPARATOR ?NEWLINE?}} |
| 73 do_test shell1-1.2.2 { | 59 do_test shell5-1.2.2 { |
| 74 catchcmd "test.db" ".separator FOO" | 60 catchcmd "test.db" ".separator ONE" |
| 75 } {0 {}} | 61 } {0 {}} |
| 76 do_test shell1-1.2.3 { | 62 do_test shell5-1.2.3 { |
| 63 catchcmd "test.db" ".separator ONE TWO" |
| 64 } {0 {}} |
| 65 do_test shell5-1.2.4 { |
| 77 # too many arguments | 66 # too many arguments |
| 78 catchcmd "test.db" ".separator FOO BAD" | 67 catchcmd "test.db" ".separator ONE TWO THREE" |
| 79 } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" f
or help}} | 68 } {1 {Usage: .separator SEPARATOR ?NEWLINE?}} |
| 80 | 69 |
| 81 # separator should default to "|" | 70 # separator should default to "|" |
| 82 do_test shell5-1.3.1 { | 71 do_test shell5-1.3.1 { |
| 83 set res [catchcmd "test.db" ".show"] | 72 set res [catchcmd "test.db" ".show"] |
| 84 list [regexp {separator: \"\|\"} $res] | 73 list [regexp {separator: \"\|\"} $res] |
| 85 } {1} | 74 } {1} |
| 86 | 75 |
| 87 # set separator to different value. | 76 # set separator to different value. |
| 88 # check that .show reports new value | 77 # check that .show reports new value |
| 89 do_test shell5-1.3.2 { | 78 do_test shell5-1.3.2 { |
| 90 set res [catchcmd "test.db" {.separator , | 79 set res [catchcmd "test.db" {.separator , |
| 91 .show}] | 80 .show}] |
| 92 list [regexp {separator: \",\"} $res] | 81 list [regexp {separator: \",\"} $res] |
| 93 } {1} | 82 } {1} |
| 94 | 83 |
| 95 # import file doesn't exist | 84 # import file doesn't exist |
| 96 do_test shell5-1.4.1 { | 85 do_test shell5-1.4.1 { |
| 97 file delete -force FOO | 86 forcedelete FOO |
| 98 set res [catchcmd "test.db" {CREATE TABLE t1(a, b); | 87 set res [catchcmd "test.db" {CREATE TABLE t1(a, b); |
| 99 .import FOO t1}] | 88 .import FOO t1}] |
| 100 } {1 {Error: cannot open "FOO"}} | 89 } {1 {Error: cannot open "FOO"}} |
| 101 | 90 |
| 102 # empty import file | 91 # empty import file |
| 103 do_test shell5-1.4.2 { | 92 do_test shell5-1.4.2 { |
| 104 file delete -force shell5.csv | 93 forcedelete shell5.csv |
| 105 set in [open shell5.csv w] | 94 set in [open shell5.csv w] |
| 106 close $in | 95 close $in |
| 107 set res [catchcmd "test.db" {.import shell5.csv t1 | 96 set res [catchcmd "test.db" {.import shell5.csv t1 |
| 108 SELECT COUNT(*) FROM t1;}] | 97 SELECT COUNT(*) FROM t1;}] |
| 109 } {0 0} | 98 } {0 0} |
| 110 | 99 |
| 111 # import file with 1 row, 1 column (expecting 2 cols) | 100 # import file with 1 row, 1 column (expecting 2 cols) |
| 112 do_test shell5-1.4.3 { | 101 do_test shell5-1.4.3 { |
| 113 set in [open shell5.csv w] | 102 set in [open shell5.csv w] |
| 114 puts $in "1" | 103 puts $in "1" |
| 115 close $in | 104 close $in |
| 116 set res [catchcmd "test.db" {.import shell5.csv t1}] | 105 set res [catchcmd "test.db" {.import shell5.csv t1}] |
| 117 } {1 {Error: shell5.csv line 1: expected 2 columns of data but found 1}} | 106 } {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}
} |
| 118 | 107 |
| 119 # import file with 1 row, 3 columns (expecting 2 cols) | 108 # import file with 1 row, 3 columns (expecting 2 cols) |
| 120 do_test shell5-1.4.4 { | 109 do_test shell5-1.4.4 { |
| 121 set in [open shell5.csv w] | 110 set in [open shell5.csv w] |
| 122 puts $in "1|2|3" | 111 puts $in "1|2|3" |
| 123 close $in | 112 close $in |
| 124 set res [catchcmd "test.db" {.import shell5.csv t1}] | 113 set res [catchcmd "test.db" {.import shell5.csv t1}] |
| 125 } {1 {Error: shell5.csv line 1: expected 2 columns of data but found 3}} | 114 } {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}} |
| 126 | 115 |
| 127 # import file with 1 row, 2 columns | 116 # import file with 1 row, 2 columns |
| 128 do_test shell5-1.4.5 { | 117 do_test shell5-1.4.5 { |
| 129 set in [open shell5.csv w] | 118 set in [open shell5.csv w] |
| 130 puts $in "1|2" | 119 puts $in "1|2" |
| 131 close $in | 120 close $in |
| 132 set res [catchcmd "test.db" {.import shell5.csv t1 | 121 set res [catchcmd "test.db" {DELETE FROM t1; |
| 122 .import shell5.csv t1 |
| 133 SELECT COUNT(*) FROM t1;}] | 123 SELECT COUNT(*) FROM t1;}] |
| 134 } {0 1} | 124 } {0 1} |
| 135 | 125 |
| 136 # import file with 2 rows, 2 columns | 126 # import file with 2 rows, 2 columns |
| 137 # note we end up with 3 rows because of the 1 row | 127 # note we end up with 3 rows because of the 1 row |
| 138 # imported above. | 128 # imported above. |
| 139 do_test shell5-1.4.6 { | 129 do_test shell5-1.4.6 { |
| 140 set in [open shell5.csv w] | 130 set in [open shell5.csv w] |
| 141 puts $in "2|3" | 131 puts $in "2|3" |
| 142 puts $in "3|4" | 132 puts $in "3|4" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 do_test shell5-1.4.10.1 { | 177 do_test shell5-1.4.10.1 { |
| 188 set in [open shell5.csv w] | 178 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 cou
ntry.\"" | 179 puts $in "7|\"Now is the time for all good men to come to the aid of their cou
ntry.\"" |
| 190 close $in | 180 close $in |
| 191 set res [catchcmd "test.db" {.import shell5.csv t1 | 181 set res [catchcmd "test.db" {.import shell5.csv t1 |
| 192 SELECT COUNT(*) FROM t1;}] | 182 SELECT COUNT(*) FROM t1;}] |
| 193 } {0 7} | 183 } {0 7} |
| 194 | 184 |
| 195 do_test shell5-1.4.10.2 { | 185 do_test shell5-1.4.10.2 { |
| 196 catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';} | 186 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."}} | 187 } {0 {Now is the time for all good men to come to the aid of their country.}} |
| 198 | 188 |
| 199 # check importing very long field | 189 # check importing very long field |
| 200 do_test shell5-1.5.1 { | 190 do_test shell5-1.5.1 { |
| 201 set str [string repeat X 999] | 191 set str [string repeat X 999] |
| 202 set in [open shell5.csv w] | 192 set in [open shell5.csv w] |
| 203 puts $in "8|$str" | 193 puts $in "8|$str" |
| 204 close $in | 194 close $in |
| 205 set res [catchcmd "test.db" {.import shell5.csv t1 | 195 set res [catchcmd "test.db" {.import shell5.csv t1 |
| 206 SELECT length(b) FROM t1 WHERE a='8';}] | 196 SELECT length(b) FROM t1 WHERE a='8';}] |
| 207 } {0 999} | 197 } {0 999} |
| 208 | 198 |
| 209 # try importing into a table with a large number of columns. | 199 # try importing into a table with a large number of columns. |
| 210 # This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999. | 200 # This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999. |
| 211 set cols 999 | 201 set cols 999 |
| 212 do_test shell5-1.6.1 { | 202 do_test shell5-1.6.1 { |
| 213 set sql {CREATE TABLE t2(} | |
| 214 set data {} | 203 set data {} |
| 215 for {set i 1} {$i<$cols} {incr i} { | 204 for {set i 1} {$i<$cols} {incr i} { |
| 216 append sql "c$i," | 205 append data "c$i|" |
| 206 } |
| 207 append data "c$cols\n"; |
| 208 for {set i 1} {$i<$cols} {incr i} { |
| 217 append data "$i|" | 209 append data "$i|" |
| 218 } | 210 } |
| 219 append sql "c$cols);" | |
| 220 append data "$cols" | 211 append data "$cols" |
| 221 catchcmd "test.db" $sql | |
| 222 set in [open shell5.csv w] | 212 set in [open shell5.csv w] |
| 223 puts $in $data | 213 puts $in $data |
| 224 close $in | 214 close $in |
| 225 set res [catchcmd "test.db" {.import shell5.csv t2 | 215 set res [catchcmd "test.db" {.import shell5.csv t2 |
| 226 SELECT COUNT(*) FROM t2;}] | 216 SELECT COUNT(*) FROM t2;}] |
| 227 } {0 1} | 217 } {0 1} |
| 228 | 218 |
| 229 # try importing a large number of rows | 219 # try importing a large number of rows |
| 230 set rows 999999 | 220 set rows 9999 |
| 231 do_test shell5-1.7.1 { | 221 do_test shell5-1.7.1 { |
| 232 set in [open shell5.csv w] | 222 set in [open shell5.csv w] |
| 223 puts $in a |
| 233 for {set i 1} {$i<=$rows} {incr i} { | 224 for {set i 1} {$i<=$rows} {incr i} { |
| 234 puts $in $i | 225 puts $in $i |
| 235 } | 226 } |
| 236 close $in | 227 close $in |
| 237 set res [catchcmd "test.db" {CREATE TABLE t3(a); | 228 set res [catchcmd "test.db" {.mode csv |
| 238 .import shell5.csv t3 | 229 .import shell5.csv t3 |
| 239 SELECT COUNT(*) FROM t3;}] | 230 SELECT COUNT(*) FROM t3;}] |
| 240 } [list 0 $rows] | 231 } [list 0 $rows] |
| 241 | 232 |
| 233 # Inport from a pipe. (Unix only, as it requires "awk") |
| 234 if {$tcl_platform(platform)=="unix"} { |
| 235 do_test shell5-1.8 { |
| 236 forcedelete test.db |
| 237 catchcmd test.db {.mode csv |
| 238 .import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t
1 |
| 239 SELECT * FROM t1;} |
| 240 } {0 {1,"this is 1" |
| 241 2,"this is 2" |
| 242 3,"this is 3" |
| 243 4,"this is 4" |
| 244 5,"this is 5"}} |
| 245 } |
| 242 | 246 |
| 243 puts "CLI tests completed successfully" | 247 # Import columns containing quoted strings |
| 248 do_test shell5-1.9 { |
| 249 set out [open shell5.csv w] |
| 250 fconfigure $out -translation lf |
| 251 puts $out {1,"",11} |
| 252 puts $out {2,"x",22} |
| 253 puts $out {3,"""",33} |
| 254 puts $out {4,"hello",44} |
| 255 puts $out "5,55,\"\"\r" |
| 256 puts $out {6,66,"x"} |
| 257 puts $out {7,77,""""} |
| 258 puts $out {8,88,"hello"} |
| 259 puts $out {"",9,99} |
| 260 puts $out {"x",10,110} |
| 261 puts $out {"""",11,121} |
| 262 puts $out {"hello",12,132} |
| 263 close $out |
| 264 forcedelete test.db |
| 265 catchcmd test.db {.mode csv |
| 266 CREATE TABLE t1(a,b,c); |
| 267 .import shell5.csv t1 |
| 268 } |
| 269 sqlite3 db test.db |
| 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 |} |
| 272 db close |
| 273 |
| 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" |
| 281 close $out |
| 282 forcedelete test.db |
| 283 catchcmd test.db {.mode csv |
| 284 CREATE TABLE t1(a,b,c,d); |
| 285 .import shell5.csv t1 |
| 286 } |
| 287 sqlite3 db test.db |
| 288 db eval {SELECT hex(c) FROM t1 ORDER BY rowid} |
| 289 } {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033} |
| 290 |
| 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" |
| 296 puts $out "a,b, \r" |
| 297 puts $out "x,y,\r" |
| 298 puts $out "p,q,r\r" |
| 299 close $out |
| 300 catch {db close} |
| 301 forcedelete test.db |
| 302 catchcmd test.db {.mode csv |
| 303 .import shell5.csv t1 |
| 304 } |
| 305 sqlite3 db test.db |
| 306 db eval {SELECT *, '|' FROM t1} |
| 307 } {a b { } | x y {} | p q r |} |
| 308 db close |
| 309 |
| 310 #---------------------------------------------------------------------------- |
| 311 # |
| 312 reset_db |
| 313 sqlite3 db test.db |
| 314 do_test shell5-2.1 { |
| 315 set fd [open shell5.csv w] |
| 316 puts $fd ",hello" |
| 317 close $fd |
| 318 catchcmd test.db [string trim { |
| 319 .mode csv |
| 320 CREATE TABLE t1(a, b); |
| 321 .import shell5.csv t1 |
| 322 }] |
| 323 db eval { SELECT * FROM t1 } |
| 324 } {{} hello} |
| 325 |
| 326 do_test shell5-2.2 { |
| 327 set fd [open shell5.csv w] |
| 328 puts $fd {"",hello} |
| 329 close $fd |
| 330 catchcmd test.db [string trim { |
| 331 .mode csv |
| 332 CREATE TABLE t2(a, b); |
| 333 .import shell5.csv t2 |
| 334 }] |
| 335 db eval { SELECT * FROM t2 } |
| 336 } {{} hello} |
| 337 |
| 338 do_test shell5-2.3 { |
| 339 set fd [open shell5.csv w] |
| 340 puts $fd {"x""y",hello} |
| 341 close $fd |
| 342 catchcmd test.db [string trim { |
| 343 .mode csv |
| 344 CREATE TABLE t3(a, b); |
| 345 .import shell5.csv t3 |
| 346 }] |
| 347 db eval { SELECT * FROM t3 } |
| 348 } {x\"y hello} |
| 349 |
| 350 do_test shell5-2.4 { |
| 351 set fd [open shell5.csv w] |
| 352 puts $fd {"xy""",hello} |
| 353 close $fd |
| 354 catchcmd test.db [string trim { |
| 355 .mode csv |
| 356 CREATE TABLE t4(a, b); |
| 357 .import shell5.csv t4 |
| 358 }] |
| 359 db eval { SELECT * FROM t4 } |
| 360 } {xy\" hello} |
| 361 |
| 362 do_test shell5-2.5 { |
| 363 set fd [open shell5.csv w] |
| 364 puts $fd {"one","2"} |
| 365 puts $fd {} |
| 366 close $fd |
| 367 catchcmd test.db [string trim { |
| 368 .mode csv |
| 369 CREATE TABLE t4(a, b); |
| 370 .import shell5.csv t4 |
| 371 }] |
| 372 db eval { SELECT * FROM t4 } |
| 373 } {xy\" hello one 2 {} {}} |
| 374 |
| 375 |
| 376 finish_test |
| OLD | NEW |