1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# 2016 March 30
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file implements regression tests for the sessions module.
# Specifically, it tests that UNIQUE constraints are dealt with correctly.
#
if {![info exists testdir]} {
set testdir [file join [file dirname [info script]] .. .. test]
}
source [file join [file dirname [info script]] session_common.tcl]
source $testdir/tester.tcl
ifcapable !session {finish_test; return}
set testprefix sessionG
forcedelete test.db2
sqlite3 db2 test.db2
do_test 1.0 {
do_common_sql {
CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
INSERT INTO t1 VALUES(3, 'three');
}
do_then_apply_sql {
DELETE FROM t1 WHERE a=1;
INSERT INTO t1 VALUES(4, 'one');
}
compare_db db db2
} {}
do_test 1.1 {
do_then_apply_sql {
DELETE FROM t1 WHERE a=4;
INSERT INTO t1 VALUES(1, 'one');
}
compare_db db db2
} {}
do_test 1.2 {
execsql { INSERT INTO t1 VALUES(5, 'five') } db2
do_then_apply_sql {
INSERT INTO t1 VALUES(11, 'eleven');
INSERT INTO t1 VALUES(12, 'five');
}
execsql { SELECT * FROM t1 } db2
} {2 two 3 three 1 one 5 five 11 eleven}
do_test 1.3 {
execsql { SELECT * FROM t1 }
} {2 two 3 three 1 one 11 eleven 12 five}
#-------------------------------------------------------------------------
#
reset_db
db2 close
forcedelete test.db2
sqlite3 db2 test.db2
do_test 2.1 {
do_common_sql {
CREATE TABLE t1(a PRIMARY KEY, b UNIQUE, c UNIQUE);
INSERT INTO t1 VALUES(1, 1, 1);
INSERT INTO t1 VALUES(2, 2, 2);
INSERT INTO t1 VALUES(3, 3, 3);
}
} {}
do_test 2.2.1 {
# It is not possible to apply the changeset generated by the following
# SQL, as none of the three updated rows may be updated as part of the
# first pass.
do_then_apply_sql {
UPDATE t1 SET b=0 WHERE a=1;
UPDATE t1 SET b=1 WHERE a=2;
UPDATE t1 SET b=2 WHERE a=3;
UPDATE t1 SET b=3 WHERE a=1;
}
db2 eval { SELECT a, b FROM t1 }
} {1 1 2 2 3 3}
do_test 2.2.2 { db eval { SELECT a, b FROM t1 } } {1 3 2 1 3 2}
#-------------------------------------------------------------------------
#
reset_db
db2 close
forcedelete test.db2
sqlite3 db2 test.db2
do_test 3.1 {
do_common_sql {
CREATE TABLE t1(a PRIMARY KEY, b UNIQUE, c UNIQUE);
INSERT INTO t1 VALUES(1, 1, 1);
INSERT INTO t1 VALUES(2, 2, 2);
INSERT INTO t1 VALUES(3, 3, 3);
}
} {}
do_test 3.3 {
do_then_apply_sql {
UPDATE t1 SET b=4 WHERE a=3;
UPDATE t1 SET b=3 WHERE a=2;
UPDATE t1 SET b=2 WHERE a=1;
}
compare_db db db2
} {}
do_test 3.4 {
do_then_apply_sql {
UPDATE t1 SET b=1 WHERE a=1;
UPDATE t1 SET b=2 WHERE a=2;
UPDATE t1 SET b=3 WHERE a=3;
}
compare_db db db2
} {}
#-------------------------------------------------------------------------
#
reset_db
db2 close
forcedelete test.db2
sqlite3 db2 test.db2
do_test 4.1 {
do_common_sql {
CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
INSERT INTO t1 VALUES(1, 1);
INSERT INTO t1 VALUES(2, 2);
INSERT INTO t1 VALUES(3, 3);
CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
INSERT INTO t2 VALUES(1, 1);
INSERT INTO t2 VALUES(2, 2);
INSERT INTO t2 VALUES(3, 3);
}
} {}
do_test 4.2 {
do_then_apply_sql {
UPDATE t1 SET b=4 WHERE a=3;
UPDATE t1 SET b=3 WHERE a=2;
UPDATE t1 SET b=2 WHERE a=1;
UPDATE t2 SET b=0 WHERE a=1;
UPDATE t2 SET b=1 WHERE a=2;
UPDATE t2 SET b=2 WHERE a=3;
}
compare_db db db2
} {}
do_test 4.3 {
do_then_apply_sql {
UPDATE t1 SET b=1 WHERE a=1;
UPDATE t1 SET b=2 WHERE a=2;
UPDATE t1 SET b=3 WHERE a=3;
UPDATE t2 SET b=3 WHERE a=3;
UPDATE t2 SET b=2 WHERE a=2;
UPDATE t2 SET b=1 WHERE a=1;
}
compare_db db db2
} {}
#-------------------------------------------------------------------------
reset_db
catch { db2 close }
forcedelete test.db2
sqlite3 db2 test.db2
do_execsql_test 5.0.1 {
CREATE TABLE t1(a PRIMARY KEY, b, c);
CREATE TABLE t2(a, b, c PRIMARY KEY);
CREATE TABLE t3(a, b PRIMARY KEY, c);
}
do_execsql_test -db db2 5.0.2 {
CREATE TABLE t1(a PRIMARY KEY, b, c);
CREATE TABLE t2(a, b, c);
CREATE TABLE t3(a, b PRIMARY KEY, c);
}
do_test 5.1 {
do_then_apply_sql {
INSERT INTO t1 VALUES(1, 2, 3);
INSERT INTO t2 VALUES(4, 5, 6);
INSERT INTO t3 VALUES(7, 8, 9);
}
db2 eval {
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
}
} {1 2 3 7 8 9}
#-------------------------------------------------------------------------
reset_db
db func number_name number_name
do_execsql_test 6.0 {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
CREATE UNIQUE INDEX t1b ON t1(b);
WITH s(i) AS (
SELECT 1
UNION ALL
SELECT i+1 FROM s WHERE i<1000
)
INSERT INTO t1 SELECT i, number_name(i) FROM s;
}
do_test 6.1 {
db eval BEGIN
set ::C [changeset_from_sql {
DELETE FROM t1;
WITH s(i) AS (
SELECT 1
UNION ALL
SELECT i+1 FROM s WHERE i<1000
)
INSERT INTO t1 SELECT i, number_name(i+1) FROM s;
}]
db eval ROLLBACK
execsql { SELECT count(*) FROM t1 WHERE number_name(a) IS NOT b }
} {0}
proc xConflict {args} { exit ; return "OMIT" }
do_test 6.2 {
sqlite3changeset_apply db $C xConflict
} {}
do_execsql_test 6.3 { SELECT count(*) FROM t1; } {1000}
do_execsql_test 6.4 {
SELECT count(*) FROM t1 WHERE number_name(a+1) IS NOT b;
} {0}
# db eval { SELECT * FROM t1 } { puts "$a || $b" }
finish_test
|