interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / sqldiff1.test

181 lines · 5020 bytes Raw Blame History
1# 2015-05-11
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#
12# Quick tests for the sqldiff tool
13#
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16
17set PROG [test_find_sqldiff]
18
19db close
20forcedelete test.db test2.db
21sqlite3 db test.db
22
23do_test sqldiff-1.0 {
24 db eval {
25 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
26 CREATE TABLE t2(a INT PRIMARY KEY, b) WITHOUT ROWID;
27 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
28 INSERT INTO t1(a,b) SELECT x, printf('abc-%d-xyz',x) FROM c;
29 INSERT INTO t2(a,b) SELECT a, b FROM t1;
30 }
31 db backup test2.db
32 db eval {
33 ATTACH 'test2.db' AS x2;
34 DELETE FROM x2.t1 WHERE a=49;
35 DELETE FROM x2.t2 WHERE a=48;
36 INSERT INTO x2.t1(a,b) VALUES(1234,'hello');
37 INSERT INTO x2.t2(a,b) VALUES(50.5,'xyzzy');
38 INSERT INTO x2.t2(a,b) VALUES(51.5,'');
39 INSERT INTO x2.t2(a,b) VALUES(52.5,''||X'0d0a');
40 INSERT INTO x2.t2(a,b) VALUES(53.5,'one'||X'0a0d');
41 INSERT INTO x2.t2(a,b) VALUES(54.5,'one'||X'0a'||'two');
42 CREATE TABLE x2.t3(a,b,c);
43 INSERT INTO x2.t3 VALUES(111,222,333);
44 CREATE TABLE main.t4(x,y,z);
45 INSERT INTO t4 SELECT * FROM t3;
46 }
47 set line "exec $PROG test.db test2.db"
48 unset -nocomplain ::MSG
49 catch {eval $line} ::MSG
50} {0}
51do_test sqldiff-1.1 {
52 set ::MSG
53} {DELETE FROM t1 WHERE a=49;
54INSERT INTO t1(a,b) VALUES(1234,'hello');
55DELETE FROM t2 WHERE a=48;
56INSERT INTO t2(a,b) VALUES(50.5,'xyzzy');
57INSERT INTO t2(a,b) VALUES(51.5,'');
58INSERT INTO t2(a,b) VALUES(52.5,''||X'0d0a');
59INSERT INTO t2(a,b) VALUES(53.5,'one'||X'0a0d');
60INSERT INTO t2(a,b) VALUES(54.5,'one'||X'0a'
61||'two');
62CREATE TABLE t3(a,b,c);
63INSERT INTO t3(rowid,a,b,c) VALUES(1,111,222,333);
64DROP TABLE t4;}
65
66db close
67forcedelete test.db test2.db
68sqlite3 db test.db
69
70do_test sqldiff-2.0 {
71 db eval {
72 CREATE TABLE t1(a INTEGER PRIMARY KEY);
73 }
74 db close
75 sqlite3 db test2.db
76 db eval {
77 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
78 }
79 db close
80 set line "exec $PROG test.db test2.db"
81 unset -nocomplain ::MSG
82 catch {eval $line} ::MSG
83} {0}
84do_test sqldiff-2.1 {
85 set ::MSG
86} {ALTER TABLE t1 ADD COLUMN b;}
87
88# Bug 2026-10-18T20:38:30Z
89#
90proc sqldiff_apply_test {tn} {
91 set patch [exec $::PROG test.db test2.db]
92 sqlite3 db test.db
93 set rc [catch {db eval $patch} msg]
94 db close
95 if {$rc} {
96 puts "PATCH: [list $patch]"
97 }
98 do_test $tn.1 [list list $rc $msg] {0 {}}
99 do_test $tn.2 {exec $::PROG test.db test2.db} {}
100}
101
102forcedelete test.db test2.db
103sqlite3 db test.db
104do_execsql_test sqldiff-3.0 {
105 CREATE TABLE t1(a INTEGER PRIMARY KEY, b REAL);
106}
107db backup test2.db
108db close
109sqlite3 db test2.db
110do_execsql_test sqldiff-3.1 {
111 INSERT INTO t1 VALUES(1, 0.1+0.2);
112 INSERT INTO t1 VALUES(2, 1.0/3.0);
113 INSERT INTO t1 VALUES(3, 1.7976931348623157e308);
114 INSERT INTO t1 VALUES(4, -1.7976931348623157e308);
115 INSERT INTO t1 VALUES(5, 4.9406564584124654e-324);
116 INSERT INTO t1 VALUES(6, 123456789.12345678);
117}
118db close
119sqldiff_apply_test sqldiff-3.2
120sqlite3 db test.db
121do_execsql_test sqldiff-3.3 {
122 ATTACH 'test2.db' AS x2;
123 SELECT a FROM t1 WHERE b IS NOT (SELECT b FROM x2.t1 AS y WHERE y.a=t1.a);
124} {}
125db close
126
127forcedelete test.db test2.db
128sqlite3 db test.db
129do_execsql_test sqldiff-4.0 {
130 CREATE TABLE t1(a INTEGER PRIMARY KEY, b REAL);
131}
132db backup test2.db
133db close
134sqlite3 db test2.db
135do_execsql_test sqldiff-4.1 {
136 INSERT INTO t1 VALUES(1, 9e999);
137 INSERT INTO t1 VALUES(2, -9e999);
138}
139db close
140sqldiff_apply_test sqldiff-4.2
141sqlite3 db test.db
142do_execsql_test sqldiff-4.3 {
143 SELECT a, b>1e308, b<-1e308 FROM t1 ORDER BY a;
144} {1 1 0 2 0 1}
145db close
146
147# The following test cases demonstrate that if the source table contains
148# an AUTOINCREMENT table but there is no corresponding entry in the
149# sqlite_sequence table, then the sqlite_sequence table is not updated
150# correctly. This is a long-standing deficiency in sqldiff, though the
151# issue is contrived and does not appear in practice. If the issue is
152# ever fixed, then simply uncomment the following test sequence to verify
153# the fix.
154#
155if 0 {
156 forcedelete test.db test2.db
157 sqlite3 db test.db
158 do_execsql_test sqldiff-5.0 {
159 CREATE TABLE a1(id INTEGER PRIMARY KEY AUTOINCREMENT, x);
160 }
161 db backup test2.db
162 db close
163 sqlite3 db test2.db
164 do_execsql_test sqldiff-5.1 {
165 INSERT INTO a1(x) VALUES('one'),('two'),('three');
166 DELETE FROM a1 WHERE id=3;
167 SELECT seq FROM sqlite_sequence WHERE name='a1';
168 } {3}
169 db close
170 sqldiff_apply_test sqldiff-5.2
171 sqlite3 db test.db
172 do_execsql_test sqldiff-5.3 {
173 SELECT seq FROM sqlite_sequence WHERE name='a1';
174 } {3}
175 do_execsql_test sqldiff-5.4 {
176 INSERT INTO a1(x) VALUES('four');
177 SELECT id FROM a1 WHERE x='four';
178 } {4}
179}
180
181finish_test