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
|
# 2018 October 18
#
# 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 SQLite library.
#
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 sessioninvert
proc iter_invert {C} {
set x [list]
sqlite3session_foreach -invert c $C { lappend x $c }
set x
}
proc do_invert_test {tn sql {iter {}}} {
forcecopy test.db test.db2
sqlite3 db2 test.db2
set C [changeset_from_sql $sql]
forcecopy test.db test.db3
sqlite3 db3 test.db3
uplevel [list do_test $tn.1 [list compare_db db db3] {}]
set I [sqlite3changeset_invert $C]
sqlite3changeset_apply db $I {}
uplevel [list do_test $tn.2 [list compare_db db db2] {}]
sqlite3changeset_apply_v2 -invert db3 $C {}
uplevel [list do_test $tn.3 [list compare_db db db3] {}]
if {$iter!=""} {
uplevel [list do_test $tn.4 [list iter_invert $C] [list {*}$iter]]
}
catch { db2 close }
catch { db3 close }
}
do_execsql_test 1.0 {
CREATE TABLE t1(a PRIMARY KEY, b, c);
CREATE TABLE t2(d, e, f, PRIMARY KEY(e, f));
INSERT INTO t1 VALUES(1, 'one', 'i');
INSERT INTO t1 VALUES(2, 'two', 'ii');
INSERT INTO t1 VALUES(3, 'three', 'iii');
INSERT INTO t1 VALUES(4, 'four', 'iv');
INSERT INTO t1 VALUES(5, 'five', 'v');
INSERT INTO t1 VALUES(6, 'six', 'vi');
INSERT INTO t2 SELECT * FROM t1;
}
do_invert_test 1.1 {
INSERT INTO t1 VALUES(7, 'seven', 'vii');
} {
{DELETE t1 0 X.. {i 7 t seven t vii} {}}
}
do_invert_test 1.2 {
DELETE FROM t1 WHERE a<4;
} {
{INSERT t1 0 X.. {} {i 1 t one t i}}
{INSERT t1 0 X.. {} {i 2 t two t ii}}
{INSERT t1 0 X.. {} {i 3 t three t iii}}
}
do_invert_test 1.3 {
UPDATE t1 SET c=5;
} {
{UPDATE t1 0 X.. {i 1 {} {} i 5} {{} {} {} {} t i}}
{UPDATE t1 0 X.. {i 2 {} {} i 5} {{} {} {} {} t ii}}
{UPDATE t1 0 X.. {i 3 {} {} i 5} {{} {} {} {} t iii}}
{UPDATE t1 0 X.. {i 4 {} {} i 5} {{} {} {} {} t iv}}
{UPDATE t1 0 X.. {i 5 {} {} i 5} {{} {} {} {} t v}}
{UPDATE t1 0 X.. {i 6 {} {} i 5} {{} {} {} {} t vi}}
}
do_invert_test 1.4 {
UPDATE t1 SET b = a+1 WHERE a%2;
DELETE FROM t2;
INSERT INTO t1 VALUES(10, 'ten', NULL);
}
do_invert_test 1.5 {
UPDATE t2 SET d = d-1;
} {
{UPDATE t2 0 .XX {i 2 t three t iii} {i 3 {} {} {} {}}}
{UPDATE t2 0 .XX {i 1 t two t ii} {i 2 {} {} {} {}}}
{UPDATE t2 0 .XX {i 5 t six t vi} {i 6 {} {} {} {}}}
{UPDATE t2 0 .XX {i 3 t four t iv} {i 4 {} {} {} {}}}
{UPDATE t2 0 .XX {i 0 t one t i} {i 1 {} {} {} {}}}
{UPDATE t2 0 .XX {i 4 t five t v} {i 5 {} {} {} {}}}
}
do_execsql_test 2.0 {
ANALYZE;
PRAGMA writable_schema = 1;
DROP TABLE IF EXISTS sqlite_stat4;
SELECT * FROM sqlite_stat1;
} {
t2 sqlite_autoindex_t2_1 {6 1 1}
t1 sqlite_autoindex_t1_1 {6 1}
}
do_invert_test 2.1 {
INSERT INTO sqlite_stat1 VALUES('t3', 'idx2', '1 2 3');
} {
{DELETE sqlite_stat1 0 XX. {t t3 t idx2 t {1 2 3}} {}}
}
do_invert_test 2.2 {
DELETE FROM sqlite_stat1;
} {
{INSERT sqlite_stat1 0 XX. {} {t t1 t sqlite_autoindex_t1_1 t {6 1}}}
{INSERT sqlite_stat1 0 XX. {} {t t2 t sqlite_autoindex_t2_1 t {6 1 1}}}
}
do_invert_test 2.3 {
UPDATE sqlite_stat1 SET stat = 'hello world';
}
do_test 3.0 {
forcecopy test.db test.db2
sqlite3 db2 test.db2
set P [patchset_from_sql {
INSERT INTO t2 VALUES(1, 2, 3);
DELETE FROM t2 WHERE d = 3;
}]
list [catch { sqlite3changeset_apply_v2 -invert db2 $P {} } msg] $msg
} {1 SQLITE_CORRUPT}
do_test 3.1 {
list [catch { sqlite3session_foreach -invert db2 $P {} } msg] $msg
} {1 SQLITE_CORRUPT}
do_test 3.2 {
sqlite3changeset_apply_v2 db2 $P {}
compare_db db db2
} {}
#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 4.0 {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
INSERT INTO t1 VALUES(3, 'three');
INSERT INTO t1 VALUES(4, 'four');
}
do_invert_test 4.1 {
DELETE FROM t1;
INSERT INTO t1 VALUES(1, 'two');
INSERT INTO t1 VALUES(2, 'five');
INSERT INTO t1 VALUES(3, 'one');
INSERT INTO t1 VALUES(4, 'three');
} {
{UPDATE t1 0 X. {i 1 t two} {{} {} t one}}
{UPDATE t1 0 X. {i 2 t five} {{} {} t two}}
{UPDATE t1 0 X. {i 3 t one} {{} {} t three}}
{UPDATE t1 0 X. {i 4 t three} {{} {} t four}}
}
finish_test
|