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. The
2N/A# focus of this file is testing the INSERT statement.
2N/A#
2N/A# $Id: insert.test,v 1.15 2003/06/15 23:42:25 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Try to insert into a non-existant table.
2N/A#
2N/Ado_test insert-1.1 {
2N/A set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
2N/A lappend v $msg
2N/A} {1 {no such table: test1}}
2N/A
2N/A# Try to insert into sqlite_master
2N/A#
2N/Ado_test insert-1.2 {
2N/A set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
2N/A lappend v $msg
2N/A} {1 {table sqlite_master may not be modified}}
2N/A
2N/A# Try to insert the wrong number of entries.
2N/A#
2N/Ado_test insert-1.3 {
2N/A execsql {CREATE TABLE test1(one int, two int, three int)}
2N/A set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
2N/A lappend v $msg
2N/A} {1 {table test1 has 3 columns but 2 values were supplied}}
2N/Ado_test insert-1.3b {
2N/A set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
2N/A lappend v $msg
2N/A} {1 {table test1 has 3 columns but 4 values were supplied}}
2N/Ado_test insert-1.3c {
2N/A set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
2N/A lappend v $msg
2N/A} {1 {4 values for 2 columns}}
2N/Ado_test insert-1.3d {
2N/A set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
2N/A lappend v $msg
2N/A} {1 {1 values for 2 columns}}
2N/A
2N/A# Try to insert into a non-existant column of a table.
2N/A#
2N/Ado_test insert-1.4 {
2N/A set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
2N/A lappend v $msg
2N/A} {1 {table test1 has no column named four}}
2N/A
2N/A# Make sure the inserts actually happen
2N/A#
2N/Ado_test insert-1.5 {
2N/A execsql {INSERT INTO test1 VALUES(1,2,3)}
2N/A execsql {SELECT * FROM test1}
2N/A} {1 2 3}
2N/Ado_test insert-1.5b {
2N/A execsql {INSERT INTO test1 VALUES(4,5,6)}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {1 2 3 4 5 6}
2N/Ado_test insert-1.5c {
2N/A execsql {INSERT INTO test1 VALUES(7,8,9)}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {1 2 3 4 5 6 7 8 9}
2N/A
2N/Ado_test insert-1.6 {
2N/A execsql {DELETE FROM test1}
2N/A execsql {INSERT INTO test1(one,two) VALUES(1,2)}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {1 2 {}}
2N/Ado_test insert-1.6b {
2N/A execsql {INSERT INTO test1(two,three) VALUES(5,6)}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {{} 5 6 1 2 {}}
2N/Ado_test insert-1.6c {
2N/A execsql {INSERT INTO test1(three,one) VALUES(7,8)}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {{} 5 6 1 2 {} 8 {} 7}
2N/A
2N/A# A table to use for testing default values
2N/A#
2N/Ado_test insert-2.1 {
2N/A execsql {
2N/A CREATE TABLE test2(
2N/A f1 int default -111,
2N/A f2 real default +4.32,
2N/A f3 int default +222,
2N/A f4 int default 7.89
2N/A )
2N/A }
2N/A execsql {SELECT * from test2}
2N/A} {}
2N/Ado_test insert-2.2 {
2N/A execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
2N/A execsql {SELECT * FROM test2}
2N/A} {10 4.32 -10 7.89}
2N/Ado_test insert-2.3 {
2N/A execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
2N/A execsql {SELECT * FROM test2 WHERE f1==-111}
2N/A} {-111 1.23 222 -3.45}
2N/Ado_test insert-2.4 {
2N/A execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
2N/A execsql {SELECT * FROM test2 WHERE f1==77}
2N/A} {77 1.23 222 3.45}
2N/Ado_test insert-2.10 {
2N/A execsql {
2N/A DROP TABLE test2;
2N/A CREATE TABLE test2(
2N/A f1 int default 111,
2N/A f2 real default -4.32,
2N/A f3 text default hi,
2N/A f4 text default 'abc-123',
2N/A f5 varchar(10)
2N/A )
2N/A }
2N/A execsql {SELECT * from test2}
2N/A} {}
2N/Ado_test insert-2.11 {
2N/A execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
2N/A execsql {SELECT * FROM test2}
2N/A} {111 -2.22 hi hi! {}}
2N/Ado_test insert-2.12 {
2N/A execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
2N/A execsql {SELECT * FROM test2 ORDER BY f1}
2N/A} {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
2N/A
2N/A# Do additional inserts with default values, but this time
2N/A# on a table that has indices. In particular we want to verify
2N/A# that the correct default values are inserted into the indices.
2N/A#
2N/Ado_test insert-3.1 {
2N/A execsql {
2N/A DELETE FROM test2;
2N/A CREATE INDEX index9 ON test2(f1,f2);
2N/A CREATE INDEX indext ON test2(f4,f5);
2N/A SELECT * from test2;
2N/A }
2N/A} {}
2N/Ado_test insert-3.2 {
2N/A execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
2N/A execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
2N/A} {111 -3.33 hi hum {}}
2N/Ado_test insert-3.3 {
2N/A execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
2N/A execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
2N/A} {111 -3.33 hi hum {}}
2N/Ado_test insert-3.4 {
2N/A execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
2N/A} {22 -4.44 hi abc-123 wham}
2N/Aintegrity_check insert-3.5
2N/A
2N/A# Test of expressions in the VALUES clause
2N/A#
2N/Ado_test insert-4.1 {
2N/A execsql {
2N/A CREATE TABLE t3(a,b,c);
2N/A INSERT INTO t3 VALUES(1+2+3,4,5);
2N/A SELECT * FROM t3;
2N/A }
2N/A} {6 4 5}
2N/Ado_test insert-4.2 {
2N/A execsql {
2N/A INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);
2N/A SELECT * FROM t3 ORDER BY a;
2N/A }
2N/A} {6 4 5 7 5 6}
2N/Ado_test insert-4.3 {
2N/A catchsql {
2N/A INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
2N/A SELECT * FROM t3 ORDER BY a;
2N/A }
2N/A} {1 {no such column: t3.a}}
2N/Ado_test insert-4.4 {
2N/A execsql {
2N/A INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);
2N/A SELECT * FROM t3 ORDER BY a;
2N/A }
2N/A} {{} 6 7 6 4 5 7 5 6}
2N/Ado_test insert-4.5 {
2N/A execsql {
2N/A SELECT b,c FROM t3 WHERE a IS NULL;
2N/A }
2N/A} {6 7}
2N/Ado_test insert-4.6 {
2N/A catchsql {
2N/A INSERT INTO t3 VALUES(notafunc(2,3),2,3);
2N/A }
2N/A} {1 {no such function: notafunc}}
2N/Ado_test insert-4.7 {
2N/A execsql {
2N/A INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
2N/A SELECT * FROM t3 WHERE c=99;
2N/A }
2N/A} {1 3 99}
2N/A
2N/A# Test the ability to insert from a temporary table into itself.
2N/A# Ticket #275.
2N/A#
2N/Ado_test insert-5.1 {
2N/A execsql {
2N/A CREATE TEMP TABLE t4(x);
2N/A INSERT INTO t4 VALUES(1);
2N/A SELECT * FROM t4;
2N/A }
2N/A} {1}
2N/Ado_test insert-5.2 {
2N/A execsql {
2N/A INSERT INTO t4 SELECT x+1 FROM t4;
2N/A SELECT * FROM t4;
2N/A }
2N/A} {1 2}
2N/Ado_test insert-5.3 {
2N/A # verify that a temporary table is used to copy t4 to t4
2N/A set x [execsql {
2N/A EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
2N/A }]
2N/A expr {[lsearch $x OpenTemp]>0}
2N/A} {1}
2N/Ado_test insert-5.4 {
2N/A # Verify that table "test1" begins on page 3. This should be the same
2N/A # page number used by "t4" above.
2N/A execsql {
2N/A SELECT rootpage FROM sqlite_master WHERE name='test1';
2N/A }
2N/A} {3}
2N/Ado_test insert-5.5 {
2N/A # Verify that "t4" begins on page 3.
2N/A execsql {
2N/A SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
2N/A }
2N/A} {3}
2N/Ado_test insert-5.6 {
2N/A # This should not use an intermediate temporary table.
2N/A execsql {
2N/A INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
2N/A SELECT * FROM t4
2N/A }
2N/A} {1 2 8}
2N/Ado_test insert-5.7 {
2N/A # verify that no temporary table is used to copy test1 to t4
2N/A set x [execsql {
2N/A EXPLAIN INSERT INTO t4 SELECT one FROM test1;
2N/A }]
2N/A expr {[lsearch $x OpenTemp]>0}
2N/A} {0}
2N/A
2N/A# Ticket #334: REPLACE statement corrupting indices.
2N/A#
2N/Ado_test insert-6.1 {
2N/A execsql {
2N/A CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
2N/A INSERT INTO t1 VALUES(1,2);
2N/A INSERT INTO t1 VALUES(2,3);
2N/A SELECT b FROM t1 WHERE b=2;
2N/A }
2N/A} {2}
2N/Ado_test insert-6.2 {
2N/A execsql {
2N/A REPLACE INTO t1 VALUES(1,4);
2N/A SELECT b FROM t1 WHERE b=2;
2N/A }
2N/A} {}
2N/Ado_test insert-6.3 {
2N/A execsql {
2N/A UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
2N/A SELECT * FROM t1 WHERE b=4;
2N/A }
2N/A} {2 4}
2N/Ado_test insert-6.4 {
2N/A execsql {
2N/A SELECT * FROM t1 WHERE b=3;
2N/A }
2N/A} {}
2N/A
2N/Aintegrity_check insert-99.0
2N/A
2N/Afinish_test