1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
# 2005 January 13
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing corner cases of the INSERT statement.
#
# $Id: insert3.test,v 1.9 2009/04/23 14:58:40 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# All the tests in this file require trigger support
#
ifcapable {trigger} {
# Create a table and a corresponding insert trigger. Do a self-insert
# into the table.
#
do_test insert3-1.0 {
execsql {
CREATE TABLE t1(a,b);
CREATE TABLE log(x UNIQUE, y);
CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
UPDATE log SET y=y+1 WHERE x=new.a;
INSERT OR IGNORE INTO log VALUES(new.a, 1);
END;
INSERT INTO t1 VALUES('hello','world');
INSERT INTO t1 VALUES(5,10);
SELECT * FROM log ORDER BY x;
}
} {5 1 hello 1}
do_test insert3-1.1 {
execsql {
INSERT INTO t1 SELECT a, b+10 FROM t1;
SELECT * FROM log ORDER BY x;
}
} {5 2 hello 2}
do_test insert3-1.2 {
execsql {
CREATE TABLE log2(x PRIMARY KEY,y);
CREATE TRIGGER r2 BEFORE INSERT ON t1 BEGIN
UPDATE log2 SET y=y+1 WHERE x=new.b;
INSERT OR IGNORE INTO log2 VALUES(new.b,1);
END;
INSERT INTO t1 VALUES(453,'hi');
SELECT * FROM log ORDER BY x;
}
} {5 2 453 1 hello 2}
do_test insert3-1.3 {
execsql {
SELECT * FROM log2 ORDER BY x;
}
} {hi 1}
ifcapable compound {
do_test insert3-1.4.1 {
execsql {
INSERT INTO t1 SELECT * FROM t1;
SELECT 'a:', x, y FROM log UNION ALL
SELECT 'b:', x, y FROM log2 ORDER BY x;
}
} {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1}
do_test insert3-1.4.2 {
execsql {
SELECT 'a:', x, y FROM log UNION ALL
SELECT 'b:', x, y FROM log2 ORDER BY x, y;
}
} {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1}
do_test insert3-1.5 {
execsql {
INSERT INTO t1(a) VALUES('xyz');
SELECT * FROM log ORDER BY x;
}
} {5 4 453 2 hello 4 xyz 1}
}
do_test insert3-2.1 {
execsql {
CREATE TABLE t2(
a INTEGER PRIMARY KEY,
b DEFAULT 'b',
c DEFAULT 'c'
);
CREATE TABLE t2dup(a,b,c);
CREATE TRIGGER t2r1 BEFORE INSERT ON t2 BEGIN
INSERT INTO t2dup(a,b,c) VALUES(new.a,new.b,new.c);
END;
INSERT INTO t2(a) VALUES(123);
INSERT INTO t2(b) VALUES(234);
INSERT INTO t2(c) VALUES(345);
SELECT * FROM t2dup;
}
} {123 b c -1 234 c -1 b 345}
do_test insert3-2.2 {
execsql {
DELETE FROM t2dup;
INSERT INTO t2(a) SELECT 1 FROM t1 LIMIT 1;
INSERT INTO t2(b) SELECT 987 FROM t1 LIMIT 1;
INSERT INTO t2(c) SELECT 876 FROM t1 LIMIT 1;
SELECT * FROM t2dup;
}
} {1 b c -1 987 c -1 b 876}
# Test for proper detection of malformed WHEN clauses on INSERT triggers.
#
do_test insert3-3.1 {
execsql {
CREATE TABLE t3(a,b,c);
CREATE TRIGGER t3r1 BEFORE INSERT on t3 WHEN nosuchcol BEGIN
SELECT 'illegal WHEN clause';
END;
}
} {}
do_test insert3-3.2 {
catchsql {
INSERT INTO t3 VALUES(1,2,3)
}
} {1 {no such column: nosuchcol}}
do_test insert3-3.3 {
execsql {
CREATE TABLE t4(a,b,c);
CREATE TRIGGER t4r1 AFTER INSERT on t4 WHEN nosuchcol BEGIN
SELECT 'illegal WHEN clause';
END;
}
} {}
do_test insert3-3.4 {
catchsql {
INSERT INTO t4 VALUES(1,2,3)
}
} {1 {no such column: nosuchcol}}
} ;# ifcapable {trigger}
# Tests for the INSERT INTO ... DEFAULT VALUES construct
#
do_test insert3-3.5 {
execsql {
CREATE TABLE t5(
a INTEGER PRIMARY KEY,
b DEFAULT 'xyz'
);
INSERT INTO t5 DEFAULT VALUES;
SELECT * FROM t5;
}
} {1 xyz}
do_test insert3-3.6 {
execsql {
INSERT INTO t5 DEFAULT VALUES;
SELECT * FROM t5;
}
} {1 xyz 2 xyz}
ifcapable bloblit {
do_test insert3-3.7 {
execsql {
CREATE TABLE t6(x,y DEFAULT 4.3, z DEFAULT x'6869');
INSERT INTO t6 DEFAULT VALUES;
SELECT * FROM t6;
}
} {{} 4.3 hi}
}
foreach tab [db eval {SELECT name FROM sqlite_master WHERE type = 'table'}] {
db eval "DROP TABLE $tab"
}
db close
sqlite3 db test.db
#-------------------------------------------------------------------------
# While developing tests for a different feature (savepoint) the following
# sequence was found to cause an assert() in btree.c to fail. These
# tests are included to ensure that that bug is fixed.
#
do_test insert3-4.1 {
execsql {
CREATE TABLE t1(a, b, c);
CREATE INDEX i1 ON t1(a, b);
BEGIN;
INSERT INTO t1 VALUES(randstr(10,400),randstr(10,400),randstr(10,400));
}
set r "randstr(10,400)"
for {set ii 0} {$ii < 10} {incr ii} {
execsql "INSERT INTO t1 SELECT $r, $r, $r FROM t1"
}
execsql { COMMIT }
} {}
do_test insert3-4.2 {
execsql {
PRAGMA cache_size = 10;
BEGIN;
UPDATE t1 SET a = randstr(10,10) WHERE (rowid%4)==0;
DELETE FROM t1 WHERE rowid%2;
INSERT INTO t1 SELECT randstr(10,400), randstr(10,400), c FROM t1;
COMMIT;
}
} {}
finish_test
|