insert3.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # 2005 January 13
  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 corner cases of the INSERT statement.
  13. #
  14. # $Id: insert3.test,v 1.9 2009/04/23 14:58:40 danielk1977 Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # All the tests in this file require trigger support
  18. #
  19. ifcapable {trigger} {
  20. # Create a table and a corresponding insert trigger. Do a self-insert
  21. # into the table.
  22. #
  23. do_test insert3-1.0 {
  24. execsql {
  25. CREATE TABLE t1(a,b);
  26. CREATE TABLE log(x UNIQUE, y);
  27. CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
  28. UPDATE log SET y=y+1 WHERE x=new.a;
  29. INSERT OR IGNORE INTO log VALUES(new.a, 1);
  30. END;
  31. INSERT INTO t1 VALUES('hello','world');
  32. INSERT INTO t1 VALUES(5,10);
  33. SELECT * FROM log ORDER BY x;
  34. }
  35. } {5 1 hello 1}
  36. do_test insert3-1.1 {
  37. execsql {
  38. INSERT INTO t1 SELECT a, b+10 FROM t1;
  39. SELECT * FROM log ORDER BY x;
  40. }
  41. } {5 2 hello 2}
  42. do_test insert3-1.2 {
  43. execsql {
  44. CREATE TABLE log2(x PRIMARY KEY,y);
  45. CREATE TRIGGER r2 BEFORE INSERT ON t1 BEGIN
  46. UPDATE log2 SET y=y+1 WHERE x=new.b;
  47. INSERT OR IGNORE INTO log2 VALUES(new.b,1);
  48. END;
  49. INSERT INTO t1 VALUES(453,'hi');
  50. SELECT * FROM log ORDER BY x;
  51. }
  52. } {5 2 453 1 hello 2}
  53. do_test insert3-1.3 {
  54. execsql {
  55. SELECT * FROM log2 ORDER BY x;
  56. }
  57. } {hi 1}
  58. ifcapable compound {
  59. do_test insert3-1.4.1 {
  60. execsql {
  61. INSERT INTO t1 SELECT * FROM t1;
  62. SELECT 'a:', x, y FROM log UNION ALL
  63. SELECT 'b:', x, y FROM log2 ORDER BY x;
  64. }
  65. } {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1}
  66. do_test insert3-1.4.2 {
  67. execsql {
  68. SELECT 'a:', x, y FROM log UNION ALL
  69. SELECT 'b:', x, y FROM log2 ORDER BY x, y;
  70. }
  71. } {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1}
  72. do_test insert3-1.5 {
  73. execsql {
  74. INSERT INTO t1(a) VALUES('xyz');
  75. SELECT * FROM log ORDER BY x;
  76. }
  77. } {5 4 453 2 hello 4 xyz 1}
  78. }
  79. do_test insert3-2.1 {
  80. execsql {
  81. CREATE TABLE t2(
  82. a INTEGER PRIMARY KEY,
  83. b DEFAULT 'b',
  84. c DEFAULT 'c'
  85. );
  86. CREATE TABLE t2dup(a,b,c);
  87. CREATE TRIGGER t2r1 BEFORE INSERT ON t2 BEGIN
  88. INSERT INTO t2dup(a,b,c) VALUES(new.a,new.b,new.c);
  89. END;
  90. INSERT INTO t2(a) VALUES(123);
  91. INSERT INTO t2(b) VALUES(234);
  92. INSERT INTO t2(c) VALUES(345);
  93. SELECT * FROM t2dup;
  94. }
  95. } {123 b c -1 234 c -1 b 345}
  96. do_test insert3-2.2 {
  97. execsql {
  98. DELETE FROM t2dup;
  99. INSERT INTO t2(a) SELECT 1 FROM t1 LIMIT 1;
  100. INSERT INTO t2(b) SELECT 987 FROM t1 LIMIT 1;
  101. INSERT INTO t2(c) SELECT 876 FROM t1 LIMIT 1;
  102. SELECT * FROM t2dup;
  103. }
  104. } {1 b c -1 987 c -1 b 876}
  105. # Test for proper detection of malformed WHEN clauses on INSERT triggers.
  106. #
  107. do_test insert3-3.1 {
  108. execsql {
  109. CREATE TABLE t3(a,b,c);
  110. CREATE TRIGGER t3r1 BEFORE INSERT on t3 WHEN nosuchcol BEGIN
  111. SELECT 'illegal WHEN clause';
  112. END;
  113. }
  114. } {}
  115. do_test insert3-3.2 {
  116. catchsql {
  117. INSERT INTO t3 VALUES(1,2,3)
  118. }
  119. } {1 {no such column: nosuchcol}}
  120. do_test insert3-3.3 {
  121. execsql {
  122. CREATE TABLE t4(a,b,c);
  123. CREATE TRIGGER t4r1 AFTER INSERT on t4 WHEN nosuchcol BEGIN
  124. SELECT 'illegal WHEN clause';
  125. END;
  126. }
  127. } {}
  128. do_test insert3-3.4 {
  129. catchsql {
  130. INSERT INTO t4 VALUES(1,2,3)
  131. }
  132. } {1 {no such column: nosuchcol}}
  133. } ;# ifcapable {trigger}
  134. # Tests for the INSERT INTO ... DEFAULT VALUES construct
  135. #
  136. do_test insert3-3.5 {
  137. execsql {
  138. CREATE TABLE t5(
  139. a INTEGER PRIMARY KEY,
  140. b DEFAULT 'xyz'
  141. );
  142. INSERT INTO t5 DEFAULT VALUES;
  143. SELECT * FROM t5;
  144. }
  145. } {1 xyz}
  146. do_test insert3-3.6 {
  147. execsql {
  148. INSERT INTO t5 DEFAULT VALUES;
  149. SELECT * FROM t5;
  150. }
  151. } {1 xyz 2 xyz}
  152. ifcapable bloblit {
  153. do_test insert3-3.7 {
  154. execsql {
  155. CREATE TABLE t6(x,y DEFAULT 4.3, z DEFAULT x'6869');
  156. INSERT INTO t6 DEFAULT VALUES;
  157. SELECT * FROM t6;
  158. }
  159. } {{} 4.3 hi}
  160. }
  161. foreach tab [db eval {SELECT name FROM sqlite_master WHERE type = 'table'}] {
  162. db eval "DROP TABLE $tab"
  163. }
  164. db close
  165. sqlite3 db test.db
  166. #-------------------------------------------------------------------------
  167. # While developing tests for a different feature (savepoint) the following
  168. # sequence was found to cause an assert() in btree.c to fail. These
  169. # tests are included to ensure that that bug is fixed.
  170. #
  171. do_test insert3-4.1 {
  172. execsql {
  173. CREATE TABLE t1(a, b, c);
  174. CREATE INDEX i1 ON t1(a, b);
  175. BEGIN;
  176. INSERT INTO t1 VALUES(randstr(10,400),randstr(10,400),randstr(10,400));
  177. }
  178. set r "randstr(10,400)"
  179. for {set ii 0} {$ii < 10} {incr ii} {
  180. execsql "INSERT INTO t1 SELECT $r, $r, $r FROM t1"
  181. }
  182. execsql { COMMIT }
  183. } {}
  184. do_test insert3-4.2 {
  185. execsql {
  186. PRAGMA cache_size = 10;
  187. BEGIN;
  188. UPDATE t1 SET a = randstr(10,10) WHERE (rowid%4)==0;
  189. DELETE FROM t1 WHERE rowid%2;
  190. INSERT INTO t1 SELECT randstr(10,400), randstr(10,400), c FROM t1;
  191. COMMIT;
  192. }
  193. } {}
  194. finish_test