1# 2011 Mar 16 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# The focus of this file is testing the session module. 13# 14 15if {![info exists testdir]} { 16 set testdir [file join [file dirname [info script]] .. .. test] 17} 18source [file join [file dirname [info script]] session_common.tcl] 19source $testdir/tester.tcl 20 21set testprefix session2 22 23proc test_reset {} { 24 catch { db close } 25 catch { db2 close } 26 forcedelete test.db test.db2 27 sqlite3 db test.db 28 sqlite3 db2 test.db2 29} 30 31proc do_common_sql {sql} { 32 execsql $sql db 33 execsql $sql db2 34} 35proc xConflict args { return "OMIT" } 36 37proc do_then_apply_sql {sql} { 38 sqlite3session S db main 39 db eval {SELECT name FROM sqlite_master WHERE type = 'table'} { 40 S attach $name 41 } 42 43 db eval $sql 44 sqlite3changeset_apply db2 [S changeset] xConflict 45 S delete 46} 47 48proc do_iterator_test {tn tbl_list sql res} { 49 sqlite3session S db main 50 foreach t $tbl_list {S attach $t} 51 execsql $sql 52 53 set r [list] 54 foreach v $res { lappend r $v } 55 56 set x [list] 57 sqlite3session_foreach c [S changeset] { lappend x $c } 58 uplevel do_test $tn [list [list set {} $x]] [list $r] 59 60 S delete 61} 62 63# Compare the contents of all tables in [db1] and [db2]. Throw an error if 64# they are not identical, or return an empty string if they are. 65# 66proc compare_db {db1 db2} { 67 68 set sql {SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name} 69 set lot1 [$db1 eval $sql] 70 set lot2 [$db2 eval $sql] 71 72 if {$lot1 != $lot2} { error "databases contain different tables" } 73 74 foreach tbl $lot1 { 75 set col1 [list] 76 set col2 [list] 77 78 $db1 eval "PRAGMA table_info = $tbl" { lappend col1 $name } 79 $db2 eval "PRAGMA table_info = $tbl" { lappend col2 $name } 80 if {$col1 != $col2} { error "table $tbl schema mismatch" } 81 82 set sql "SELECT * FROM $tbl ORDER BY [join $col1 ,]" 83 set data1 [$db1 eval $sql] 84 set data2 [$db2 eval $sql] 85 if {$data1 != $data2} { error "table $tbl data mismatch" } 86 } 87 88 return "" 89} 90 91########################################################################## 92# End of proc definitions. Start of tests. 93########################################################################## 94 95test_reset 96do_execsql_test 1.0 { 97 CREATE TABLE t1(a PRIMARY KEY, b); 98 INSERT INTO t1 VALUES('i', 'one'); 99} 100do_iterator_test 1.1 t1 { 101 DELETE FROM t1 WHERE a = 'i'; 102 INSERT INTO t1 VALUES('ii', 'two'); 103} { 104 {DELETE t1 {t i t one} {}} 105 {INSERT t1 {} {t ii t two}} 106} 107 108test_reset 109do_common_sql { 110 CREATE TABLE t1(a PRIMARY KEY, b); 111 CREATE TABLE t2(a, b INTEGER PRIMARY KEY); 112 CREATE TABLE t3(a, b, c, PRIMARY KEY(a, b)); 113} 114 115foreach {tn sql} { 116 1 { INSERT INTO t1 VALUES(1, 2) } 117 2 { 118 INSERT INTO t2 VALUES(1, NULL); 119 INSERT INTO t2 VALUES(2, NULL); 120 INSERT INTO t2 VALUES(3, NULL); 121 DELETE FROM t2 WHERE a = 2; 122 INSERT INTO t2 VALUES(4, NULL); 123 UPDATE t2 SET b=0 WHERE b=1; 124 } 125 3 { INSERT INTO t3 SELECT *, NULL FROM t2 } 126 4 { 127 INSERT INTO t3 SELECT a||a, b||b, NULL FROM t3; 128 DELETE FROM t3 WHERE rowid%2; 129 } 130 5 { UPDATE t3 SET c = a||b } 131 6 { UPDATE t1 SET a = 32 } 132} { 133 do_then_apply_sql $sql 134 do_test $tn { compare_db db db2 } {} 135} 136 137finish_test 138 139