2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 October 7
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.
2N/A#
2N/A# This file implements tests for temporary tables and indices.
2N/A#
2N/A# $Id: temptable.test,v 1.11 2004/02/14 16:31:04 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Create an alternative connection to the database
2N/A#
2N/Ado_test temptable-1.0 {
2N/A sqlite db2 ./test.db
2N/A set dummy {}
2N/A} {}
2N/A
2N/A# Create a permanent table.
2N/A#
2N/Ado_test temptable-1.1 {
2N/A execsql {CREATE TABLE t1(a,b,c);}
2N/A execsql {INSERT INTO t1 VALUES(1,2,3);}
2N/A execsql {SELECT * FROM t1}
2N/A} {1 2 3}
2N/Ado_test temptable-1.2 {
2N/A catch {db2 eval {SELECT * FROM sqlite_master}}
2N/A db2 eval {SELECT * FROM t1}
2N/A} {1 2 3}
2N/Ado_test temptable-1.3 {
2N/A execsql {SELECT name FROM sqlite_master}
2N/A} {t1}
2N/Ado_test temptable-1.4 {
2N/A db2 eval {SELECT name FROM sqlite_master}
2N/A} {t1}
2N/A
2N/A# Create a temporary table. Verify that only one of the two
2N/A# processes can see it.
2N/A#
2N/Ado_test temptable-1.5 {
2N/A db2 eval {
2N/A CREATE TEMP TABLE t2(x,y,z);
2N/A INSERT INTO t2 VALUES(4,5,6);
2N/A }
2N/A db2 eval {SELECT * FROM t2}
2N/A} {4 5 6}
2N/Ado_test temptable-1.6 {
2N/A catch {execsql {SELECT * FROM sqlite_master}}
2N/A catchsql {SELECT * FROM t2}
2N/A} {1 {no such table: t2}}
2N/Ado_test temptable-1.7 {
2N/A catchsql {INSERT INTO t2 VALUES(8,9,0);}
2N/A} {1 {no such table: t2}}
2N/Ado_test temptable-1.8 {
2N/A db2 eval {INSERT INTO t2 VALUES(8,9,0);}
2N/A db2 eval {SELECT * FROM t2 ORDER BY x}
2N/A} {4 5 6 8 9 0}
2N/Ado_test temptable-1.9 {
2N/A db2 eval {DELETE FROM t2 WHERE x==8}
2N/A db2 eval {SELECT * FROM t2 ORDER BY x}
2N/A} {4 5 6}
2N/Ado_test temptable-1.10 {
2N/A db2 eval {DELETE FROM t2}
2N/A db2 eval {SELECT * FROM t2}
2N/A} {}
2N/Ado_test temptable-1.11 {
2N/A db2 eval {
2N/A INSERT INTO t2 VALUES(7,6,5);
2N/A INSERT INTO t2 VALUES(4,3,2);
2N/A SELECT * FROM t2 ORDER BY x;
2N/A }
2N/A} {4 3 2 7 6 5}
2N/Ado_test temptable-1.12 {
2N/A db2 eval {DROP TABLE t2;}
2N/A set r [catch {db2 eval {SELECT * FROM t2}} msg]
2N/A lappend r $msg
2N/A} {1 {no such table: t2}}
2N/A
2N/A# Make sure temporary tables work with transactions
2N/A#
2N/Ado_test temptable-2.1 {
2N/A execsql {
2N/A BEGIN TRANSACTION;
2N/A CREATE TEMPORARY TABLE t2(x,y);
2N/A INSERT INTO t2 VALUES(1,2);
2N/A SELECT * FROM t2;
2N/A }
2N/A} {1 2}
2N/Ado_test temptable-2.2 {
2N/A execsql {ROLLBACK}
2N/A catchsql {SELECT * FROM t2}
2N/A} {1 {no such table: t2}}
2N/Ado_test temptable-2.3 {
2N/A execsql {
2N/A BEGIN TRANSACTION;
2N/A CREATE TEMPORARY TABLE t2(x,y);
2N/A INSERT INTO t2 VALUES(1,2);
2N/A SELECT * FROM t2;
2N/A }
2N/A} {1 2}
2N/Ado_test temptable-2.4 {
2N/A execsql {COMMIT}
2N/A catchsql {SELECT * FROM t2}
2N/A} {0 {1 2}}
2N/Ado_test temptable-2.5 {
2N/A set r [catch {db2 eval {SELECT * FROM t2}} msg]
2N/A lappend r $msg
2N/A} {1 {no such table: t2}}
2N/A
2N/A# Make sure indices on temporary tables are also temporary.
2N/A#
2N/Ado_test temptable-3.1 {
2N/A execsql {
2N/A CREATE INDEX i2 ON t2(x);
2N/A SELECT name FROM sqlite_master WHERE type='index';
2N/A }
2N/A} {}
2N/Ado_test temptable-3.2 {
2N/A execsql {
2N/A SELECT y FROM t2 WHERE x=1;
2N/A }
2N/A} {2}
2N/Ado_test temptable-3.3 {
2N/A execsql {
2N/A DROP INDEX i2;
2N/A SELECT y FROM t2 WHERE x=1;
2N/A }
2N/A} {2}
2N/Ado_test temptable-3.4 {
2N/A execsql {
2N/A CREATE INDEX i2 ON t2(x);
2N/A DROP TABLE t2;
2N/A }
2N/A catchsql {DROP INDEX i2}
2N/A} {1 {no such index: i2}}
2N/A
2N/A# Check for correct name collision processing. A name collision can
2N/A# occur when process A creates a temporary table T then process B
2N/A# creates a permanent table also named T. The temp table in process A
2N/A# hides the existance of the permanent table.
2N/A#
2N/Ado_test temptable-4.1 {
2N/A execsql {
2N/A CREATE TEMP TABLE t2(x,y);
2N/A INSERT INTO t2 VALUES(10,20);
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {10 20}
2N/Ado_test temptable-4.2 {
2N/A execsql {
2N/A CREATE TABLE t2(x,y,z);
2N/A INSERT INTO t2 VALUES(9,8,7);
2N/A SELECT * FROM t2;
2N/A }
2N/A} {9 8 7}
2N/Ado_test temptable-4.3 {
2N/A catchsql {
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {0 {10 20}}
2N/Ado_test temptable-4.4.1 {
2N/A catchsql {
2N/A SELECT * FROM temp.t2;
2N/A } db2
2N/A} {0 {10 20}}
2N/Ado_test temptable-4.4.2 {
2N/A catchsql {
2N/A SELECT * FROM main.t2;
2N/A } db2
2N/A} {1 {no such table: main.t2}}
2N/A#do_test temptable-4.4.3 {
2N/A# catchsql {
2N/A# SELECT name FROM main.sqlite_master WHERE type='table';
2N/A# } db2
2N/A#} {1 {database schema has changed}}
2N/Ado_test temptable-4.4.4 {
2N/A catchsql {
2N/A SELECT name FROM main.sqlite_master WHERE type='table';
2N/A } db2
2N/A} {0 {t1 t2}}
2N/Ado_test temptable-4.4.5 {
2N/A catchsql {
2N/A SELECT * FROM main.t2;
2N/A } db2
2N/A} {0 {9 8 7}}
2N/Ado_test temptable-4.4.6 {
2N/A # TEMP takes precedence over MAIN
2N/A catchsql {
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {0 {10 20}}
2N/Ado_test temptable-4.5 {
2N/A catchsql {
2N/A DROP TABLE t2; -- should drop TEMP
2N/A SELECT * FROM t2; -- data should be from MAIN
2N/A } db2
2N/A} {0 {9 8 7}}
2N/Ado_test temptable-4.6 {
2N/A db2 close
2N/A sqlite db2 ./test.db
2N/A catchsql {
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {0 {9 8 7}}
2N/Ado_test temptable-4.7 {
2N/A catchsql {
2N/A DROP TABLE t2;
2N/A SELECT * FROM t2;
2N/A }
2N/A} {1 {no such table: t2}}
2N/Ado_test temptable-4.8 {
2N/A db2 close
2N/A sqlite db2 ./test.db
2N/A execsql {
2N/A CREATE TEMP TABLE t2(x unique,y);
2N/A INSERT INTO t2 VALUES(1,2);
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {1 2}
2N/Ado_test temptable-4.9 {
2N/A execsql {
2N/A CREATE TABLE t2(x unique, y);
2N/A INSERT INTO t2 VALUES(3,4);
2N/A SELECT * FROM t2;
2N/A }
2N/A} {3 4}
2N/Ado_test temptable-4.10.1 {
2N/A catchsql {
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {0 {1 2}}
2N/A#do_test temptable-4.10.2 {
2N/A# catchsql {
2N/A# SELECT name FROM sqlite_master WHERE type='table'
2N/A# } db2
2N/A#} {1 {database schema has changed}}
2N/Ado_test temptable-4.10.3 {
2N/A catchsql {
2N/A SELECT name FROM sqlite_master WHERE type='table'
2N/A } db2
2N/A} {0 {t1 t2}}
2N/Ado_test temptable-4.11 {
2N/A execsql {
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {1 2}
2N/Ado_test temptable-4.12 {
2N/A execsql {
2N/A SELECT * FROM t2;
2N/A }
2N/A} {3 4}
2N/Ado_test temptable-4.13 {
2N/A catchsql {
2N/A DROP TABLE t2; -- drops TEMP.T2
2N/A SELECT * FROM t2; -- uses MAIN.T2
2N/A } db2
2N/A} {0 {3 4}}
2N/Ado_test temptable-4.14 {
2N/A execsql {
2N/A SELECT * FROM t2;
2N/A }
2N/A} {3 4}
2N/Ado_test temptable-4.15 {
2N/A db2 close
2N/A sqlite db2 ./test.db
2N/A execsql {
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {3 4}
2N/A
2N/A# Now create a temporary table in db2 and a permanent index in db. The
2N/A# temporary table in db2 should mask the name of the permanent index,
2N/A# but the permanent index should still be accessible and should still
2N/A# be updated when its corresponding table changes.
2N/A#
2N/Ado_test temptable-5.1 {
2N/A execsql {
2N/A CREATE TEMP TABLE mask(a,b,c)
2N/A } db2
2N/A execsql {
2N/A CREATE INDEX mask ON t2(x);
2N/A SELECT * FROM t2;
2N/A }
2N/A} {3 4}
2N/A#do_test temptable-5.2 {
2N/A# catchsql {
2N/A# SELECT * FROM t2;
2N/A# } db2
2N/A#} {1 {database schema has changed}}
2N/Ado_test temptable-5.3 {
2N/A catchsql {
2N/A SELECT * FROM t2;
2N/A } db2
2N/A} {0 {3 4}}
2N/Ado_test temptable-5.4 {
2N/A execsql {
2N/A SELECT y FROM t2 WHERE x=3
2N/A }
2N/A} {4}
2N/Ado_test temptable-5.5 {
2N/A execsql {
2N/A SELECT y FROM t2 WHERE x=3
2N/A } db2
2N/A} {4}
2N/Ado_test temptable-5.6 {
2N/A execsql {
2N/A INSERT INTO t2 VALUES(1,2);
2N/A SELECT y FROM t2 WHERE x=1;
2N/A } db2
2N/A} {2}
2N/Ado_test temptable-5.7 {
2N/A execsql {
2N/A SELECT y FROM t2 WHERE x=3
2N/A } db2
2N/A} {4}
2N/Ado_test temptable-5.8 {
2N/A execsql {
2N/A SELECT y FROM t2 WHERE x=1;
2N/A }
2N/A} {2}
2N/Ado_test temptable-5.9 {
2N/A execsql {
2N/A SELECT y FROM t2 WHERE x=3
2N/A }
2N/A} {4}
2N/A
2N/Adb2 close
2N/A
2N/A# Test for correct operation of read-only databases
2N/A#
2N/Ado_test temptable-6.1 {
2N/A execsql {
2N/A CREATE TABLE t8(x);
2N/A INSERT INTO t8 VALUES('xyzzy');
2N/A SELECT * FROM t8;
2N/A }
2N/A} {xyzzy}
2N/Ado_test temptable-6.2 {
2N/A db close
2N/A catch {file attributes test.db -permissions 0444}
2N/A catch {file attributes test.db -readonly 1}
2N/A sqlite db test.db
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 execsql {
2N/A SELECT * FROM t8;
2N/A }
2N/A} {xyzzy}
2N/Ado_test temptable-6.3 {
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 catchsql {
2N/A CREATE TABLE t9(x,y);
2N/A }
2N/A} {1 {attempt to write a readonly database}}
2N/Ado_test temptable-6.4 {
2N/A catchsql {
2N/A CREATE TEMP TABLE t9(x,y);
2N/A }
2N/A} {0 {}}
2N/Ado_test temptable-6.5 {
2N/A catchsql {
2N/A INSERT INTO t9 VALUES(1,2);
2N/A SELECT * FROM t9;
2N/A }
2N/A} {0 {1 2}}
2N/Ado_test temptable-6.6 {
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 catchsql {
2N/A INSERT INTO t8 VALUES('hello');
2N/A SELECT * FROM t8;
2N/A }
2N/A} {1 {attempt to write a readonly database}}
2N/Ado_test temptable-6.7 {
2N/A catchsql {
2N/A SELECT * FROM t8,t9;
2N/A }
2N/A} {0 {xyzzy 1 2}}
2N/Ado_test temptable-6.8 {
2N/A db close
2N/A sqlite db test.db
2N/A catchsql {
2N/A SELECT * FROM t8,t9;
2N/A }
2N/A} {1 {no such table: t9}}
2N/A
2N/Afinish_test