version.test revision 2
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2002 July 17
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing the ability of the library to detect
2N/A# past or future file format version numbers and respond appropriately.
2N/A#
2N/A# $Id: version.test,v 1.9 2004/02/12 19:01:05 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Current file format version
2N/Aset VX 4
2N/A
2N/A# Create a new database
2N/A#
2N/Ado_test version-1.1 {
2N/A execsql {
2N/A CREATE TABLE t1(x);
2N/A INSERT INTO t1 VALUES(1);
2N/A INSERT INTO t1 SELECT x+1 FROM t1;
2N/A INSERT INTO t1 SELECT x+2 FROM t1;
2N/A INSERT INTO t1 SELECT x+4 FROM t1;
2N/A SELECT * FROM t1;
2N/A }
2N/A} {1 2 3 4 5 6 7 8}
2N/A
2N/A# Make sure the version number is set correctly
2N/A#
2N/Ado_test version-1.2 {
2N/A db close
2N/A set ::bt [btree_open test.db]
2N/A btree_begin_transaction $::bt
2N/A set ::meta [btree_get_meta $::bt]
2N/A btree_rollback $::bt
2N/A lindex $::meta 2
2N/A} $VX
2N/A
2N/A# Increase the file_format number by one. Verify that the
2N/A# file will refuse to open.
2N/A#
2N/Ado_test version-1.3 {
2N/A set m2 [lreplace $::meta 2 2 [expr {$::VX+1}]]
2N/A btree_begin_transaction $::bt
2N/A eval btree_update_meta $::bt $m2
2N/A btree_commit $::bt
2N/A set rc [catch {sqlite db test.db} msg]
2N/A lappend rc $msg
2N/A} {1 {unsupported file format}}
2N/A
2N/A# Decrease the file_format number by one. Verify that the
2N/A# file will open correctly.
2N/A#
2N/Ado_test version-1.4 {
2N/A set m2 [lreplace $::meta 2 2 [expr {$::VX-1}]]
2N/A btree_begin_transaction $::bt
2N/A eval btree_update_meta $::bt $m2
2N/A btree_commit $::bt
2N/A sqlite db test.db
2N/A execsql {
2N/A SELECT * FROM t1;
2N/A }
2N/A} {1 2 3 4 5 6 7 8}
2N/A
2N/A# Set the file_format number to 2. This should cause the automatic
2N/A# upgrade processing to run.
2N/A#
2N/Ado_test version-1.5 {
2N/A set m2 [lreplace $::meta 2 2 2]
2N/A btree_begin_transaction $::bt
2N/A eval btree_update_meta $::bt $m2
2N/A btree_commit $::bt
2N/A sqlite db test.db
2N/A execsql {
2N/A SELECT * FROM t1;
2N/A }
2N/A} {1 2 3 4 5 6 7 8}
2N/Ado_test version-1.6 {
2N/A set ::meta [btree_get_meta $::bt]
2N/A lindex $::meta 2
2N/A} $VX
2N/A
2N/A# Add some triggers, views, and indices to the schema and make sure the
2N/A# automatic upgrade still works.
2N/A#
2N/Ado_test version-1.7 {
2N/A execsql {
2N/A CREATE INDEX i1 ON t1(x);
2N/A DELETE FROM t1;
2N/A CREATE TABLE t2(a INTEGER PRIMARY KEY, b UNIQUE, c);
2N/A CREATE TABLE cnt(name,ins, del);
2N/A INSERT INTO cnt VALUES('t1',0,0);
2N/A INSERT INTO cnt VALUES('t2',0,0);
2N/A CREATE TRIGGER r1 AFTER INSERT ON t1 FOR EACH ROW BEGIN
2N/A UPDATE cnt SET ins=ins+1 WHERE name='t1';
2N/A END;
2N/A CREATE TRIGGER r2 AFTER DELETE ON t1 FOR EACH ROW BEGIN
2N/A UPDATE cnt SET del=del+1 WHERE name='t1';
2N/A END;
2N/A CREATE TRIGGER r3 AFTER INSERT ON t2 FOR EACH ROW BEGIN
2N/A UPDATE cnt SET ins=ins+1 WHERE name='t2';
2N/A END;
2N/A CREATE TRIGGER r4 AFTER DELETE ON t2 FOR EACH ROW BEGIN
2N/A UPDATE cnt SET del=del+1 WHERE name='t2';
2N/A END;
2N/A CREATE VIEW v1 AS SELECT x+100 FROM t1;
2N/A CREATE VIEW v2 AS SELECT sum(ins), sum(del) FROM cnt;
2N/A INSERT INTO t1 VALUES(1);
2N/A INSERT INTO t1 SELECT x+1 FROM t1;
2N/A INSERT INTO t1 SELECT x+2 FROM t1;
2N/A INSERT INTO t1 SELECT x+4 FROM t1;
2N/A SELECT * FROM t1;
2N/A }
2N/A} {1 2 3 4 5 6 7 8}
2N/Ado_test version-1.8 {
2N/A execsql {
2N/A SELECT * FROM v2;
2N/A }
2N/A} {8 0}
2N/Ado_test version-1.9 {
2N/A execsql {
2N/A SELECT * FROM cnt;
2N/A }
2N/A} {t1 8 0 t2 0 0}
2N/Ado_test version-1.10 {
2N/A execsql {
2N/A INSERT INTO t2 SELECT x*3, x*2, x FROM t1;
2N/A SELECT * FROM t2;
2N/A }
2N/A} {3 2 1 6 4 2 9 6 3 12 8 4 15 10 5 18 12 6 21 14 7 24 16 8}
2N/Ado_test version-1.11 {
2N/A execsql {
2N/A SELECT * FROM cnt;
2N/A }
2N/A} {t1 8 0 t2 8 0}
2N/A
2N/A# Here we do the upgrade test.
2N/A#
2N/Ado_test version-1.12 {
2N/A db close
2N/A set m2 [lreplace $::meta 2 2 2]
2N/A btree_begin_transaction $::bt
2N/A eval btree_update_meta $::bt $m2
2N/A btree_commit $::bt
2N/A sqlite db test.db
2N/A execsql {
2N/A SELECT * FROM cnt;
2N/A }
2N/A} {t1 8 0 t2 8 0}
2N/Ado_test version-1.13 {
2N/A execsql {
2N/A SELECT * FROM v1;
2N/A }
2N/A} {101 102 103 104 105 106 107 108}
2N/Ado_test version-1.14 {
2N/A execsql {
2N/A SELECT * FROM v2;
2N/A }
2N/A} {16 0}
2N/A
2N/A# Try to do an upgrade where the database file is read-only
2N/A#
2N/Ado_test version-2.1 {
2N/A db close
2N/A set m2 [lreplace $::meta 2 2 2]
2N/A btree_begin_transaction $::bt
2N/A eval btree_update_meta $::bt $m2
2N/A btree_commit $::bt
2N/A btree_close $::bt
2N/A catch {file attributes test.db -permissions 0444}
2N/A catch {file attributes test.db -readonly 1}
2N/A if {[file writable test.db]} {
2N/A error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
2N/A }
2N/A set rc [catch {sqlite db test.db} msg]
2N/A lappend rc $msg
2N/A} {1 {unable to upgrade database to the version 2.6 format: attempt to write a readonly database}}
2N/Ado_test version-2.2 {
2N/A file delete -force test.db
2N/A set fd [open test.db w]
2N/A set txt "This is not a valid database file\n"
2N/A while {[string length $txt]<4092} {append txt $txt}
2N/A puts $fd $txt
2N/A close $fd
2N/A set rc [catch {sqlite db test.db} msg]
2N/A lappend rc $msg
2N/A} {1 {file is encrypted or is not a database}}
2N/A
2N/A
2N/Afinish_test