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 |
| 35 | sqlite3 db test.db |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 36 | |
| 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 |
| 42 | do_test shell5-1.1.1 { |
| 43 | catchcmd "test.db" ".import" |
| 44 | } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}} |
| 45 | do_test shell5-1.1.2 { |
| 46 | catchcmd "test.db" ".import FOO" |
| 47 | } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}} |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 48 | #do_test shell5-1.1.2 { |
| 49 | # catchcmd "test.db" ".import FOO BAR" |
| 50 | #} {1 {Error: no such table: BAR}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 51 | do_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 |
| 57 | do_test shell1-1.2.1 { |
| 58 | catchcmd "test.db" ".separator" |
| 59 | } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}} |
| 60 | do_test shell1-1.2.2 { |
| 61 | catchcmd "test.db" ".separator FOO" |
| 62 | } {0 {}} |
| 63 | do_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 "|" |
| 69 | do_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 |
| 76 | do_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 |
| 83 | do_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 |
| 90 | do_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 |
| 95 | SELECT COUNT(*) FROM t1;}] |
| 96 | } {0 0} |
| 97 | |
| 98 | # import file with 1 row, 1 column (expecting 2 cols) |
| 99 | do_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}] |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 104 | } {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] | 105 | |
| 106 | # import file with 1 row, 3 columns (expecting 2 cols) |
| 107 | do_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}] |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 112 | } {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}} |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 113 | |
| 114 | # import file with 1 row, 2 columns |
| 115 | do_test shell5-1.4.5 { |
| 116 | set in [open shell5.csv w] |
| 117 | puts $in "1|2" |
| 118 | close $in |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 119 | set res [catchcmd "test.db" {DELETE FROM t1; |
| 120 | .import shell5.csv t1 |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 121 | SELECT COUNT(*) FROM t1;}] |
| 122 | } {0 1} |
| 123 | |
| 124 | # import file with 2 rows, 2 columns |
| 125 | # note we end up with 3 rows because of the 1 row |
| 126 | # imported above. |
| 127 | do_test shell5-1.4.6 { |
| 128 | set in [open shell5.csv w] |
| 129 | puts $in "2|3" |
| 130 | puts $in "3|4" |
| 131 | close $in |
| 132 | set res [catchcmd "test.db" {.import shell5.csv t1 |
| 133 | SELECT COUNT(*) FROM t1;}] |
| 134 | } {0 3} |
| 135 | |
| 136 | # import file with 1 row, 2 columns, using a comma |
| 137 | do_test shell5-1.4.7 { |
| 138 | set in [open shell5.csv w] |
| 139 | puts $in "4,5" |
| 140 | close $in |
| 141 | set res [catchcmd "test.db" {.separator , |
| 142 | .import shell5.csv t1 |
| 143 | SELECT COUNT(*) FROM t1;}] |
| 144 | } {0 4} |
| 145 | |
| 146 | # import file with 1 row, 2 columns, text data |
| 147 | do_test shell5-1.4.8.1 { |
| 148 | set in [open shell5.csv w] |
| 149 | puts $in "5|Now is the time for all good men to come to the aid of their country." |
| 150 | close $in |
| 151 | set res [catchcmd "test.db" {.import shell5.csv t1 |
| 152 | SELECT COUNT(*) FROM t1;}] |
| 153 | } {0 5} |
| 154 | |
| 155 | do_test shell5-1.4.8.2 { |
| 156 | catchcmd "test.db" {SELECT b FROM t1 WHERE a='5';} |
| 157 | } {0 {Now is the time for all good men to come to the aid of their country.}} |
| 158 | |
| 159 | # import file with 1 row, 2 columns, quoted text data |
| 160 | # note that currently sqlite doesn't support quoted fields, and |
| 161 | # imports the entire field, quotes and all. |
| 162 | do_test shell5-1.4.9.1 { |
| 163 | set in [open shell5.csv w] |
| 164 | puts $in "6|'Now is the time for all good men to come to the aid of their country.'" |
| 165 | close $in |
| 166 | set res [catchcmd "test.db" {.import shell5.csv t1 |
| 167 | SELECT COUNT(*) FROM t1;}] |
| 168 | } {0 6} |
| 169 | |
| 170 | do_test shell5-1.4.9.2 { |
| 171 | catchcmd "test.db" {SELECT b FROM t1 WHERE a='6';} |
| 172 | } {0 {'Now is the time for all good men to come to the aid of their country.'}} |
| 173 | |
| 174 | # import file with 1 row, 2 columns, quoted text data |
| 175 | do_test shell5-1.4.10.1 { |
| 176 | set in [open shell5.csv w] |
| 177 | puts $in "7|\"Now is the time for all good men to come to the aid of their country.\"" |
| 178 | close $in |
| 179 | set res [catchcmd "test.db" {.import shell5.csv t1 |
| 180 | SELECT COUNT(*) FROM t1;}] |
| 181 | } {0 7} |
| 182 | |
| 183 | do_test shell5-1.4.10.2 { |
| 184 | catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';} |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 185 | } {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] | 186 | |
| 187 | # check importing very long field |
| 188 | do_test shell5-1.5.1 { |
| 189 | set str [string repeat X 999] |
| 190 | set in [open shell5.csv w] |
| 191 | puts $in "8|$str" |
| 192 | close $in |
| 193 | set res [catchcmd "test.db" {.import shell5.csv t1 |
| 194 | SELECT length(b) FROM t1 WHERE a='8';}] |
| 195 | } {0 999} |
| 196 | |
| 197 | # try importing into a table with a large number of columns. |
| 198 | # This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999. |
| 199 | set cols 999 |
| 200 | do_test shell5-1.6.1 { |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 201 | set data {} |
| 202 | for {set i 1} {$i<$cols} {incr i} { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 203 | append data "c$i|" |
| 204 | } |
| 205 | append data "c$cols\n"; |
| 206 | for {set i 1} {$i<$cols} {incr i} { |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 207 | append data "$i|" |
| 208 | } |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 209 | append data "$cols" |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 210 | set in [open shell5.csv w] |
| 211 | puts $in $data |
| 212 | close $in |
| 213 | set res [catchcmd "test.db" {.import shell5.csv t2 |
| 214 | SELECT COUNT(*) FROM t2;}] |
| 215 | } {0 1} |
| 216 | |
| 217 | # try importing a large number of rows |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 218 | set rows 9999 |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 219 | do_test shell5-1.7.1 { |
| 220 | set in [open shell5.csv w] |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 221 | puts $in a |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 222 | for {set i 1} {$i<=$rows} {incr i} { |
| 223 | puts $in $i |
| 224 | } |
| 225 | close $in |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 226 | set res [catchcmd "test.db" {.mode csv |
shaneh | 1b8f78c | 2010-08-18 17:16:26 +0000 | [diff] [blame] | 227 | .import shell5.csv t3 |
| 228 | SELECT COUNT(*) FROM t3;}] |
| 229 | } [list 0 $rows] |
| 230 | |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 231 | # Inport from a pipe. (Unix only, as it requires "awk") |
| 232 | if {$tcl_platform(platform)=="unix"} { |
| 233 | do_test shell5-1.8 { |
| 234 | file delete -force test.db |
| 235 | catchcmd test.db {.mode csv |
| 236 | .import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1 |
| 237 | SELECT * FROM t1;} |
| 238 | } {0 {1,"this is 1" |
| 239 | 2,"this is 2" |
| 240 | 3,"this is 3" |
| 241 | 4,"this is 4" |
| 242 | 5,"this is 5"}} |
| 243 | } |
| 244 | |
drh | f7f8de5 | 2013-08-28 11:57:52 +0000 | [diff] [blame] | 245 | # Import columns containing quoted strings |
| 246 | do_test shell5-1.9 { |
| 247 | set out [open shell5.csv w] |
| 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 |
| 261 | file delete -force test.db |
| 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 | 8df9185 | 2012-04-24 12:46:05 +0000 | [diff] [blame] | 271 | finish_test |