1# 2011 March 24 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 the session module. More 12# specifically, it focuses on testing the session modules response to 13# database schema modifications and mismatches. 14# 15 16if {![info exists testdir]} { 17 set testdir [file join [file dirname [info script]] .. .. test] 18} 19source [file join [file dirname [info script]] session_common.tcl] 20source $testdir/tester.tcl 21ifcapable !session {finish_test; return} 22 23set testprefix session3 24 25#------------------------------------------------------------------------- 26# These tests - session3-1.* - verify that the session module behaves 27# correctly when confronted with a schema mismatch when applying a 28# changeset (in function sqlite3changeset_apply()). 29# 30# session3-1.1.*: Table does not exist in target db. 31# session3-1.2.*: Table has wrong number of columns in target db. 32# session3-1.3.*: Table has wrong PK columns in target db. 33# 34db close 35sqlite3_shutdown 36test_sqlite3_log log 37sqlite3 db test.db 38 39proc log {code msg} { lappend ::log $code $msg } 40 41forcedelete test.db2 42sqlite3 db2 test.db2 43 44do_execsql_test 1.0 { 45 CREATE TABLE t1(a PRIMARY KEY, b); 46} 47do_test 1.1 { 48 set ::log {} 49 do_then_apply_sql { 50 INSERT INTO t1 VALUES(1, 2); 51 INSERT INTO t1 VALUES(3, 4); 52 } 53 set ::log 54} {SQLITE_SCHEMA {sqlite3changeset_apply(): no such table: t1}} 55 56do_test 1.2.0 { 57 execsql { CREATE TABLE t1(a PRIMARY KEY, b, c) } db2 58} {} 59do_test 1.2.1 { 60 set ::log {} 61 do_then_apply_sql { 62 INSERT INTO t1 VALUES(5, 6); 63 INSERT INTO t1 VALUES(7, 8); 64 } 65 set ::log 66} {SQLITE_SCHEMA {sqlite3changeset_apply(): table t1 has 3 columns, expected 2}} 67 68do_test 1.3.0 { 69 execsql { 70 DROP TABLE t1; 71 CREATE TABLE t1(a, b PRIMARY KEY); 72 } db2 73} {} 74do_test 1.3.1 { 75 set ::log {} 76 do_then_apply_sql { 77 INSERT INTO t1 VALUES(9, 10); 78 INSERT INTO t1 VALUES(11, 12); 79 } 80 set ::log 81} {SQLITE_SCHEMA {sqlite3changeset_apply(): primary key mismatch for table t1}} 82 83#------------------------------------------------------------------------- 84# These tests - session3-2.* - verify that the session module behaves 85# correctly when the schema of an attached table is modified during the 86# session. 87# 88# session3-2.1.*: Table is dropped midway through the session. 89# session3-2.2.*: Table is dropped and recreated with a different # cols. 90# session3-2.3.*: Table is dropped and recreated with a different PK. 91# 92# In all of these scenarios, the call to sqlite3session_changeset() will 93# return SQLITE_SCHEMA. Also: 94# 95# session3-2.4.*: Table is dropped and recreated with an identical schema. 96# In this case sqlite3session_changeset() returns SQLITE_OK. 97# 98 99do_test 2.1 { 100 execsql { CREATE TABLE t2(a, b PRIMARY KEY) } 101 sqlite3session S db main 102 S attach t2 103 execsql { 104 INSERT INTO t2 VALUES(1, 2); 105 DROP TABLE t2; 106 } 107 list [catch { S changeset } msg] $msg 108} {1 SQLITE_SCHEMA} 109 110do_test 2.2.1 { 111 S delete 112 sqlite3session S db main 113 execsql { CREATE TABLE t2(a, b PRIMARY KEY, c) } 114 S attach t2 115 execsql { 116 INSERT INTO t2 VALUES(1, 2, 3); 117 DROP TABLE t2; 118 CREATE TABLE t2(a, b PRIMARY KEY); 119 } 120 list [catch { S changeset } msg] $msg 121} {1 SQLITE_SCHEMA} 122do_test 2.2.2 { 123 S delete 124 sqlite3session S db main 125 execsql { 126 DROP TABLE t2; 127 CREATE TABLE t2(a, b PRIMARY KEY, c); 128 } 129 S attach t2 130 execsql { 131 INSERT INTO t2 VALUES(1, 2, 3); 132 DROP TABLE t2; 133 CREATE TABLE t2(a, b PRIMARY KEY, c, d); 134 } 135 list [catch { S changeset } msg] $msg 136} {1 SQLITE_SCHEMA} 137do_test 2.2.3 { 138 S delete 139 sqlite3session S db main 140 execsql { 141 DROP TABLE t2; 142 CREATE TABLE t2(a, b PRIMARY KEY, c); 143 } 144 S attach t2 145 execsql { 146 INSERT INTO t2 VALUES(1, 2, 3); 147 DROP TABLE t2; 148 CREATE TABLE t2(a, b PRIMARY KEY); 149 INSERT INTO t2 VALUES(4, 5); 150 } 151 list [catch { S changeset } msg] $msg 152} {1 SQLITE_SCHEMA} 153do_test 2.2.4 { 154 S delete 155 sqlite3session S db main 156 execsql { 157 DROP TABLE t2; 158 CREATE TABLE t2(a, b PRIMARY KEY, c); 159 } 160 S attach t2 161 execsql { 162 INSERT INTO t2 VALUES(1, 2, 3); 163 DROP TABLE t2; 164 CREATE TABLE t2(a, b PRIMARY KEY, c, d); 165 INSERT INTO t2 VALUES(4, 5, 6, 7); 166 } 167 list [catch { S changeset } msg] $msg 168} {1 SQLITE_SCHEMA} 169 170do_test 2.3 { 171 S delete 172 sqlite3session S db main 173 execsql { 174 DROP TABLE t2; 175 CREATE TABLE t2(a, b PRIMARY KEY); 176 } 177 S attach t2 178 execsql { 179 INSERT INTO t2 VALUES(1, 2); 180 DROP TABLE t2; 181 CREATE TABLE t2(a PRIMARY KEY, b); 182 } 183 list [catch { S changeset } msg] $msg 184} {1 SQLITE_SCHEMA} 185 186do_test 2.4 { 187 S delete 188 sqlite3session S db main 189 execsql { 190 DROP TABLE t2; 191 CREATE TABLE t2(a, b PRIMARY KEY); 192 } 193 S attach t2 194 execsql { 195 INSERT INTO t2 VALUES(1, 2); 196 DROP TABLE t2; 197 CREATE TABLE t2(a, b PRIMARY KEY); 198 } 199 list [catch { S changeset } msg] $msg 200} {0 {}} 201 202S delete 203 204 205catch { db close } 206catch { db2 close } 207sqlite3_shutdown 208test_sqlite3_log 209sqlite3_initialize 210 211finish_test 212