aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/session/sessionB.test176
1 files changed, 175 insertions, 1 deletions
diff --git a/ext/session/sessionB.test b/ext/session/sessionB.test
index 8791272f8..d61cada98 100644
--- a/ext/session/sessionB.test
+++ b/ext/session/sessionB.test
@@ -327,6 +327,180 @@ do_patchconcat_test 4.3.9 -revert {
{DELETE t2 0 .XX. {{} {} i 1 i 2 {} {}} {}}
}
-finish_test
+#-------------------------------------------------------------------------
+# More rigorous testing of the _patchset(), _apply and _concat() APIs.
+#
+# The inputs to each test are a populate database and a list of DML
+# statements. This test determines that the final database is the same
+# if:
+#
+# 1) the statements are executed directly on the database.
+#
+# 2) a single patchset is collected while executing the statements and
+# then applied to a copy of the original database file.
+#
+# 3) individual patchsets are collected for statement while executing
+# them and concatenated together before being applied to a copy of
+# the original database. The concatenation is done in a couple of
+# different ways - linear, pairwise etc.
+#
+# All tests, as it happens, are run with both changesets and patchsets.
+# But the focus is on patchset capabilities.
+#
+
+# Return a checksum of the contents of the database file. Implicit IPK
+# columns are not included in the checksum - just modifying rowids does
+# not change the database checksum.
+#
+proc databasecksum {db} {
+ set alltab [$db eval {SELECT name FROM sqlite_master WHERE type='table'}]
+ foreach tab $alltab {
+ $db eval "SELECT * FROM $tab LIMIT 1" res { }
+ set slist [list]
+ foreach col [lsort $res(*)] {
+ lappend slist "quote($col)"
+ }
+ set sql "SELECT [join $slist ,] FROM $tab"
+ append txt "[lsort [$db eval $sql]]\n"
+ }
+ return [md5 $txt]
+}
+proc do_patchset_test {tn tstcmd lSql} {
+ if {$tstcmd != "patchset" && $tstcmd != "changeset"} {
+ error "have $tstcmd: must be patchset or changeset"
+ }
+
+ foreach fname {test.db2 test.db3 test.db4 test.db5} {
+ forcedelete $fname
+ forcecopy test.db $fname
+ }
+
+ # Execute the SQL statements on [db]. Collect a patchset for each
+ # individual statement, as well as a single patchset for the entire
+ # operation.
+ sqlite3session S db main
+ S attach *
+ foreach sql $lSql {
+ sqlite3session T db main
+ T attach *
+ db eval $sql
+ lappend lPatch [T patchset]
+ T delete
+ }
+ set patchset [S patchset]
+ S delete
+
+ # Calculate a checksum for the final database.
+ set cksum [databasecksum db]
+
+ # 1. Apply the single large patchset to test.db2
+ sqlite3 db2 test.db2
+ sqlite3changeset_apply db2 $patchset noop
+ uplevel [list do_test $tn.1 { databasecksum db2 } $cksum ]
+ db2 close
+
+ # 2. Apply each of the single-statement patchsets to test.db3
+ sqlite3 db2 test.db3
+ foreach p $lPatch {
+ sqlite3changeset_apply db2 $p noop
+ }
+ uplevel [list do_test $tn.2 { databasecksum db2 } $cksum ]
+ db2 close
+
+ # 3. Concatenate all single-statement patchsets into a single large
+ # patchset, then apply it to test.db4.
+ #
+ sqlite3 db2 test.db4
+ set big ""
+ foreach p $lPatch {
+ set big [sqlite3changeset_concat $big $p]
+ }
+ sqlite3changeset_apply db2 $big noop
+ uplevel [list do_test $tn.3 { databasecksum db2 } $cksum ]
+ db2 close
+
+ # 4. Concatenate all single-statement patchsets pairwise into a single
+ # large patchset, then apply it to test.db5. Pairwise concatenation:
+ #
+ # a b c d e f g h i j k
+ # -> {a b} {c d} {e f} {g h} {i j} k
+ # -> {a b c d} {e f g h} {i j k}
+ # -> {a b c d e f g h} {i j k}
+ # -> {a b c d e f g h i j k}
+ # -> APPLY!
+ #
+ sqlite3 db2 test.db5
+ set L $lPatch
+ while {[llength $L] > 1} {
+ set O [list]
+ for {set i 0} {$i < [llength $L]} {incr i 2} {
+ if {$i==[llength $L]-1} {
+ lappend O [lindex $L $i]
+ } else {
+ set i1 [expr $i+1]
+ lappend O [sqlite3changeset_concat [lindex $L $i] [lindex $L $i1]]
+ }
+ }
+ set L $O
+ }
+ sqlite3changeset_apply db2 [lindex $L 0] noop
+ uplevel [list do_test $tn.4 { databasecksum db2 } $cksum ]
+ db2 close
+}
+
+proc do_patchset_changeset_test {tn initsql args} {
+ foreach tstcmd {patchset changeset} {
+ reset_db
+ execsql $initsql
+ foreach sql $args {
+ set lSql [split $sql ";"]
+ uplevel [list do_patchset_test $tn.$tstcmd $tstcmd $lSql]
+ }
+ }
+}
+
+do_patchset_changeset_test 5.1 {
+ CREATE TABLE t1(a PRIMARY KEY, b, c);
+ INSERT INTO t1 VALUES(1, 2, 3);
+} {
+ INSERT INTO t1 VALUES(4, 5, 6);
+ DELETE FROM t1 WHERE a=1;
+} {
+ INSERT INTO t1 VALUES(7, 8, 9);
+ UPDATE t1 SET c = 5;
+ INSERT INTO t1 VALUES(10, 11, 12);
+ UPDATE t1 SET c = 6;
+ INSERT INTO t1 VALUES(13, 14, 15);
+} {
+ UPDATE t1 SET c=c+1;
+ DELETE FROM t1 WHERE (a%2);
+}
+
+do_patchset_changeset_test 5.2 {
+ CREATE TABLE t1(a PRIMARY KEY, b, c);
+ CREATE TABLE t2(a, b, c, d, PRIMARY KEY(c, b));
+} {
+ INSERT INTO t1 VALUES(x'00', 0, 'zero');
+ INSERT INTO t1 VALUES(x'01', 1, 'one');
+ INSERT INTO t1 VALUES(x'02', 4, 'four');
+ INSERT INTO t1 VALUES(x'03', 9, 'nine');
+ INSERT INTO t1 VALUES(x'04', 16, 'sixteen');
+ INSERT INTO t1 VALUES(x'05', 25, 'twenty-five');
+} {
+ UPDATE t1 SET a = b WHERE b<=4;
+ INSERT INTO t2 SELECT NULL, * FROM t1;
+ DELETE FROM t1 WHERE b=25;
+} {
+ DELETE FROM t2;
+ INSERT INTO t2 SELECT NULL, * FROM t1;
+ DELETE FROM t1;
+ INSERT INTO t1 SELECT b, c, d FROM t2;
+ UPDATE t1 SET b = b+1;
+ UPDATE t1 SET b = b+1;
+ UPDATE t1 SET b = b+1;
+}
+
+
+finish_test