copy.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # 2001 September 15
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the COPY statement.
  13. #
  14. # $Id: copy.test,v 1.10 2002/02/19 13:39:23 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create a file of data from which to copy.
  18. #
  19. set f [open data1.txt w]
  20. puts $f "11\t22\t33"
  21. puts $f "22\t33\t11"
  22. close $f
  23. set f [open data2.txt w]
  24. puts $f "11\t22\t33"
  25. puts $f "\\."
  26. puts $f "22\t33\t11"
  27. close $f
  28. set f [open data3.txt w]
  29. puts $f "11\t22\t33\t44"
  30. puts $f "22\t33\t11"
  31. close $f
  32. set f [open data4.txt w]
  33. puts $f "11 | 22 | 33"
  34. puts $f "22 | 33 | 11"
  35. close $f
  36. set f [open data5.txt w]
  37. puts $f "11|22|33"
  38. puts $f "22|33|11"
  39. close $f
  40. # Try to COPY into a non-existant table.
  41. #
  42. do_test copy-1.1 {
  43. set v [catch {execsql {COPY test1 FROM 'data1.txt'}} msg]
  44. lappend v $msg
  45. } {1 {no such table: test1}}
  46. # Try to insert into sqlite_master
  47. #
  48. do_test copy-1.2 {
  49. set v [catch {execsql {COPY sqlite_master FROM 'data2.txt'}} msg]
  50. lappend v $msg
  51. } {1 {table sqlite_master may not be modified}}
  52. # Do some actual inserts
  53. #
  54. do_test copy-1.3 {
  55. execsql {CREATE TABLE test1(one int, two int, three int)}
  56. execsql {COPY test1 FROM 'data1.txt'}
  57. execsql {SELECT * FROM test1 ORDER BY one}
  58. } {11 22 33 22 33 11}
  59. # Make sure input terminates at \.
  60. #
  61. do_test copy-1.4 {
  62. execsql {DELETE FROM test1}
  63. execsql {COPY test1 FROM 'data2.txt'}
  64. execsql {SELECT * FROM test1 ORDER BY one}
  65. } {11 22 33}
  66. # Test out the USING DELIMITERS clause
  67. #
  68. do_test copy-1.5 {
  69. execsql {DELETE FROM test1}
  70. execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS ' | '}
  71. execsql {SELECT * FROM test1 ORDER BY one}
  72. } {11 22 33 22 33 11}
  73. do_test copy-1.6 {
  74. execsql {DELETE FROM test1}
  75. execsql {COPY test1 FROM 'data5.txt' USING DELIMITERS '|'}
  76. execsql {SELECT * FROM test1 ORDER BY one}
  77. } {11 22 33 22 33 11}
  78. do_test copy-1.7 {
  79. execsql {DELETE FROM test1}
  80. execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS '|'}
  81. execsql {SELECT * FROM test1 ORDER BY one}
  82. } {{11 } { 22 } { 33} {22 } { 33 } { 11}}
  83. # Try copying into a table that has one or more indices.
  84. #
  85. do_test copy-1.8 {
  86. execsql {DELETE FROM test1}
  87. execsql {CREATE INDEX index1 ON test1(one)}
  88. execsql {CREATE INDEX index2 ON test1(two)}
  89. execsql {CREATE INDEX index3 ON test1(three)}
  90. execsql {COPY test1 from 'data1.txt'}
  91. execsql {SELECT * FROM test1 WHERE one=11}
  92. } {11 22 33}
  93. do_test copy-1.8b {
  94. execsql {SELECT * FROM test1 WHERE one=22}
  95. } {22 33 11}
  96. do_test copy-1.8c {
  97. execsql {SELECT * FROM test1 WHERE two=22}
  98. } {11 22 33}
  99. do_test copy-1.8d {
  100. execsql {SELECT * FROM test1 WHERE three=11}
  101. } {22 33 11}
  102. # Try inserting really long data
  103. #
  104. set x {}
  105. for {set i 0} {$i<100} {incr i} {
  106. append x "($i)-abcdefghijklmnopqrstyvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-"
  107. }
  108. do_test copy-2.1 {
  109. execsql {CREATE TABLE test2(a int, x text)}
  110. set f [open data21.txt w]
  111. puts $f "123\t$x"
  112. close $f
  113. execsql {COPY test2 FROM 'data21.txt'}
  114. execsql {SELECT x from test2}
  115. } $x
  116. file delete -force data21.txt
  117. # Test the escape character mechanism
  118. #
  119. do_test copy-3.1 {
  120. set fd [open data6.txt w]
  121. puts $fd "hello\\\tworld\t1"
  122. puts $fd "hello\tworld\\\t2"
  123. close $fd
  124. execsql {
  125. CREATE TABLE t1(a text, b text);
  126. COPY t1 FROM 'data6.txt';
  127. SELECT * FROM t1 ORDER BY a;
  128. }
  129. } {hello {world 2} {hello world} 1}
  130. do_test copy-3.2 {
  131. set fd [open data6.txt w]
  132. puts $fd "1\thello\\\nworld"
  133. puts $fd "2\thello world"
  134. close $fd
  135. execsql {
  136. DELETE FROM t1;
  137. COPY t1 FROM 'data6.txt';
  138. SELECT * FROM t1 ORDER BY a;
  139. }
  140. } {1 {hello
  141. world} 2 {hello world}}
  142. # Test the embedded NULL logic.
  143. #
  144. do_test copy-4.1 {
  145. set fd [open data6.txt w]
  146. puts $fd "1\t\\N"
  147. puts $fd "\\N\thello world"
  148. close $fd
  149. execsql {
  150. DELETE FROM t1;
  151. COPY t1 FROM 'data6.txt';
  152. SELECT * FROM t1 WHERE a IS NULL;
  153. }
  154. } {{} {hello world}}
  155. do_test copy-4.2 {
  156. execsql {
  157. SELECT * FROM t1 WHERE b IS NULL;
  158. }
  159. } {1 {}}
  160. # Test the conflict resolution logic for COPY
  161. #
  162. do_test copy-5.1 {
  163. execsql {
  164. DROP TABLE t1;
  165. CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c);
  166. COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
  167. SELECT * FROM t1;
  168. }
  169. } {11 22 33 22 33 11}
  170. do_test copy-5.2 {
  171. set fd [open data6.txt w]
  172. puts $fd "33|22|44"
  173. close $fd
  174. catchsql {
  175. COPY t1 FROM 'data6.txt' USING DELIMITERS '|';
  176. SELECT * FROM t1;
  177. }
  178. } {1 {constraint failed}}
  179. do_test copy-5.3 {
  180. set fd [open data6.txt w]
  181. puts $fd "33|22|44"
  182. close $fd
  183. catchsql {
  184. COPY OR IGNORE t1 FROM 'data6.txt' USING DELIMITERS '|';
  185. SELECT * FROM t1;
  186. }
  187. } {0 {11 22 33 22 33 11}}
  188. do_test copy-5.4 {
  189. set fd [open data6.txt w]
  190. puts $fd "33|22|44"
  191. close $fd
  192. catchsql {
  193. COPY OR REPLACE t1 FROM 'data6.txt' USING DELIMITERS '|';
  194. SELECT * FROM t1;
  195. }
  196. } {0 {22 33 11 33 22 44}}
  197. do_test copy-5.5 {
  198. execsql {
  199. DELETE FROM t1;
  200. PRAGMA count_changes=on;
  201. COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
  202. }
  203. } {2}
  204. do_test copy-5.6 {
  205. execsql {
  206. COPY OR REPLACE t1 FROM 'data5.txt' USING DELIMITERS '|';
  207. }
  208. } {2}
  209. do_test copy-5.7 {
  210. execsql {
  211. COPY OR IGNORE t1 FROM 'data5.txt' USING DELIMITERS '|';
  212. }
  213. } {0}
  214. do_test copy-6.0 {
  215. set x [execsql {PRAGMA integrity_check}]
  216. if {$x==""} {set x ok}
  217. set x
  218. } {ok}
  219. # Cleanup
  220. #
  221. file delete -force data1.txt data2.txt data3.txt data4.txt data5.txt data6.txt
  222. finish_test