1# 2011 July 13 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# This file implements regression tests for SQLite library. 12# 13 14if {![info exists testdir]} { 15 set testdir [file join [file dirname [info script]] .. .. test] 16} 17source [file join [file dirname [info script]] session_common.tcl] 18source $testdir/tester.tcl 19ifcapable !session {finish_test; return} 20 21set testprefix session8 22 23proc noop {args} {} 24 25# Like [dbcksum] in tester.tcl. Except this version is not sensitive 26# to changes in the value of implicit IPK columns. 27# 28proc udbcksum {db dbname} { 29 if {$dbname=="temp"} { 30 set master sqlite_temp_master 31 } else { 32 set master $dbname.sqlite_master 33 } 34 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] 35 set txt [$db eval "SELECT * FROM $master"]\n 36 foreach tab $alltab { 37 append txt [lsort [$db eval "SELECT * FROM $dbname.$tab"]]\n 38 } 39 return [md5 $txt] 40} 41 42proc do_then_undo {tn sql} { 43 set ck1 [udbcksum db main] 44 45 sqlite3session S db main 46 S attach * 47 db eval $sql 48 49 set ck2 [udbcksum db main] 50 51 set invert [sqlite3changeset_invert [S changeset]] 52 S delete 53 sqlite3changeset_apply db $invert noop 54 55 set ck3 [udbcksum db main] 56 57 set a [expr {$ck1==$ck2}] 58 set b [expr {$ck1==$ck3}] 59 uplevel [list do_test $tn.1 "set {} $a" 0] 60 uplevel [list do_test $tn.2 "set {} $b" 1] 61} 62 63do_execsql_test 1.1 { 64 CREATE TABLE t1(a PRIMARY KEY, b); 65 INSERT INTO t1 VALUES(1, 2); 66 INSERT INTO t1 VALUES("abc", "xyz"); 67} 68do_then_undo 1.2 { INSERT INTO t1 VALUES(3, 4); } 69do_then_undo 1.3 { DELETE FROM t1 WHERE b=2; } 70do_then_undo 1.4 { UPDATE t1 SET b = 3 WHERE a = 1; } 71 72do_execsql_test 2.1 { 73 CREATE TABLE t2(a, b PRIMARY KEY); 74 INSERT INTO t2 VALUES(1, 2); 75 INSERT INTO t2 VALUES('abc', 'xyz'); 76} 77do_then_undo 1.2 { INSERT INTO t2 VALUES(3, 4); } 78do_then_undo 1.3 { DELETE FROM t2 WHERE b=2; } 79do_then_undo 1.4 { UPDATE t1 SET a = '123' WHERE b = 'xyz'; } 80 81do_execsql_test 3.1 { 82 CREATE TABLE t3(a, b, c, d, e, PRIMARY KEY(c, e)); 83 INSERT INTO t3 VALUES('x', 45, 0.0, 'abcdef', 12); 84 INSERT INTO t3 VALUES(45, 0.0, 'abcdef', 12, 'x'); 85 INSERT INTO t3 VALUES(0.0, 'abcdef', 12, 'x', 45); 86} 87 88do_then_undo 3.2 { UPDATE t3 SET b=b||b WHERE e!='x' } 89do_then_undo 3.3 { UPDATE t3 SET a = 46 } 90 91finish_test 92