blob: 24580ca34533f8e49761a6019759ba7942e65f8b [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh62c68192000-05-30 00:51:26 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh62c68192000-05-30 00:51:26 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh62c68192000-05-30 00:51:26 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the INSERT statement.
13#
drhe497f002004-11-07 13:01:49 +000014# $Id: insert.test,v 1.20 2004/11/07 13:01:50 drh Exp $
drh62c68192000-05-30 00:51:26 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Try to insert into a non-existant table.
20#
21do_test insert-1.1 {
22 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
23 lappend v $msg
drh1d37e282000-05-30 03:12:21 +000024} {1 {no such table: test1}}
drh62c68192000-05-30 00:51:26 +000025
26# Try to insert into sqlite_master
27#
28do_test insert-1.2 {
29 set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
30 lappend v $msg
drh1d37e282000-05-30 03:12:21 +000031} {1 {table sqlite_master may not be modified}}
drh62c68192000-05-30 00:51:26 +000032
33# Try to insert the wrong number of entries.
34#
35do_test insert-1.3 {
36 execsql {CREATE TABLE test1(one int, two int, three int)}
37 set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
38 lappend v $msg
39} {1 {table test1 has 3 columns but 2 values were supplied}}
40do_test insert-1.3b {
41 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
42 lappend v $msg
43} {1 {table test1 has 3 columns but 4 values were supplied}}
44do_test insert-1.3c {
45 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
46 lappend v $msg
47} {1 {4 values for 2 columns}}
48do_test insert-1.3d {
49 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
50 lappend v $msg
51} {1 {1 values for 2 columns}}
52
53# Try to insert into a non-existant column of a table.
54#
55do_test insert-1.4 {
56 set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
57 lappend v $msg
58} {1 {table test1 has no column named four}}
59
60# Make sure the inserts actually happen
61#
62do_test insert-1.5 {
63 execsql {INSERT INTO test1 VALUES(1,2,3)}
64 execsql {SELECT * FROM test1}
65} {1 2 3}
66do_test insert-1.5b {
67 execsql {INSERT INTO test1 VALUES(4,5,6)}
68 execsql {SELECT * FROM test1 ORDER BY one}
69} {1 2 3 4 5 6}
70do_test insert-1.5c {
71 execsql {INSERT INTO test1 VALUES(7,8,9)}
72 execsql {SELECT * FROM test1 ORDER BY one}
73} {1 2 3 4 5 6 7 8 9}
74
75do_test insert-1.6 {
76 execsql {DELETE FROM test1}
77 execsql {INSERT INTO test1(one,two) VALUES(1,2)}
78 execsql {SELECT * FROM test1 ORDER BY one}
79} {1 2 {}}
80do_test insert-1.6b {
81 execsql {INSERT INTO test1(two,three) VALUES(5,6)}
82 execsql {SELECT * FROM test1 ORDER BY one}
83} {{} 5 6 1 2 {}}
84do_test insert-1.6c {
85 execsql {INSERT INTO test1(three,one) VALUES(7,8)}
86 execsql {SELECT * FROM test1 ORDER BY one}
87} {{} 5 6 1 2 {} 8 {} 7}
88
drh7020f652000-06-03 18:06:52 +000089# A table to use for testing default values
90#
drh7020f652000-06-03 18:06:52 +000091do_test insert-2.1 {
drhc4a3c772001-04-04 11:48:57 +000092 execsql {
93 CREATE TABLE test2(
94 f1 int default -111,
95 f2 real default +4.32,
96 f3 int default +222,
97 f4 int default 7.89
98 )
99 }
drh7020f652000-06-03 18:06:52 +0000100 execsql {SELECT * from test2}
101} {}
102do_test insert-2.2 {
drhc4a3c772001-04-04 11:48:57 +0000103 execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
104 execsql {SELECT * FROM test2}
105} {10 4.32 -10 7.89}
106do_test insert-2.3 {
107 execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
108 execsql {SELECT * FROM test2 WHERE f1==-111}
109} {-111 1.23 222 -3.45}
110do_test insert-2.4 {
111 execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
112 execsql {SELECT * FROM test2 WHERE f1==77}
113} {77 1.23 222 3.45}
114do_test insert-2.10 {
115 execsql {
116 DROP TABLE test2;
117 CREATE TABLE test2(
118 f1 int default 111,
119 f2 real default -4.32,
120 f3 text default hi,
121 f4 text default 'abc-123',
122 f5 varchar(10)
123 )
124 }
125 execsql {SELECT * from test2}
126} {}
127do_test insert-2.11 {
drh7020f652000-06-03 18:06:52 +0000128 execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
129 execsql {SELECT * FROM test2}
130} {111 -2.22 hi hi! {}}
drhc4a3c772001-04-04 11:48:57 +0000131do_test insert-2.12 {
drh7020f652000-06-03 18:06:52 +0000132 execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
133 execsql {SELECT * FROM test2 ORDER BY f1}
134} {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
135
136# Do additional inserts with default values, but this time
137# on a table that has indices. In particular we want to verify
138# that the correct default values are inserted into the indices.
139#
drh7020f652000-06-03 18:06:52 +0000140do_test insert-3.1 {
drh5974a302000-06-07 14:42:26 +0000141 execsql {
142 DELETE FROM test2;
143 CREATE INDEX index9 ON test2(f1,f2);
144 CREATE INDEX indext ON test2(f4,f5);
145 SELECT * from test2;
146 }
drh7020f652000-06-03 18:06:52 +0000147} {}
danielk1977452c9892004-05-13 05:16:15 +0000148
drhef4ac8f2004-06-19 00:16:31 +0000149# Update for sqlite3 v3:
danielk1977452c9892004-05-13 05:16:15 +0000150# Change the 111 to '111' in the following two test cases, because
151# the default value is being inserted as a string. TODO: It shouldn't be.
drh7020f652000-06-03 18:06:52 +0000152do_test insert-3.2 {
153 execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
danielk1977452c9892004-05-13 05:16:15 +0000154 execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33}
drh7020f652000-06-03 18:06:52 +0000155} {111 -3.33 hi hum {}}
156do_test insert-3.3 {
157 execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
danielk1977452c9892004-05-13 05:16:15 +0000158 execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33}
drh7020f652000-06-03 18:06:52 +0000159} {111 -3.33 hi hum {}}
160do_test insert-3.4 {
161 execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
162} {22 -4.44 hi abc-123 wham}
drhe497f002004-11-07 13:01:49 +0000163ifcapable {reindex} {
164 do_test insert-3.5 {
165 execsql REINDEX
166 } {}
167}
drh21504322002-06-25 13:16:02 +0000168integrity_check insert-3.5
drh24e97df2002-02-03 19:06:02 +0000169
drhadbe3532002-04-12 13:11:52 +0000170# Test of expressions in the VALUES clause
171#
drhe64e7b22002-02-18 13:56:36 +0000172do_test insert-4.1 {
173 execsql {
174 CREATE TABLE t3(a,b,c);
175 INSERT INTO t3 VALUES(1+2+3,4,5);
176 SELECT * FROM t3;
177 }
178} {6 4 5}
179do_test insert-4.2 {
180 execsql {
181 INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);
182 SELECT * FROM t3 ORDER BY a;
183 }
184} {6 4 5 7 5 6}
185do_test insert-4.3 {
186 catchsql {
187 INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
188 SELECT * FROM t3 ORDER BY a;
189 }
190} {1 {no such column: t3.a}}
191do_test insert-4.4 {
192 execsql {
193 INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);
194 SELECT * FROM t3 ORDER BY a;
195 }
196} {{} 6 7 6 4 5 7 5 6}
197do_test insert-4.5 {
198 execsql {
199 SELECT b,c FROM t3 WHERE a IS NULL;
200 }
201} {6 7}
drhadbe3532002-04-12 13:11:52 +0000202do_test insert-4.6 {
203 catchsql {
204 INSERT INTO t3 VALUES(notafunc(2,3),2,3);
205 }
206} {1 {no such function: notafunc}}
207do_test insert-4.7 {
208 execsql {
209 INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
210 SELECT * FROM t3 WHERE c=99;
211 }
212} {1 3 99}
213
drh048c5302003-04-03 01:50:44 +0000214# Test the ability to insert from a temporary table into itself.
215# Ticket #275.
216#
217do_test insert-5.1 {
218 execsql {
219 CREATE TEMP TABLE t4(x);
220 INSERT INTO t4 VALUES(1);
221 SELECT * FROM t4;
222 }
223} {1}
224do_test insert-5.2 {
225 execsql {
226 INSERT INTO t4 SELECT x+1 FROM t4;
227 SELECT * FROM t4;
228 }
229} {1 2}
drh6bf89572004-11-03 16:27:01 +0000230ifcapable {explain} {
231 do_test insert-5.3 {
232 # verify that a temporary table is used to copy t4 to t4
233 set x [execsql {
234 EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
235 }]
236 expr {[lsearch $x OpenTemp]>0}
237 } {1}
238}
danielk1977bf57cfe2004-05-11 09:50:02 +0000239
drh048c5302003-04-03 01:50:44 +0000240do_test insert-5.4 {
241 # Verify that table "test1" begins on page 3. This should be the same
242 # page number used by "t4" above.
danielk1977bf57cfe2004-05-11 09:50:02 +0000243 #
244 # Update for v3 - the first table now begins on page 2 of each file, not 3.
drh048c5302003-04-03 01:50:44 +0000245 execsql {
246 SELECT rootpage FROM sqlite_master WHERE name='test1';
247 }
danielk1977bf57cfe2004-05-11 09:50:02 +0000248} {2}
drh048c5302003-04-03 01:50:44 +0000249do_test insert-5.5 {
250 # Verify that "t4" begins on page 3.
danielk1977bf57cfe2004-05-11 09:50:02 +0000251 #
252 # Update for v3 - the first table now begins on page 2 of each file, not 3.
drh048c5302003-04-03 01:50:44 +0000253 execsql {
254 SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
255 }
danielk1977bf57cfe2004-05-11 09:50:02 +0000256} {2}
drh048c5302003-04-03 01:50:44 +0000257do_test insert-5.6 {
258 # This should not use an intermediate temporary table.
259 execsql {
260 INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
261 SELECT * FROM t4
262 }
263} {1 2 8}
drh6bf89572004-11-03 16:27:01 +0000264ifcapable {explain} {
265 do_test insert-5.7 {
266 # verify that no temporary table is used to copy test1 to t4
267 set x [execsql {
268 EXPLAIN INSERT INTO t4 SELECT one FROM test1;
269 }]
270 expr {[lsearch $x OpenTemp]>0}
271 } {0}
272}
drh048c5302003-04-03 01:50:44 +0000273
drh5383ae52003-06-04 12:23:30 +0000274# Ticket #334: REPLACE statement corrupting indices.
275#
276do_test insert-6.1 {
277 execsql {
278 CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
279 INSERT INTO t1 VALUES(1,2);
280 INSERT INTO t1 VALUES(2,3);
281 SELECT b FROM t1 WHERE b=2;
282 }
283} {2}
284do_test insert-6.2 {
285 execsql {
286 REPLACE INTO t1 VALUES(1,4);
287 SELECT b FROM t1 WHERE b=2;
288 }
289} {}
drh7d02cb72003-06-04 16:24:39 +0000290do_test insert-6.3 {
291 execsql {
292 UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
293 SELECT * FROM t1 WHERE b=4;
294 }
295} {2 4}
296do_test insert-6.4 {
297 execsql {
298 SELECT * FROM t1 WHERE b=3;
299 }
300} {}
drhe497f002004-11-07 13:01:49 +0000301ifcapable {reindex} {
302 do_test insert-6.7 {
303 execsql REINDEX
304 } {}
305}
drh7020f652000-06-03 18:06:52 +0000306
drhed717fe2003-06-15 23:42:24 +0000307integrity_check insert-99.0
308
drh62c68192000-05-30 00:51:26 +0000309finish_test