xref: /sqlite-3.40.0/ext/session/session3.test (revision e5754eec)
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
21
22set testprefix session3
23
24#-------------------------------------------------------------------------
25# These tests - session3-1.* - verify that the session module behaves
26# correctly when confronted with a schema mismatch when applying a
27# changeset (in function sqlite3changeset_apply()).
28#
29#   session3-1.1.*: Table does not exist in target db.
30#   session3-1.2.*: Table has wrong number of columns in target db.
31#   session3-1.3.*: Table has wrong PK columns in target db.
32#
33db close
34sqlite3_shutdown
35test_sqlite3_log log
36sqlite3 db test.db
37
38proc log {code msg} { lappend ::log $code $msg }
39
40forcedelete test.db2
41sqlite3 db2 test.db2
42
43do_execsql_test 1.0 {
44  CREATE TABLE t1(a PRIMARY KEY, b);
45}
46do_test 1.1 {
47  set ::log {}
48  do_then_apply_sql {
49    INSERT INTO t1 VALUES(1, 2);
50    INSERT INTO t1 VALUES(3, 4);
51  }
52  set ::log
53} {SQLITE_SCHEMA {sqlite3changeset_apply(): no such table: t1}}
54
55do_test 1.2.0 {
56  execsql { CREATE TABLE t1(a PRIMARY KEY, b, c) } db2
57} {}
58do_test 1.2.1 {
59  set ::log {}
60  do_then_apply_sql {
61    INSERT INTO t1 VALUES(5, 6);
62    INSERT INTO t1 VALUES(7, 8);
63  }
64  set ::log
65} {SQLITE_SCHEMA {sqlite3changeset_apply(): table t1 has 3 columns, expected 2}}
66
67do_test 1.3.0 {
68  execsql {
69    DROP TABLE t1;
70    CREATE TABLE t1(a, b PRIMARY KEY);
71  } db2
72} {}
73do_test 1.3.1 {
74  set ::log {}
75  do_then_apply_sql {
76    INSERT INTO t1 VALUES(9, 10);
77    INSERT INTO t1 VALUES(11, 12);
78  }
79  set ::log
80} {SQLITE_SCHEMA {sqlite3changeset_apply(): primary key mismatch for table t1}}
81
82#-------------------------------------------------------------------------
83# These tests - session3-2.* - verify that the session module behaves
84# correctly when the schema of an attached table is modified during the
85# session.
86#
87#   session3-2.1.*: Table is dropped midway through the session.
88#   session3-2.2.*: Table is dropped and recreated with a different # cols.
89#   session3-2.3.*: Table is dropped and recreated with a different PK.
90#
91# In all of these scenarios, the call to sqlite3session_changeset() will
92# return SQLITE_SCHEMA. Also:
93#
94#   session3-2.4.*: Table is dropped and recreated with an identical schema.
95#                   In this case sqlite3session_changeset() returns SQLITE_OK.
96#
97
98do_test 2.1 {
99  execsql { CREATE TABLE t2(a, b PRIMARY KEY) }
100  sqlite3session S db main
101  S attach t2
102  execsql {
103    INSERT INTO t2 VALUES(1, 2);
104    DROP TABLE t2;
105  }
106  list [catch { S changeset } msg] $msg
107} {1 SQLITE_SCHEMA}
108
109do_test 2.2.1 {
110  S delete
111  sqlite3session S db main
112  execsql { CREATE TABLE t2(a, b PRIMARY KEY, c) }
113  S attach t2
114  execsql {
115    INSERT INTO t2 VALUES(1, 2, 3);
116    DROP TABLE t2;
117    CREATE TABLE t2(a, b PRIMARY KEY);
118  }
119  list [catch { S changeset } msg] $msg
120} {1 SQLITE_SCHEMA}
121
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}
137
138do_test 2.3 {
139  S delete
140  sqlite3session S db main
141  execsql {
142    DROP TABLE t2;
143    CREATE TABLE t2(a, b PRIMARY KEY);
144  }
145  S attach t2
146  execsql {
147    INSERT INTO t2 VALUES(1, 2);
148    DROP TABLE t2;
149    CREATE TABLE t2(a PRIMARY KEY, b);
150  }
151  list [catch { S changeset } msg] $msg
152} {1 SQLITE_SCHEMA}
153
154do_test 2.4 {
155  S delete
156  sqlite3session S db main
157  execsql {
158    DROP TABLE t2;
159    CREATE TABLE t2(a, b PRIMARY KEY);
160  }
161  S attach t2
162  execsql {
163    INSERT INTO t2 VALUES(1, 2);
164    DROP TABLE t2;
165    CREATE TABLE t2(a, b PRIMARY KEY);
166  }
167  list [catch { S changeset } msg] $msg
168} {0 {}}
169
170S delete
171
172
173catch { db close }
174catch { db2 close }
175sqlite3_shutdown
176test_sqlite3_log
177sqlite3_initialize
178
179finish_test
180
181