Small performance optimization in sqlite3VdbeRecordCompareWithSkip() for
[sqlite.git] / test / conflict3.test
blob751a44268519678e91b9270a9be0a1a0734d786a
1 # 2013-11-05
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for the conflict resolution extension
14 # to SQLite.
16 # This file focuses on making sure that combinations of REPLACE,
17 # IGNORE, and FAIL conflict resolution play well together.
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
22 set testprefix conflict3
24 ifcapable !conflict {
25   finish_test
26   return
29 do_execsql_test 1.1 {
30   CREATE TABLE t1(
31     a INTEGER PRIMARY KEY ON CONFLICT REPLACE, 
32     b UNIQUE ON CONFLICT IGNORE,
33     c UNIQUE ON CONFLICT FAIL
34   );
35   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
36   SELECT a,b,c FROM t1 ORDER BY a;
37 } {1 2 3 2 3 4}
39 # Insert a row that conflicts on column B.  The insert should be ignored.
41 do_execsql_test 1.2 {
42   INSERT INTO t1(a,b,c) VALUES(3,2,5);
43   SELECT a,b,c FROM t1 ORDER BY a;
44 } {1 2 3 2 3 4}
46 # Insert two rows where the second conflicts on C.  The first row show go
47 # and and then there should be a constraint error.
49 do_test 1.3 {
50   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
51 } {1 {UNIQUE constraint failed: t1.c}}
52 do_execsql_test 1.4 {
53   SELECT a,b,c FROM t1 ORDER BY a;
54 } {1 2 3 2 3 4 4 5 6}
56 # Replete the tests above, but this time on a table non-INTEGER primary key.
58 do_execsql_test 2.1 {
59   DROP TABLE t1;
60   CREATE TABLE t1(
61     a INT PRIMARY KEY ON CONFLICT REPLACE, 
62     b UNIQUE ON CONFLICT IGNORE,
63     c UNIQUE ON CONFLICT FAIL
64   );
65   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
66   SELECT a,b,c FROM t1 ORDER BY a;
67 } {1 2 3 2 3 4}
69 # Insert a row that conflicts on column B.  The insert should be ignored.
71 do_execsql_test 2.2 {
72   INSERT INTO t1(a,b,c) VALUES(3,2,5);
73   SELECT a,b,c FROM t1 ORDER BY a;
74 } {1 2 3 2 3 4}
76 # Insert two rows where the second conflicts on C.  The first row show go
77 # and and then there should be a constraint error.
79 do_test 2.3 {
80   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
81 } {1 {UNIQUE constraint failed: t1.c}}
82 do_execsql_test 2.4 {
83   SELECT a,b,c FROM t1 ORDER BY a;
84 } {1 2 3 2 3 4 4 5 6}
86 # Replete again on a WITHOUT ROWID table.
88 do_execsql_test 3.1 {
89   DROP TABLE t1;
90   CREATE TABLE t1(
91     a INT PRIMARY KEY ON CONFLICT REPLACE, 
92     b UNIQUE ON CONFLICT IGNORE,
93     c UNIQUE ON CONFLICT FAIL
94   ) WITHOUT ROWID;
95   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
96   SELECT a,b,c FROM t1 ORDER BY a;
97 } {1 2 3 2 3 4}
99 # Insert a row that conflicts on column B.  The insert should be ignored.
101 do_execsql_test 3.2 {
102   INSERT INTO t1(a,b,c) VALUES(3,2,5);
103   SELECT a,b,c FROM t1 ORDER BY a;
104 } {1 2 3 2 3 4}
106 # Insert two rows where the second conflicts on C.  The first row show go
107 # and and then there should be a constraint error.
109 do_test 3.3 {
110   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
111 } {1 {UNIQUE constraint failed: t1.c}}
112 do_execsql_test 3.4 {
113   SELECT a,b,c FROM t1 ORDER BY a;
114 } {1 2 3 2 3 4 4 5 6}
116 # Arrange the table rows in a different order and repeat.
118 do_execsql_test 4.1 {
119   DROP TABLE t1;
120   CREATE TABLE t1(
121     b UNIQUE ON CONFLICT IGNORE,
122     c UNIQUE ON CONFLICT FAIL,
123     a INT PRIMARY KEY ON CONFLICT REPLACE
124   ) WITHOUT ROWID;
125   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
126   SELECT a,b,c FROM t1 ORDER BY a;
127 } {1 2 3 2 3 4}
129 # Insert a row that conflicts on column B.  The insert should be ignored.
131 do_execsql_test 4.2 {
132   INSERT INTO t1(a,b,c) VALUES(3,2,5);
133   SELECT a,b,c FROM t1 ORDER BY a;
134 } {1 2 3 2 3 4}
136 # Insert two rows where the second conflicts on C.  The first row show go
137 # and and then there should be a constraint error.
139 do_test 4.3 {
140   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
141 } {1 {UNIQUE constraint failed: t1.c}}
142 do_execsql_test 4.4 {
143   SELECT a,b,c FROM t1 ORDER BY a;
144 } {1 2 3 2 3 4 4 5 6}
146 # Arrange the table rows in a different order and repeat.
148 do_execsql_test 5.1 {
149   DROP TABLE t1;
150   CREATE TABLE t1(
151     b UNIQUE ON CONFLICT IGNORE,
152     a INT PRIMARY KEY ON CONFLICT REPLACE,
153     c UNIQUE ON CONFLICT FAIL
154   );
155   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
156   SELECT a,b,c FROM t1 ORDER BY a;
157 } {1 2 3 2 3 4}
159 # Insert a row that conflicts on column B.  The insert should be ignored.
161 do_execsql_test 5.2 {
162   INSERT INTO t1(a,b,c) VALUES(3,2,5);
163   SELECT a,b,c FROM t1 ORDER BY a;
164 } {1 2 3 2 3 4}
166 # Insert two rows where the second conflicts on C.  The first row show go
167 # and and then there should be a constraint error.
169 do_test 5.3 {
170   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
171 } {1 {UNIQUE constraint failed: t1.c}}
172 do_execsql_test 5.4 {
173   SELECT a,b,c FROM t1 ORDER BY a;
174 } {1 2 3 2 3 4 4 5 6}
176 # Arrange the table rows in a different order and repeat.
178 do_execsql_test 6.1 {
179   DROP TABLE t1;
180   CREATE TABLE t1(
181     c UNIQUE ON CONFLICT FAIL,
182     a INT PRIMARY KEY ON CONFLICT REPLACE,
183     b UNIQUE ON CONFLICT IGNORE
184   );
185   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
186   SELECT a,b,c FROM t1 ORDER BY a;
187 } {1 2 3 2 3 4}
189 # Insert a row that conflicts on column B.  The insert should be ignored.
191 do_execsql_test 6.2 {
192   INSERT INTO t1(a,b,c) VALUES(3,2,5);
193   SELECT a,b,c FROM t1 ORDER BY a;
194 } {1 2 3 2 3 4}
196 # Insert two rows where the second conflicts on C.  The first row show go
197 # and and then there should be a constraint error.
199 do_test 6.3 {
200   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
201 } {1 {UNIQUE constraint failed: t1.c}}
202 do_execsql_test 6.4 {
203   SELECT a,b,c FROM t1 ORDER BY a;
204 } {1 2 3 2 3 4 4 5 6}
206 # Change which column is the PRIMARY KEY
208 do_execsql_test 7.1 {
209   DROP TABLE t1;
210   CREATE TABLE t1(
211     a UNIQUE ON CONFLICT REPLACE, 
212     b INTEGER PRIMARY KEY ON CONFLICT IGNORE,
213     c UNIQUE ON CONFLICT FAIL
214   );
215   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
216   SELECT a,b,c FROM t1 ORDER BY a;
217 } {1 2 3 2 3 4}
219 # Insert a row that conflicts on column B.  The insert should be ignored.
221 do_execsql_test 7.2 {
222   INSERT INTO t1(a,b,c) VALUES(3,2,5);
223   SELECT a,b,c FROM t1 ORDER BY a;
224 } {1 2 3 2 3 4}
226 # Insert two rows where the second conflicts on C.  The first row show go
227 # and and then there should be a constraint error.
229 do_test 7.3 {
230   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
231 } {1 {UNIQUE constraint failed: t1.c}}
232 do_execsql_test 7.4 {
233   SELECT a,b,c FROM t1 ORDER BY a;
234 } {1 2 3 2 3 4 4 5 6}
236 # Change which column is the PRIMARY KEY
238 do_execsql_test 8.1 {
239   DROP TABLE t1;
240   CREATE TABLE t1(
241     a UNIQUE ON CONFLICT REPLACE, 
242     b INT PRIMARY KEY ON CONFLICT IGNORE,
243     c UNIQUE ON CONFLICT FAIL
244   );
245   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
246   SELECT a,b,c FROM t1 ORDER BY a;
247 } {1 2 3 2 3 4}
249 # Insert a row that conflicts on column B.  The insert should be ignored.
251 do_execsql_test 8.2 {
252   INSERT INTO t1(a,b,c) VALUES(3,2,5);
253   SELECT a,b,c FROM t1 ORDER BY a;
254 } {1 2 3 2 3 4}
256 # Insert two rows where the second conflicts on C.  The first row show go
257 # and and then there should be a constraint error.
259 do_test 8.3 {
260   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
261 } {1 {UNIQUE constraint failed: t1.c}}
262 do_execsql_test 8.4 {
263   SELECT a,b,c FROM t1 ORDER BY a;
264 } {1 2 3 2 3 4 4 5 6}
266 # Change which column is the PRIMARY KEY
268 do_execsql_test 9.1 {
269   DROP TABLE t1;
270   CREATE TABLE t1(
271     a UNIQUE ON CONFLICT REPLACE, 
272     b INT PRIMARY KEY ON CONFLICT IGNORE,
273     c UNIQUE ON CONFLICT FAIL
274   ) WITHOUT ROWID;
275   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
276   SELECT a,b,c FROM t1 ORDER BY a;
277 } {1 2 3 2 3 4}
279 # Insert a row that conflicts on column B.  The insert should be ignored.
281 do_execsql_test 9.2 {
282   INSERT INTO t1(a,b,c) VALUES(3,2,5);
283   SELECT a,b,c FROM t1 ORDER BY a;
284 } {1 2 3 2 3 4}
286 # Insert two rows where the second conflicts on C.  The first row show go
287 # and and then there should be a constraint error.
289 do_test 9.3 {
290   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
291 } {1 {UNIQUE constraint failed: t1.c}}
292 do_execsql_test 9.4 {
293   SELECT a,b,c FROM t1 ORDER BY a;
294 } {1 2 3 2 3 4 4 5 6}
296 # Change which column is the PRIMARY KEY
298 do_execsql_test 10.1 {
299   DROP TABLE t1;
300   CREATE TABLE t1(
301     a UNIQUE ON CONFLICT REPLACE, 
302     b UNIQUE ON CONFLICT IGNORE,
303     c INTEGER PRIMARY KEY ON CONFLICT FAIL
304   );
305   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
306   SELECT a,b,c FROM t1 ORDER BY a;
307 } {1 2 3 2 3 4}
309 # Insert a row that conflicts on column B.  The insert should be ignored.
311 do_execsql_test 10.2 {
312   INSERT INTO t1(a,b,c) VALUES(3,2,5);
313   SELECT a,b,c FROM t1 ORDER BY a;
314 } {1 2 3 2 3 4}
316 # Insert two rows where the second conflicts on C.  The first row show go
317 # and and then there should be a constraint error.
319 do_test 10.3 {
320   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
321 } {1 {UNIQUE constraint failed: t1.c}}
322 do_execsql_test 10.4 {
323   SELECT a,b,c FROM t1 ORDER BY a;
324 } {1 2 3 2 3 4 4 5 6}
326 # Change which column is the PRIMARY KEY
328 do_execsql_test 11.1 {
329   DROP TABLE t1;
330   CREATE TABLE t1(
331     a UNIQUE ON CONFLICT REPLACE, 
332     b UNIQUE ON CONFLICT IGNORE,
333     c PRIMARY KEY ON CONFLICT FAIL
334   ) WITHOUT ROWID;
335   INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
336   SELECT a,b,c FROM t1 ORDER BY a;
337 } {1 2 3 2 3 4}
339 # Insert a row that conflicts on column B.  The insert should be ignored.
341 do_execsql_test 11.2 {
342   INSERT INTO t1(a,b,c) VALUES(3,2,5);
343   SELECT a,b,c FROM t1 ORDER BY a;
344 } {1 2 3 2 3 4}
346 # Insert two rows where the second conflicts on C.  The first row show go
347 # and and then there should be a constraint error.
349 do_test 11.3 {
350   catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
351 } {1 {UNIQUE constraint failed: t1.c}}
352 do_execsql_test 11.4 {
353   SELECT a,b,c FROM t1 ORDER BY a;
354 } {1 2 3 2 3 4 4 5 6}
356 # Check that ticket [f68dc596c4] has been fixed.
358 do_execsql_test 12.1 {
359   CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT);
360   INSERT INTO t2 VALUES(111, '111');
362 do_execsql_test 12.2 {
363   REPLACE INTO t2 VALUES(NULL, '112'), (111, '111B');
365 do_execsql_test 12.3 {
366   SELECT * FROM t2;
367 } {111 111B 112 112}
370 finish_test