shell5.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. # Test plan:
  18. #
  19. # shell5-1.*: Basic tests specific to the ".import" command.
  20. #
  21. set testdir [file dirname $argv0]
  22. source $testdir/tester.tcl
  23. if {$tcl_platform(platform)=="windows"} {
  24. set CLI "sqlite3.exe"
  25. } else {
  26. set CLI "./sqlite3"
  27. }
  28. if {![file executable $CLI]} {
  29. finish_test
  30. return
  31. }
  32. db close
  33. forcedelete test.db test.db-journal test.db-wal
  34. #----------------------------------------------------------------------------
  35. # Test cases shell5-1.*: Basic handling of the .import and .separator commands.
  36. #
  37. # .import FILE TABLE Import data from FILE into TABLE
  38. do_test shell5-1.1.1 {
  39. catchcmd "test.db" ".import"
  40. } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
  41. do_test shell5-1.1.2 {
  42. catchcmd "test.db" ".import FOO"
  43. } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
  44. #do_test shell5-1.1.2 {
  45. # catchcmd "test.db" ".import FOO BAR"
  46. #} {1 {Error: no such table: BAR}}
  47. do_test shell5-1.1.3 {
  48. # too many arguments
  49. catchcmd "test.db" ".import FOO BAR BAD"
  50. } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
  51. # .separator STRING Change separator used by output mode and .import
  52. do_test shell1-1.2.1 {
  53. catchcmd "test.db" ".separator"
  54. } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
  55. do_test shell1-1.2.2 {
  56. catchcmd "test.db" ".separator FOO"
  57. } {0 {}}
  58. do_test shell1-1.2.3 {
  59. # too many arguments
  60. catchcmd "test.db" ".separator FOO BAD"
  61. } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
  62. # separator should default to "|"
  63. do_test shell5-1.3.1 {
  64. set res [catchcmd "test.db" ".show"]
  65. list [regexp {separator: \"\|\"} $res]
  66. } {1}
  67. # set separator to different value.
  68. # check that .show reports new value
  69. do_test shell5-1.3.2 {
  70. set res [catchcmd "test.db" {.separator ,
  71. .show}]
  72. list [regexp {separator: \",\"} $res]
  73. } {1}
  74. # import file doesn't exist
  75. do_test shell5-1.4.1 {
  76. forcedelete FOO
  77. set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
  78. .import FOO t1}]
  79. } {1 {Error: cannot open "FOO"}}
  80. # empty import file
  81. do_test shell5-1.4.2 {
  82. forcedelete shell5.csv
  83. set in [open shell5.csv w]
  84. close $in
  85. set res [catchcmd "test.db" {.import shell5.csv t1
  86. SELECT COUNT(*) FROM t1;}]
  87. } {0 0}
  88. # import file with 1 row, 1 column (expecting 2 cols)
  89. do_test shell5-1.4.3 {
  90. set in [open shell5.csv w]
  91. puts $in "1"
  92. close $in
  93. set res [catchcmd "test.db" {.import shell5.csv t1}]
  94. } {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}}
  95. # import file with 1 row, 3 columns (expecting 2 cols)
  96. do_test shell5-1.4.4 {
  97. set in [open shell5.csv w]
  98. puts $in "1|2|3"
  99. close $in
  100. set res [catchcmd "test.db" {.import shell5.csv t1}]
  101. } {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}}
  102. # import file with 1 row, 2 columns
  103. do_test shell5-1.4.5 {
  104. set in [open shell5.csv w]
  105. puts $in "1|2"
  106. close $in
  107. set res [catchcmd "test.db" {DELETE FROM t1;
  108. .import shell5.csv t1
  109. SELECT COUNT(*) FROM t1;}]
  110. } {0 1}
  111. # import file with 2 rows, 2 columns
  112. # note we end up with 3 rows because of the 1 row
  113. # imported above.
  114. do_test shell5-1.4.6 {
  115. set in [open shell5.csv w]
  116. puts $in "2|3"
  117. puts $in "3|4"
  118. close $in
  119. set res [catchcmd "test.db" {.import shell5.csv t1
  120. SELECT COUNT(*) FROM t1;}]
  121. } {0 3}
  122. # import file with 1 row, 2 columns, using a comma
  123. do_test shell5-1.4.7 {
  124. set in [open shell5.csv w]
  125. puts $in "4,5"
  126. close $in
  127. set res [catchcmd "test.db" {.separator ,
  128. .import shell5.csv t1
  129. SELECT COUNT(*) FROM t1;}]
  130. } {0 4}
  131. # import file with 1 row, 2 columns, text data
  132. do_test shell5-1.4.8.1 {
  133. set in [open shell5.csv w]
  134. puts $in "5|Now is the time for all good men to come to the aid of their country."
  135. close $in
  136. set res [catchcmd "test.db" {.import shell5.csv t1
  137. SELECT COUNT(*) FROM t1;}]
  138. } {0 5}
  139. do_test shell5-1.4.8.2 {
  140. catchcmd "test.db" {SELECT b FROM t1 WHERE a='5';}
  141. } {0 {Now is the time for all good men to come to the aid of their country.}}
  142. # import file with 1 row, 2 columns, quoted text data
  143. # note that currently sqlite doesn't support quoted fields, and
  144. # imports the entire field, quotes and all.
  145. do_test shell5-1.4.9.1 {
  146. set in [open shell5.csv w]
  147. puts $in "6|'Now is the time for all good men to come to the aid of their country.'"
  148. close $in
  149. set res [catchcmd "test.db" {.import shell5.csv t1
  150. SELECT COUNT(*) FROM t1;}]
  151. } {0 6}
  152. do_test shell5-1.4.9.2 {
  153. catchcmd "test.db" {SELECT b FROM t1 WHERE a='6';}
  154. } {0 {'Now is the time for all good men to come to the aid of their country.'}}
  155. # import file with 1 row, 2 columns, quoted text data
  156. do_test shell5-1.4.10.1 {
  157. set in [open shell5.csv w]
  158. puts $in "7|\"Now is the time for all good men to come to the aid of their country.\""
  159. close $in
  160. set res [catchcmd "test.db" {.import shell5.csv t1
  161. SELECT COUNT(*) FROM t1;}]
  162. } {0 7}
  163. do_test shell5-1.4.10.2 {
  164. catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
  165. } {0 {Now is the time for all good men to come to the aid of their country.}}
  166. # check importing very long field
  167. do_test shell5-1.5.1 {
  168. set str [string repeat X 999]
  169. set in [open shell5.csv w]
  170. puts $in "8|$str"
  171. close $in
  172. set res [catchcmd "test.db" {.import shell5.csv t1
  173. SELECT length(b) FROM t1 WHERE a='8';}]
  174. } {0 999}
  175. # try importing into a table with a large number of columns.
  176. # This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
  177. set cols 999
  178. do_test shell5-1.6.1 {
  179. set data {}
  180. for {set i 1} {$i<$cols} {incr i} {
  181. append data "c$i|"
  182. }
  183. append data "c$cols\n";
  184. for {set i 1} {$i<$cols} {incr i} {
  185. append data "$i|"
  186. }
  187. append data "$cols"
  188. set in [open shell5.csv w]
  189. puts $in $data
  190. close $in
  191. set res [catchcmd "test.db" {.import shell5.csv t2
  192. SELECT COUNT(*) FROM t2;}]
  193. } {0 1}
  194. # try importing a large number of rows
  195. set rows 9999
  196. do_test shell5-1.7.1 {
  197. set in [open shell5.csv w]
  198. puts $in a
  199. for {set i 1} {$i<=$rows} {incr i} {
  200. puts $in $i
  201. }
  202. close $in
  203. set res [catchcmd "test.db" {.mode csv
  204. .import shell5.csv t3
  205. SELECT COUNT(*) FROM t3;}]
  206. } [list 0 $rows]
  207. # Inport from a pipe. (Unix only, as it requires "awk")
  208. if {$tcl_platform(platform)=="unix"} {
  209. do_test shell5-1.8 {
  210. forcedelete test.db
  211. catchcmd test.db {.mode csv
  212. .import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1
  213. SELECT * FROM t1;}
  214. } {0 {1,"this is 1"
  215. 2,"this is 2"
  216. 3,"this is 3"
  217. 4,"this is 4"
  218. 5,"this is 5"}}
  219. }
  220. # Import columns containing quoted strings
  221. do_test shell5-1.9 {
  222. set out [open shell5.csv w]
  223. fconfigure $out -translation lf
  224. puts $out {1,"",11}
  225. puts $out {2,"x",22}
  226. puts $out {3,"""",33}
  227. puts $out {4,"hello",44}
  228. puts $out "5,55,\"\"\r"
  229. puts $out {6,66,"x"}
  230. puts $out {7,77,""""}
  231. puts $out {8,88,"hello"}
  232. puts $out {"",9,99}
  233. puts $out {"x",10,110}
  234. puts $out {"""",11,121}
  235. puts $out {"hello",12,132}
  236. close $out
  237. forcedelete test.db
  238. catchcmd test.db {.mode csv
  239. CREATE TABLE t1(a,b,c);
  240. .import shell5.csv t1
  241. }
  242. sqlite3 db test.db
  243. db eval {SELECT *, '|' FROM t1 ORDER BY rowid}
  244. } {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 |}
  245. db close
  246. finish_test