insert.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 INSERT statement.
  13. #
  14. # $Id: insert.test,v 1.10 2002/04/12 13:11:53 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Try to insert into a non-existant table.
  18. #
  19. do_test insert-1.1 {
  20. set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
  21. lappend v $msg
  22. } {1 {no such table: test1}}
  23. # Try to insert into sqlite_master
  24. #
  25. do_test insert-1.2 {
  26. set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
  27. lappend v $msg
  28. } {1 {table sqlite_master may not be modified}}
  29. # Try to insert the wrong number of entries.
  30. #
  31. do_test insert-1.3 {
  32. execsql {CREATE TABLE test1(one int, two int, three int)}
  33. set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
  34. lappend v $msg
  35. } {1 {table test1 has 3 columns but 2 values were supplied}}
  36. do_test insert-1.3b {
  37. set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
  38. lappend v $msg
  39. } {1 {table test1 has 3 columns but 4 values were supplied}}
  40. do_test insert-1.3c {
  41. set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
  42. lappend v $msg
  43. } {1 {4 values for 2 columns}}
  44. do_test insert-1.3d {
  45. set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
  46. lappend v $msg
  47. } {1 {1 values for 2 columns}}
  48. # Try to insert into a non-existant column of a table.
  49. #
  50. do_test insert-1.4 {
  51. set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
  52. lappend v $msg
  53. } {1 {table test1 has no column named four}}
  54. # Make sure the inserts actually happen
  55. #
  56. do_test insert-1.5 {
  57. execsql {INSERT INTO test1 VALUES(1,2,3)}
  58. execsql {SELECT * FROM test1}
  59. } {1 2 3}
  60. do_test insert-1.5b {
  61. execsql {INSERT INTO test1 VALUES(4,5,6)}
  62. execsql {SELECT * FROM test1 ORDER BY one}
  63. } {1 2 3 4 5 6}
  64. do_test insert-1.5c {
  65. execsql {INSERT INTO test1 VALUES(7,8,9)}
  66. execsql {SELECT * FROM test1 ORDER BY one}
  67. } {1 2 3 4 5 6 7 8 9}
  68. do_test insert-1.6 {
  69. execsql {DELETE FROM test1}
  70. execsql {INSERT INTO test1(one,two) VALUES(1,2)}
  71. execsql {SELECT * FROM test1 ORDER BY one}
  72. } {1 2 {}}
  73. do_test insert-1.6b {
  74. execsql {INSERT INTO test1(two,three) VALUES(5,6)}
  75. execsql {SELECT * FROM test1 ORDER BY one}
  76. } {{} 5 6 1 2 {}}
  77. do_test insert-1.6c {
  78. execsql {INSERT INTO test1(three,one) VALUES(7,8)}
  79. execsql {SELECT * FROM test1 ORDER BY one}
  80. } {{} 5 6 1 2 {} 8 {} 7}
  81. # A table to use for testing default values
  82. #
  83. do_test insert-2.1 {
  84. execsql {
  85. CREATE TABLE test2(
  86. f1 int default -111,
  87. f2 real default +4.32,
  88. f3 int default +222,
  89. f4 int default 7.89
  90. )
  91. }
  92. execsql {SELECT * from test2}
  93. } {}
  94. do_test insert-2.2 {
  95. execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
  96. execsql {SELECT * FROM test2}
  97. } {10 4.32 -10 7.89}
  98. do_test insert-2.3 {
  99. execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
  100. execsql {SELECT * FROM test2 WHERE f1==-111}
  101. } {-111 1.23 222 -3.45}
  102. do_test insert-2.4 {
  103. execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
  104. execsql {SELECT * FROM test2 WHERE f1==77}
  105. } {77 1.23 222 3.45}
  106. do_test insert-2.10 {
  107. execsql {
  108. DROP TABLE test2;
  109. CREATE TABLE test2(
  110. f1 int default 111,
  111. f2 real default -4.32,
  112. f3 text default hi,
  113. f4 text default 'abc-123',
  114. f5 varchar(10)
  115. )
  116. }
  117. execsql {SELECT * from test2}
  118. } {}
  119. do_test insert-2.11 {
  120. execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
  121. execsql {SELECT * FROM test2}
  122. } {111 -2.22 hi hi! {}}
  123. do_test insert-2.12 {
  124. execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
  125. execsql {SELECT * FROM test2 ORDER BY f1}
  126. } {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
  127. # Do additional inserts with default values, but this time
  128. # on a table that has indices. In particular we want to verify
  129. # that the correct default values are inserted into the indices.
  130. #
  131. do_test insert-3.1 {
  132. execsql {
  133. DELETE FROM test2;
  134. CREATE INDEX index9 ON test2(f1,f2);
  135. CREATE INDEX indext ON test2(f4,f5);
  136. SELECT * from test2;
  137. }
  138. } {}
  139. do_test insert-3.2 {
  140. execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
  141. execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
  142. } {111 -3.33 hi hum {}}
  143. do_test insert-3.3 {
  144. execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
  145. execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
  146. } {111 -3.33 hi hum {}}
  147. do_test insert-3.4 {
  148. execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
  149. } {22 -4.44 hi abc-123 wham}
  150. do_test insert-3.5 {
  151. set x [execsql {PRAGMA integrity_check}]
  152. if {$x==""} {set x ok}
  153. set x
  154. } {ok}
  155. # Test of expressions in the VALUES clause
  156. #
  157. do_test insert-4.1 {
  158. execsql {
  159. CREATE TABLE t3(a,b,c);
  160. INSERT INTO t3 VALUES(1+2+3,4,5);
  161. SELECT * FROM t3;
  162. }
  163. } {6 4 5}
  164. do_test insert-4.2 {
  165. execsql {
  166. INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);
  167. SELECT * FROM t3 ORDER BY a;
  168. }
  169. } {6 4 5 7 5 6}
  170. do_test insert-4.3 {
  171. catchsql {
  172. INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
  173. SELECT * FROM t3 ORDER BY a;
  174. }
  175. } {1 {no such column: t3.a}}
  176. do_test insert-4.4 {
  177. execsql {
  178. INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);
  179. SELECT * FROM t3 ORDER BY a;
  180. }
  181. } {{} 6 7 6 4 5 7 5 6}
  182. do_test insert-4.5 {
  183. execsql {
  184. SELECT b,c FROM t3 WHERE a IS NULL;
  185. }
  186. } {6 7}
  187. do_test insert-4.6 {
  188. catchsql {
  189. INSERT INTO t3 VALUES(notafunc(2,3),2,3);
  190. }
  191. } {1 {no such function: notafunc}}
  192. do_test insert-4.7 {
  193. execsql {
  194. INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
  195. SELECT * FROM t3 WHERE c=99;
  196. }
  197. } {1 3 99}
  198. # Test
  199. finish_test