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
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
28 do_execsql_test conflict-1.1 {
30 a INTEGER PRIMARY KEY ON CONFLICT REPLACE,
31 b UNIQUE ON CONFLICT IGNORE,
32 c UNIQUE ON CONFLICT FAIL
34 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
35 SELECT a,b,c FROM t1 ORDER BY a;
38 # Insert a row that conflicts on column B. The insert should be ignored.
40 do_execsql_test conflict-1.2 {
41 INSERT INTO t1(a,b,c) VALUES(3,2,5);
42 SELECT a,b,c FROM t1 ORDER BY a;
45 # Insert two rows where the second conflicts on C. The first row show go
46 # and and then there should be a constraint error.
48 do_test conflict-1.3 {
49 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
50 } {1 {UNIQUE constraint failed: t1.c}}
51 do_execsql_test conflict-1.4 {
52 SELECT a,b,c FROM t1 ORDER BY a;
55 # Replete the tests above, but this time on a table non-INTEGER primary key.
57 do_execsql_test conflict-2.1 {
60 a INT PRIMARY KEY ON CONFLICT REPLACE,
61 b UNIQUE ON CONFLICT IGNORE,
62 c UNIQUE ON CONFLICT FAIL
64 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
65 SELECT a,b,c FROM t1 ORDER BY a;
68 # Insert a row that conflicts on column B. The insert should be ignored.
70 do_execsql_test conflict-2.2 {
71 INSERT INTO t1(a,b,c) VALUES(3,2,5);
72 SELECT a,b,c FROM t1 ORDER BY a;
75 # Insert two rows where the second conflicts on C. The first row show go
76 # and and then there should be a constraint error.
78 do_test conflict-2.3 {
79 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
80 } {1 {UNIQUE constraint failed: t1.c}}
81 do_execsql_test conflict-2.4 {
82 SELECT a,b,c FROM t1 ORDER BY a;
85 # Replete again on a WITHOUT ROWID table.
87 do_execsql_test conflict-3.1 {
90 a INT PRIMARY KEY ON CONFLICT REPLACE,
91 b UNIQUE ON CONFLICT IGNORE,
92 c UNIQUE ON CONFLICT FAIL
94 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
95 SELECT a,b,c FROM t1 ORDER BY a;
98 # Insert a row that conflicts on column B. The insert should be ignored.
100 do_execsql_test conflict-3.2 {
101 INSERT INTO t1(a,b,c) VALUES(3,2,5);
102 SELECT a,b,c FROM t1 ORDER BY a;
105 # Insert two rows where the second conflicts on C. The first row show go
106 # and and then there should be a constraint error.
108 do_test conflict-3.3 {
109 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
110 } {1 {UNIQUE constraint failed: t1.c}}
111 do_execsql_test conflict-3.4 {
112 SELECT a,b,c FROM t1 ORDER BY a;
113 } {1 2 3 2 3 4 4 5 6}
115 # Arrange the table rows in a different order and repeat.
117 do_execsql_test conflict-4.1 {
120 b UNIQUE ON CONFLICT IGNORE,
121 c UNIQUE ON CONFLICT FAIL,
122 a INT PRIMARY KEY ON CONFLICT REPLACE
124 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
125 SELECT a,b,c FROM t1 ORDER BY a;
128 # Insert a row that conflicts on column B. The insert should be ignored.
130 do_execsql_test conflict-4.2 {
131 INSERT INTO t1(a,b,c) VALUES(3,2,5);
132 SELECT a,b,c FROM t1 ORDER BY a;
135 # Insert two rows where the second conflicts on C. The first row show go
136 # and and then there should be a constraint error.
138 do_test conflict-4.3 {
139 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
140 } {1 {UNIQUE constraint failed: t1.c}}
141 do_execsql_test conflict-4.4 {
142 SELECT a,b,c FROM t1 ORDER BY a;
143 } {1 2 3 2 3 4 4 5 6}
145 # Arrange the table rows in a different order and repeat.
147 do_execsql_test conflict-5.1 {
150 b UNIQUE ON CONFLICT IGNORE,
151 a INT PRIMARY KEY ON CONFLICT REPLACE,
152 c UNIQUE ON CONFLICT FAIL
154 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
155 SELECT a,b,c FROM t1 ORDER BY a;
158 # Insert a row that conflicts on column B. The insert should be ignored.
160 do_execsql_test conflict-5.2 {
161 INSERT INTO t1(a,b,c) VALUES(3,2,5);
162 SELECT a,b,c FROM t1 ORDER BY a;
165 # Insert two rows where the second conflicts on C. The first row show go
166 # and and then there should be a constraint error.
168 do_test conflict-5.3 {
169 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
170 } {1 {UNIQUE constraint failed: t1.c}}
171 do_execsql_test conflict-5.4 {
172 SELECT a,b,c FROM t1 ORDER BY a;
173 } {1 2 3 2 3 4 4 5 6}
175 # Arrange the table rows in a different order and repeat.
177 do_execsql_test conflict-6.1 {
180 c UNIQUE ON CONFLICT FAIL,
181 a INT PRIMARY KEY ON CONFLICT REPLACE,
182 b UNIQUE ON CONFLICT IGNORE
184 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
185 SELECT a,b,c FROM t1 ORDER BY a;
188 # Insert a row that conflicts on column B. The insert should be ignored.
190 do_execsql_test conflict-6.2 {
191 INSERT INTO t1(a,b,c) VALUES(3,2,5);
192 SELECT a,b,c FROM t1 ORDER BY a;
195 # Insert two rows where the second conflicts on C. The first row show go
196 # and and then there should be a constraint error.
198 do_test conflict-6.3 {
199 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
200 } {1 {UNIQUE constraint failed: t1.c}}
201 do_execsql_test conflict-6.4 {
202 SELECT a,b,c FROM t1 ORDER BY a;
203 } {1 2 3 2 3 4 4 5 6}
205 # Change which column is the PRIMARY KEY
207 do_execsql_test conflict-7.1 {
210 a UNIQUE ON CONFLICT REPLACE,
211 b INTEGER PRIMARY KEY ON CONFLICT IGNORE,
212 c UNIQUE ON CONFLICT FAIL
214 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
215 SELECT a,b,c FROM t1 ORDER BY a;
218 # Insert a row that conflicts on column B. The insert should be ignored.
220 do_execsql_test conflict-7.2 {
221 INSERT INTO t1(a,b,c) VALUES(3,2,5);
222 SELECT a,b,c FROM t1 ORDER BY a;
225 # Insert two rows where the second conflicts on C. The first row show go
226 # and and then there should be a constraint error.
228 do_test conflict-7.3 {
229 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
230 } {1 {UNIQUE constraint failed: t1.c}}
231 do_execsql_test conflict-7.4 {
232 SELECT a,b,c FROM t1 ORDER BY a;
233 } {1 2 3 2 3 4 4 5 6}
235 # Change which column is the PRIMARY KEY
237 do_execsql_test conflict-8.1 {
240 a UNIQUE ON CONFLICT REPLACE,
241 b INT PRIMARY KEY ON CONFLICT IGNORE,
242 c UNIQUE ON CONFLICT FAIL
244 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
245 SELECT a,b,c FROM t1 ORDER BY a;
248 # Insert a row that conflicts on column B. The insert should be ignored.
250 do_execsql_test conflict-8.2 {
251 INSERT INTO t1(a,b,c) VALUES(3,2,5);
252 SELECT a,b,c FROM t1 ORDER BY a;
255 # Insert two rows where the second conflicts on C. The first row show go
256 # and and then there should be a constraint error.
258 do_test conflict-8.3 {
259 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
260 } {1 {UNIQUE constraint failed: t1.c}}
261 do_execsql_test conflict-8.4 {
262 SELECT a,b,c FROM t1 ORDER BY a;
263 } {1 2 3 2 3 4 4 5 6}
265 # Change which column is the PRIMARY KEY
267 do_execsql_test conflict-9.1 {
270 a UNIQUE ON CONFLICT REPLACE,
271 b INT PRIMARY KEY ON CONFLICT IGNORE,
272 c UNIQUE ON CONFLICT FAIL
274 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
275 SELECT a,b,c FROM t1 ORDER BY a;
278 # Insert a row that conflicts on column B. The insert should be ignored.
280 do_execsql_test conflict-9.2 {
281 INSERT INTO t1(a,b,c) VALUES(3,2,5);
282 SELECT a,b,c FROM t1 ORDER BY a;
285 # Insert two rows where the second conflicts on C. The first row show go
286 # and and then there should be a constraint error.
288 do_test conflict-9.3 {
289 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
290 } {1 {UNIQUE constraint failed: t1.c}}
291 do_execsql_test conflict-9.4 {
292 SELECT a,b,c FROM t1 ORDER BY a;
293 } {1 2 3 2 3 4 4 5 6}
295 # Change which column is the PRIMARY KEY
297 do_execsql_test conflict-10.1 {
300 a UNIQUE ON CONFLICT REPLACE,
301 b UNIQUE ON CONFLICT IGNORE,
302 c INTEGER PRIMARY KEY ON CONFLICT FAIL
304 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
305 SELECT a,b,c FROM t1 ORDER BY a;
308 # Insert a row that conflicts on column B. The insert should be ignored.
310 do_execsql_test conflict-10.2 {
311 INSERT INTO t1(a,b,c) VALUES(3,2,5);
312 SELECT a,b,c FROM t1 ORDER BY a;
315 # Insert two rows where the second conflicts on C. The first row show go
316 # and and then there should be a constraint error.
318 do_test conflict-10.3 {
319 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
320 } {1 {UNIQUE constraint failed: t1.c}}
321 do_execsql_test conflict-10.4 {
322 SELECT a,b,c FROM t1 ORDER BY a;
323 } {1 2 3 2 3 4 4 5 6}
325 # Change which column is the PRIMARY KEY
327 do_execsql_test conflict-11.1 {
330 a UNIQUE ON CONFLICT REPLACE,
331 b UNIQUE ON CONFLICT IGNORE,
332 c PRIMARY KEY ON CONFLICT FAIL
334 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
335 SELECT a,b,c FROM t1 ORDER BY a;
338 # Insert a row that conflicts on column B. The insert should be ignored.
340 do_execsql_test conflict-11.2 {
341 INSERT INTO t1(a,b,c) VALUES(3,2,5);
342 SELECT a,b,c FROM t1 ORDER BY a;
345 # Insert two rows where the second conflicts on C. The first row show go
346 # and and then there should be a constraint error.
348 do_test conflict-11.3 {
349 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
350 } {1 {UNIQUE constraint failed: t1.c}}
351 do_execsql_test conflict-11.4 {
352 SELECT a,b,c FROM t1 ORDER BY a;
353 } {1 2 3 2 3 4 4 5 6}