sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | #!sqlite3 |
| 2 | # |
| 3 | # 2025-12-28 |
| 4 | # |
| 5 | # The author disclaims copyright to this source code. In place of |
| 6 | # a legal notice, here is a blessing: |
| 7 | # |
| 8 | # May you do good and not evil. |
| 9 | # May you find forgiveness for yourself and forgive others. |
| 10 | # May you share freely, never taking more than you give. |
| 11 | # |
| 12 | #*********************************************************************** |
| 13 | # |
| 14 | # Test cases for the ".import" command of the CLI. |
| 15 | # To run these tests: |
| 16 | # |
| 17 | # ./sqlite3 test/import01.sql |
| 18 | # |
| 19 | |
| 20 | .testcase setup |
| 21 | .open :memory: |
| 22 | .mode tty |
| 23 | .check '' |
| 24 | |
| 25 | .if NOT capable('vtab') |
| 26 | .exit |
| 27 | .endif |
| 28 | |
| 29 | .testcase 100 |
| 30 | CREATE TABLE t1(a,b,c); |
| 31 | .import -csv <<END t1 |
| 32 | 111,222,333 |
| 33 | abc,def,ghi |
| 34 | END |
| 35 | SELECT * FROM t1; |
| 36 | .check <<END |
| 37 | ╭───────┬───────┬───────╮ |
| 38 | │ a │ b │ c │ |
| 39 | ╞═══════╪═══════╪═══════╡ |
| 40 | │ '111' │ '222' │ '333' │ |
| 41 | │ abc │ def │ ghi │ |
| 42 | ╰───────┴───────┴───────╯ |
| 43 | END |
| 44 | |
| 45 | .testcase 110 |
| 46 | DELETE FROM t1; |
| 47 | .import -colsep ";" <<END t1 |
| 48 | this;is a;test |
| 49 | one;two;three |
| 50 | END |
| 51 | SELECT * FROM t1; |
| 52 | .check <<END |
| 53 | ╭──────┬──────┬───────╮ |
| 54 | │ a │ b │ c │ |
| 55 | ╞══════╪══════╪═══════╡ |
| 56 | │ this │ is a │ test │ |
| 57 | │ one │ two │ three │ |
| 58 | ╰──────┴──────┴───────╯ |
| 59 | END |
| 60 | |
| 61 | .testcase 120 |
| 62 | DELETE FROM t1; |
| 63 | .import -colsep "," -rowsep ';' <<END t1 |
| 64 | this,is a,test;one,two,three; |
| 65 | END |
| 66 | SELECT * FROM t1; |
| 67 | .check <<END |
| 68 | ╭──────┬──────┬───────╮ |
| 69 | │ a │ b │ c │ |
| 70 | ╞══════╪══════╪═══════╡ |
| 71 | │ this │ is a │ test │ |
| 72 | │ one │ two │ three │ |
| 73 | ╰──────┴──────┴───────╯ |
| 74 | END |
| 75 | |
| 76 | .testcase 130 |
| 77 | DELETE FROM t1; |
| 78 | .import -csv -colsep "," -rowsep "\n" <<END t1 |
| 79 | this,"is a","test ""with quotes""" |
| 80 | "second",,"line" |
| 81 | END |
| 82 | SELECT * FROM t1; |
| 83 | .check <<END |
| 84 | ╭────────┬──────┬────────────────────╮ |
| 85 | │ a │ b │ c │ |
| 86 | ╞════════╪══════╪════════════════════╡ |
| 87 | │ this │ is a │ test "with quotes" │ |
| 88 | │ second │ │ line │ |
| 89 | ╰────────┴──────┴────────────────────╯ |
| 90 | END |
| 91 | .testcase 131 |
| 92 | DELETE FROM t1; |
| 93 | .import -ascii -colsep "," -rowsep "\n" <<END t1 |
| 94 | this,"is a","test ""with quotes""" |
| 95 | "second",,"line" |
| 96 | END |
| 97 | SELECT * FROM t1; |
| 98 | .check <<END |
| 99 | ╭──────────┬────────┬────────────────────────╮ |
| 100 | │ a │ b │ c │ |
| 101 | ╞══════════╪════════╪════════════════════════╡ |
| 102 | │ this │ "is a" │ "test ""with quotes""" │ |
| 103 | │ "second" │ │ "line" │ |
| 104 | ╰──────────┴────────┴────────────────────────╯ |
| 105 | END |
| 106 | |
| 107 | .testcase 140 |
| 108 | DROP TABLE t1; |
| 109 | .import -csv <<END t1 |
| 110 | "abc","def","xy z" |
| 111 | "This","is","a" |
| 112 | "test","...", |
| 113 | END |
| 114 | SELECT * FROM t1; |
| 115 | .check <<END |
| 116 | ╭──────┬─────┬──────╮ |
| 117 | │ abc │ def │ xy z │ |
| 118 | ╞══════╪═════╪══════╡ |
| 119 | │ This │ is │ a │ |
| 120 | │ test │ ... │ │ |
| 121 | ╰──────┴─────┴──────╯ |
| 122 | END |
| 123 | .testcase 141 |
| 124 | SELECT sql FROM sqlite_schema WHERE name='t1'; |
| 125 | .check <<END |
| 126 | ╭───────────────────────╮ |
| 127 | │ sql │ |
| 128 | ╞═══════════════════════╡ |
| 129 | │ CREATE TABLE "t1"( │ |
| 130 | │ "abc", "def", "xy z") │ |
| 131 | ╰───────────────────────╯ |
| 132 | END |
| 133 | |
| 134 | .testcase 150 |
| 135 | DROP TABLE t1; |
| 136 | .import -csv -v <<END t1 |
| 137 | "abc","def","xy z" |
| 138 | "This","is","a" |
| 139 | "test","...", |
| 140 | END |
| 141 | SELECT * FROM t1; |
| 142 | .check <<END |
| 143 | Column separator ",", row separator "\n" |
| 144 | CREATE TABLE "main"."t1"( |
| 145 | "abc", "def", "xy z") |
| 146 | |
| 147 | Added 2 rows with 0 errors using 3 lines of input |
| 148 | ╭──────┬─────┬──────╮ |
| 149 | │ abc │ def │ xy z │ |
| 150 | ╞══════╪═════╪══════╡ |
| 151 | │ This │ is │ a │ |
| 152 | │ test │ ... │ │ |
| 153 | ╰──────┴─────┴──────╯ |
| 154 | END |
| 155 | |
| 156 | .testcase 160 |
| 157 | .mode -reset box |
| 158 | DROP TABLE t1; |
| 159 | .import -csv -schema TEMP <<END t2 |
| 160 | "x" |
| 161 | "abcdef" |
| 162 | END |
| 163 | SELECT * FROM t2; |
| 164 | .tables |
| 165 | .check <<END |
| 166 | ╭────────╮ |
| 167 | │ x │ |
| 168 | ╞════════╡ |
| 169 | │ abcdef │ |
| 170 | ╰────────╯ |
| 171 | temp.t2 |
| 172 | END |
| 173 | |
| 174 | .testcase 170 |
| 175 | .import -csv -skip 2 <<END t3 |
| 176 | a,b,c |
| 177 | d,e,f |
| 178 | g,h,i |
| 179 | j,k,l |
| 180 | m,n,o |
| 181 | END |
| 182 | SELECT * FROM t3; |
| 183 | .check <<END |
| 184 | ╭───┬───┬───╮ |
| 185 | │ g │ h │ i │ |
| 186 | ╞═══╪═══╪═══╡ |
| 187 | │ j │ k │ l │ |
| 188 | │ m │ n │ o │ |
| 189 | ╰───┴───┴───╯ |
| 190 | END |
| 191 | |
| 192 | .testcase 180 |
| 193 | DELETE FROM t3; |
| 194 | .import -csv -skip 7 <<END t3 |
| 195 | a,b,c |
| 196 | d,e,f |
| 197 | g,h,i |
| 198 | j,k,l |
| 199 | m,n,o |
| 200 | END |
| 201 | SELECT * FROM t3; |
| 202 | .check <<END |
| 203 | ╭───┬───┬───╮ |
| 204 | │ g │ h │ i │ |
| 205 | ╘═══╧═══╧═══╛ |
| 206 | END |
| 207 | |
| 208 | .testcase 200 --error-prefix ERROR: |
| 209 | .import -csv |
| 210 | .check 'ERROR: Missing FILE argument' |
| 211 | .testcase 201 |
| 212 | .import -csv file1.csv |
| 213 | .check 'ERROR: Missing TABLE argument' |
| 214 | .testcase --error-prefix test-error: 202 |
| 215 | .import -csvxyzzy file1.csv |
| 216 | .check <<END |
| 217 | test-error: .import -csvxyzzy file1.csv |
| 218 | test-error: ^--- unknown option |
| 219 | END |
| 220 | .testcase 203 |
| 221 | .import -csv file1.csv t4 -colsep |
| 222 | .check <<END |
| 223 | test-error: .import -csv file1.csv t4 -colsep |
| 224 | test-error: missing argument ---^ |
| 225 | END |