2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 15
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 miscellanous features that were
2N/A# left out of other test files.
2N/A#
2N/A# $Id: misc1.test,v 1.23 2003/08/05 13:13:39 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Test the creation and use of tables that have a large number
2N/A# of columns.
2N/A#
2N/Ado_test misc1-1.1 {
2N/A set cmd "CREATE TABLE manycol(x0 text"
2N/A for {set i 1} {$i<=99} {incr i} {
2N/A append cmd ",x$i text"
2N/A }
2N/A append cmd ")";
2N/A execsql $cmd
2N/A set cmd "INSERT INTO manycol VALUES(0"
2N/A for {set i 1} {$i<=99} {incr i} {
2N/A append cmd ",$i"
2N/A }
2N/A append cmd ")";
2N/A execsql $cmd
2N/A execsql "SELECT x99 FROM manycol"
2N/A} 99
2N/Ado_test misc1-1.2 {
2N/A execsql {SELECT x0, x10, x25, x50, x75 FROM manycol}
2N/A} {0 10 25 50 75}
2N/Ado_test misc1-1.3.1 {
2N/A for {set j 100} {$j<=1000} {incr j 100} {
2N/A set cmd "INSERT INTO manycol VALUES($j"
2N/A for {set i 1} {$i<=99} {incr i} {
2N/A append cmd ",[expr {$i+$j}]"
2N/A }
2N/A append cmd ")"
2N/A execsql $cmd
2N/A }
2N/A execsql {SELECT x50 FROM manycol ORDER BY x80+0}
2N/A} {50 150 250 350 450 550 650 750 850 950 1050}
2N/Ado_test misc1-1.3.2 {
2N/A execsql {SELECT x50 FROM manycol ORDER BY x80}
2N/A} {1050 150 250 350 450 550 650 750 50 850 950}
2N/Ado_test misc1-1.4 {
2N/A execsql {SELECT x75 FROM manycol WHERE x50=350}
2N/A} 375
2N/Ado_test misc1-1.5 {
2N/A execsql {SELECT x50 FROM manycol WHERE x99=599}
2N/A} 550
2N/Ado_test misc1-1.6 {
2N/A execsql {CREATE INDEX manycol_idx1 ON manycol(x99)}
2N/A execsql {SELECT x50 FROM manycol WHERE x99=899}
2N/A} 850
2N/Ado_test misc1-1.7 {
2N/A execsql {SELECT count(*) FROM manycol}
2N/A} 11
2N/Ado_test misc1-1.8 {
2N/A execsql {DELETE FROM manycol WHERE x98=1234}
2N/A execsql {SELECT count(*) FROM manycol}
2N/A} 11
2N/Ado_test misc1-1.9 {
2N/A execsql {DELETE FROM manycol WHERE x98=998}
2N/A execsql {SELECT count(*) FROM manycol}
2N/A} 10
2N/Ado_test misc1-1.10 {
2N/A execsql {DELETE FROM manycol WHERE x99=500}
2N/A execsql {SELECT count(*) FROM manycol}
2N/A} 10
2N/Ado_test misc1-1.11 {
2N/A execsql {DELETE FROM manycol WHERE x99=599}
2N/A execsql {SELECT count(*) FROM manycol}
2N/A} 9
2N/A
2N/A# Check GROUP BY expressions that name two or more columns.
2N/A#
2N/Ado_test misc1-2.1 {
2N/A execsql {
2N/A BEGIN TRANSACTION;
2N/A CREATE TABLE agger(one text, two text, three text, four text);
2N/A INSERT INTO agger VALUES(1, 'one', 'hello', 'yes');
2N/A INSERT INTO agger VALUES(2, 'two', 'howdy', 'no');
2N/A INSERT INTO agger VALUES(3, 'thr', 'howareya', 'yes');
2N/A INSERT INTO agger VALUES(4, 'two', 'lothere', 'yes');
2N/A INSERT INTO agger VALUES(5, 'one', 'atcha', 'yes');
2N/A INSERT INTO agger VALUES(6, 'two', 'hello', 'no');
2N/A COMMIT
2N/A }
2N/A execsql {SELECT count(*) FROM agger}
2N/A} 6
2N/Ado_test misc1-2.2 {
2N/A execsql {SELECT sum(one), two, four FROM agger
2N/A GROUP BY two, four ORDER BY sum(one) desc}
2N/A} {8 two no 6 one yes 4 two yes 3 thr yes}
2N/Ado_test misc1-2.3 {
2N/A execsql {SELECT sum((one)), (two), (four) FROM agger
2N/A GROUP BY (two), (four) ORDER BY sum(one) desc}
2N/A} {8 two no 6 one yes 4 two yes 3 thr yes}
2N/A
2N/A# Here's a test for a bug found by Joel Lucsy. The code below
2N/A# was causing an assertion failure.
2N/A#
2N/Ado_test misc1-3.1 {
2N/A set r [execsql {
2N/A CREATE TABLE t1(a);
2N/A INSERT INTO t1 VALUES('hi');
2N/A PRAGMA full_column_names=on;
2N/A SELECT rowid, * FROM t1;
2N/A }]
2N/A lindex $r 1
2N/A} {hi}
2N/A
2N/A# Here's a test for yet another bug found by Joel Lucsy. The code
2N/A# below was causing an assertion failure.
2N/A#
2N/Ado_test misc1-4.1 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE t2(a);
2N/A INSERT INTO t2 VALUES('This is a long string to use up a lot of disk -');
2N/A UPDATE t2 SET a=a||a||a||a;
2N/A INSERT INTO t2 SELECT '1 - ' || a FROM t2;
2N/A INSERT INTO t2 SELECT '2 - ' || a FROM t2;
2N/A INSERT INTO t2 SELECT '3 - ' || a FROM t2;
2N/A INSERT INTO t2 SELECT '4 - ' || a FROM t2;
2N/A INSERT INTO t2 SELECT '5 - ' || a FROM t2;
2N/A INSERT INTO t2 SELECT '6 - ' || a FROM t2;
2N/A COMMIT;
2N/A SELECT count(*) FROM t2;
2N/A }
2N/A} {64}
2N/A
2N/A# Make sure we actually see a semicolon or end-of-file in the SQL input
2N/A# before executing a command. Thus if "WHERE" is misspelled on an UPDATE,
2N/A# the user won't accidently update every record.
2N/A#
2N/Ado_test misc1-5.1 {
2N/A catchsql {
2N/A CREATE TABLE t3(a,b);
2N/A INSERT INTO t3 VALUES(1,2);
2N/A INSERT INTO t3 VALUES(3,4);
2N/A UPDATE t3 SET a=0 WHEREwww b=2;
2N/A }
2N/A} {1 {near "WHEREwww": syntax error}}
2N/Ado_test misc1-5.2 {
2N/A execsql {
2N/A SELECT * FROM t3 ORDER BY a;
2N/A }
2N/A} {1 2 3 4}
2N/A
2N/A# Certain keywords (especially non-standard keywords like "REPLACE") can
2N/A# also be used as identifiers. The way this works in the parser is that
2N/A# the parser first detects a syntax error, the error handling routine
2N/A# sees that the special keyword caused the error, then replaces the keyword
2N/A# with "ID" and tries again.
2N/A#
2N/A# Check the operation of this logic.
2N/A#
2N/Ado_test misc1-6.1 {
2N/A catchsql {
2N/A CREATE TABLE t4(
2N/A abort, asc, begin, cluster, conflict, copy, delimiters, desc, end,
2N/A explain, fail, ignore, key, offset, pragma, replace, temp,
2N/A vacuum, view
2N/A );
2N/A }
2N/A} {0 {}}
2N/Ado_test misc1-6.2 {
2N/A catchsql {
2N/A INSERT INTO t4
2N/A VALUES(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
2N/A }
2N/A} {0 {}}
2N/Ado_test misc1-6.3 {
2N/A execsql {
2N/A SELECT * FROM t4
2N/A }
2N/A} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
2N/Ado_test misc1-6.4 {
2N/A execsql {
2N/A SELECT abort+asc,max(key,pragma,temp) FROM t4
2N/A }
2N/A} {3 17}
2N/A
2N/A# Test for multi-column primary keys, and for multiple primary keys.
2N/A#
2N/Ado_test misc1-7.1 {
2N/A catchsql {
2N/A CREATE TABLE error1(
2N/A a TYPE PRIMARY KEY,
2N/A b TYPE PRIMARY KEY
2N/A );
2N/A }
2N/A} {1 {table "error1" has more than one primary key}}
2N/Ado_test misc1-7.2 {
2N/A catchsql {
2N/A CREATE TABLE error1(
2N/A a INTEGER PRIMARY KEY,
2N/A b TYPE PRIMARY KEY
2N/A );
2N/A }
2N/A} {1 {table "error1" has more than one primary key}}
2N/Ado_test misc1-7.3 {
2N/A execsql {
2N/A CREATE TABLE t5(a,b,c,PRIMARY KEY(a,b));
2N/A INSERT INTO t5 VALUES(1,2,3);
2N/A SELECT * FROM t5 ORDER BY a;
2N/A }
2N/A} {1 2 3}
2N/Ado_test misc1-7.4 {
2N/A catchsql {
2N/A INSERT INTO t5 VALUES(1,2,4);
2N/A }
2N/A} {1 {columns a, b are not unique}}
2N/Ado_test misc1-7.5 {
2N/A catchsql {
2N/A INSERT INTO t5 VALUES(0,2,4);
2N/A }
2N/A} {0 {}}
2N/Ado_test misc1-7.6 {
2N/A execsql {
2N/A SELECT * FROM t5 ORDER BY a;
2N/A }
2N/A} {0 2 4 1 2 3}
2N/A
2N/Ado_test misc1-8.1 {
2N/A catchsql {
2N/A SELECT *;
2N/A }
2N/A} {1 {no tables specified}}
2N/Ado_test misc1-8.2 {
2N/A catchsql {
2N/A SELECT t1.*;
2N/A }
2N/A} {1 {no such table: t1}}
2N/A
2N/Aexecsql {
2N/A DROP TABLE t1;
2N/A DROP TABLE t2;
2N/A DROP TABLE t3;
2N/A DROP TABLE t4;
2N/A}
2N/A
2N/A# If an integer is too big to be represented as a 32-bit machine integer,
2N/A# then treat it as a string.
2N/A#
2N/Ado_test misc1-9.1 {
2N/A catchsql {
2N/A CREATE TABLE t1(a unique not null, b unique not null);
2N/A INSERT INTO t1 VALUES('a',12345678901234567890);
2N/A INSERT INTO t1 VALUES('b',12345678911234567890);
2N/A INSERT INTO t1 VALUES('c',12345678921234567890);
2N/A SELECT * FROM t1;
2N/A }
2N/A} {0 {a 12345678901234567890 b 12345678911234567890 c 12345678921234567890}}
2N/A
2N/A# A WHERE clause is not allowed to contain more than 99 terms. Check to
2N/A# make sure this limit is enforced.
2N/A#
2N/Ado_test misc1-10.0 {
2N/A execsql {SELECT count(*) FROM manycol}
2N/A} {9}
2N/Ado_test misc1-10.1 {
2N/A set ::where {WHERE x0>=0}
2N/A for {set i 1} {$i<=99} {incr i} {
2N/A append ::where " AND x$i<>0"
2N/A }
2N/A catchsql "SELECT count(*) FROM manycol $::where"
2N/A} {0 9}
2N/Ado_test misc1-10.2 {
2N/A catchsql "SELECT count(*) FROM manycol $::where AND rowid>0"
2N/A} {1 {WHERE clause too complex - no more than 100 terms allowed}}
2N/Ado_test misc1-10.3 {
2N/A regsub "x0>=0" $::where "x0=0" ::where
2N/A catchsql "DELETE FROM manycol $::where"
2N/A} {0 {}}
2N/Ado_test misc1-10.4 {
2N/A execsql {SELECT count(*) FROM manycol}
2N/A} {8}
2N/Ado_test misc1-10.5 {
2N/A catchsql "DELETE FROM manycol $::where AND rowid>0"
2N/A} {1 {WHERE clause too complex - no more than 100 terms allowed}}
2N/Ado_test misc1-10.6 {
2N/A execsql {SELECT x1 FROM manycol WHERE x0=100}
2N/A} {101}
2N/Ado_test misc1-10.7 {
2N/A regsub "x0=0" $::where "x0=100" ::where
2N/A catchsql "UPDATE manycol SET x1=x1+1 $::where"
2N/A} {0 {}}
2N/Ado_test misc1-10.8 {
2N/A execsql {SELECT x1 FROM manycol WHERE x0=100}
2N/A} {102}
2N/Ado_test misc1-10.9 {
2N/A catchsql "UPDATE manycol SET x1=x1+1 $::where AND rowid>0"
2N/A} {1 {WHERE clause too complex - no more than 100 terms allowed}}
2N/Ado_test misc1-10.10 {
2N/A execsql {SELECT x1 FROM manycol WHERE x0=100}
2N/A} {102}
2N/A
2N/A# Make sure the initialization works even if a database is opened while
2N/A# another process has the database locked.
2N/A#
2N/Ado_test misc1-11.1 {
2N/A execsql {BEGIN}
2N/A sqlite db2 test.db
2N/A set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
2N/A lappend rc $msg
2N/A} {1 {database is locked}}
2N/Ado_test misc1-11.2 {
2N/A execsql {COMMIT}
2N/A set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
2N/A db2 close
2N/A lappend rc $msg
2N/A} {0 3}
2N/A
2N/A# Make sure string comparisons really do compare strings in format4+.
2N/A# Similar tests in the format3.test file show that for format3 and earlier
2N/A# all comparisions where numeric if either operand looked like a number.
2N/A#
2N/Ado_test misc1-12.1 {
2N/A execsql {SELECT '0'=='0.0'}
2N/A} {0}
2N/Ado_test misc1-12.2 {
2N/A execsql {SELECT '0'==0.0}
2N/A} {1}
2N/Ado_test misc1-12.3 {
2N/A execsql {SELECT '12345678901234567890'=='12345678901234567891'}
2N/A} {0}
2N/Ado_test misc1-12.4 {
2N/A execsql {
2N/A CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
2N/A INSERT INTO t6 VALUES('0','0.0');
2N/A SELECT * FROM t6;
2N/A }
2N/A} {0 0.0}
2N/Ado_test misc1-12.5 {
2N/A execsql {
2N/A INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
2N/A SELECT * FROM t6;
2N/A }
2N/A} {0 0.0}
2N/Ado_test misc1-12.6 {
2N/A execsql {
2N/A INSERT OR IGNORE INTO t6 VALUES('y',0);
2N/A SELECT * FROM t6;
2N/A }
2N/A} {0 0.0 y 0}
2N/Ado_test misc1-12.7 {
2N/A execsql {
2N/A CREATE TABLE t7(x INTEGER, y TEXT, z);
2N/A INSERT INTO t7 VALUES(0,0,1);
2N/A INSERT INTO t7 VALUES(0.0,0,2);
2N/A INSERT INTO t7 VALUES(0,0.0,3);
2N/A INSERT INTO t7 VALUES(0.0,0.0,4);
2N/A SELECT DISTINCT x, y FROM t7 ORDER BY z;
2N/A }
2N/A} {0 0 0 0.0}
2N/Ado_test misc1-12.8 {
2N/A execsql {
2N/A SELECT min(z), max(z), count(z) FROM t7 GROUP BY x ORDER BY 1;
2N/A }
2N/A} {1 4 4}
2N/Ado_test misc1-12.9 {
2N/A execsql {
2N/A SELECT min(z), max(z), count(z) FROM t7 GROUP BY y ORDER BY 1;
2N/A }
2N/A} {1 2 2 3 4 2}
2N/A
2N/A# This used to be an error. But we changed the code so that arbitrary
2N/A# identifiers can be used as a collating sequence. Collation is by text
2N/A# if the identifier contains "text", "blob", or "clob" and is numeric
2N/A# otherwise.
2N/Ado_test misc1-12.10 {
2N/A catchsql {
2N/A SELECT * FROM t6 ORDER BY a COLLATE unknown;
2N/A }
2N/A} {0 {0 0.0 y 0}}
2N/Ado_test misc1-12.11 {
2N/A execsql {
2N/A CREATE TABLE t8(x TEXT COLLATE numeric, y INTEGER COLLATE text, z);
2N/A INSERT INTO t8 VALUES(0,0,1);
2N/A INSERT INTO t8 VALUES(0.0,0,2);
2N/A INSERT INTO t8 VALUES(0,0.0,3);
2N/A INSERT INTO t8 VALUES(0.0,0.0,4);
2N/A SELECT DISTINCT x, y FROM t8 ORDER BY z;
2N/A }
2N/A} {0 0 0 0.0}
2N/Ado_test misc1-12.12 {
2N/A execsql {
2N/A SELECT min(z), max(z), count(z) FROM t8 GROUP BY x ORDER BY 1;
2N/A }
2N/A} {1 4 4}
2N/Ado_test misc1-12.13 {
2N/A execsql {
2N/A SELECT min(z), max(z), count(z) FROM t8 GROUP BY y ORDER BY 1;
2N/A }
2N/A} {1 2 2 3 4 2}
2N/A
2N/A# There was a problem with realloc() in the OP_MemStore operation of
2N/A# the VDBE. A buffer was being reallocated but some pointers into
2N/A# the old copy of the buffer were not being moved over to the new copy.
2N/A# The following code tests for the problem.
2N/A#
2N/Ado_test misc1-13.1 {
2N/A execsql {
2N/A CREATE TABLE t9(x,y);
2N/A INSERT INTO t9 VALUES('one',1);
2N/A INSERT INTO t9 VALUES('two',2);
2N/A INSERT INTO t9 VALUES('three',3);
2N/A INSERT INTO t9 VALUES('four',4);
2N/A INSERT INTO t9 VALUES('five',5);
2N/A INSERT INTO t9 VALUES('six',6);
2N/A INSERT INTO t9 VALUES('seven',7);
2N/A INSERT INTO t9 VALUES('eight',8);
2N/A INSERT INTO t9 VALUES('nine',9);
2N/A INSERT INTO t9 VALUES('ten',10);
2N/A INSERT INTO t9 VALUES('eleven',11);
2N/A SELECT y FROM t9
2N/A WHERE x=(SELECT x FROM t9 WHERE y=1)
2N/A OR x=(SELECT x FROM t9 WHERE y=2)
2N/A OR x=(SELECT x FROM t9 WHERE y=3)
2N/A OR x=(SELECT x FROM t9 WHERE y=4)
2N/A OR x=(SELECT x FROM t9 WHERE y=5)
2N/A OR x=(SELECT x FROM t9 WHERE y=6)
2N/A OR x=(SELECT x FROM t9 WHERE y=7)
2N/A OR x=(SELECT x FROM t9 WHERE y=8)
2N/A OR x=(SELECT x FROM t9 WHERE y=9)
2N/A OR x=(SELECT x FROM t9 WHERE y=10)
2N/A OR x=(SELECT x FROM t9 WHERE y=11)
2N/A OR x=(SELECT x FROM t9 WHERE y=12)
2N/A OR x=(SELECT x FROM t9 WHERE y=13)
2N/A OR x=(SELECT x FROM t9 WHERE y=14)
2N/A ;
2N/A }
2N/A} {1 2 3 4 5 6 7 8 9 10 11}
2N/A
2N/A# Make sure a database connection still works after changing the
2N/A# working directory.
2N/A#
2N/Ado_test misc1-14.1 {
2N/A file mkdir tempdir
2N/A cd tempdir
2N/A execsql {BEGIN}
2N/A file exists ./test.db-journal
2N/A} {0}
2N/Ado_test misc1-14.2 {
2N/A file exists ../test.db-journal
2N/A} {1}
2N/Ado_test misc1-14.3 {
2N/A cd ..
2N/A file delete tempdir
2N/A execsql {COMMIT}
2N/A file exists ./test.db-journal
2N/A} {0}
2N/A
2N/A# A failed create table should not leave the table in the internal
2N/A# data structures. Ticket #238.
2N/A#
2N/Ado_test misc1-15.1 {
2N/A catchsql {
2N/A CREATE TABLE t10 AS SELECT c1;
2N/A }
2N/A} {1 {no such column: c1}}
2N/Ado_test misc1-15.2 {
2N/A catchsql {
2N/A CREATE TABLE t10 AS SELECT 1;
2N/A }
2N/A # The bug in ticket #238 causes the statement above to fail with
2N/A # the error "table t10 alread exists"
2N/A} {0 {}}
2N/A
2N/A# Test for memory leaks when a CREATE TABLE containing a primary key
2N/A# fails. Ticket #249.
2N/A#
2N/Ado_test misc1-16.1 {
2N/A catchsql {SELECT name FROM sqlite_master LIMIT 1}
2N/A catchsql {
2N/A CREATE TABLE test(a integer, primary key(a));
2N/A }
2N/A} {0 {}}
2N/Ado_test misc1-16.2 {
2N/A catchsql {
2N/A CREATE TABLE test(a integer, primary key(a));
2N/A }
2N/A} {1 {table test already exists}}
2N/Ado_test misc1-16.3 {
2N/A catchsql {
2N/A CREATE TABLE test2(a text primary key, b text, primary key(a,b));
2N/A }
2N/A} {1 {table "test2" has more than one primary key}}
2N/Ado_test misc1-16.4 {
2N/A execsql {
2N/A INSERT INTO test VALUES(1);
2N/A SELECT rowid, a FROM test;
2N/A }
2N/A} {1 1}
2N/Ado_test misc1-16.5 {
2N/A execsql {
2N/A INSERT INTO test VALUES(5);
2N/A SELECT rowid, a FROM test;
2N/A }
2N/A} {1 1 5 5}
2N/Ado_test misc1-16.6 {
2N/A execsql {
2N/A INSERT INTO test VALUES(NULL);
2N/A SELECT rowid, a FROM test;
2N/A }
2N/A} {1 1 5 5 6 6}
2N/A
2N/A# Ticket #333: Temp triggers that modify persistent tables.
2N/A#
2N/Ado_test misc1-17.1 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE RealTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
2N/A CREATE TEMP TABLE TempTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
2N/A CREATE TEMP TRIGGER trigTest_1 AFTER UPDATE ON TempTable BEGIN
2N/A INSERT INTO RealTable(TestString)
2N/A SELECT new.TestString FROM TempTable LIMIT 1;
2N/A END;
2N/A INSERT INTO TempTable(TestString) VALUES ('1');
2N/A INSERT INTO TempTable(TestString) VALUES ('2');
2N/A UPDATE TempTable SET TestString = TestString + 1 WHERE TestID IN (1, 2);
2N/A COMMIT;
2N/A SELECT TestString FROM RealTable ORDER BY 1;
2N/A }
2N/A} {2 3}
2N/A
2N/Afinish_test