blob: 6ab2d3fec42938ad3382e00f3321fb0c12df4974 [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 DELETE FROM statement.
13#
drhb19a2bc2001-09-16 00:13:26 +000014# $Id: delete.test,v 1.9 2001/09/16 00:13:28 drh Exp $
drh62c68192000-05-30 00:51:26 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Try to delete from a non-existant table.
20#
21do_test delete-1.1 {
22 set v [catch {execsql {DELETE FROM test1}} msg]
23 lappend v $msg
24} {1 {no such table: test1}}
25
26# Try to delete from sqlite_master
27#
28do_test delete-2.1 {
29 set v [catch {execsql {DELETE FROM sqlite_master}} 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
drh7020f652000-06-03 18:06:52 +000033# Delete selected entries from a table with and without an index.
34#
35do_test delete-3.1a {
36 execsql {CREATE TABLE table1(f1 int, f2 int)}
37 execsql {INSERT INTO table1 VALUES(1,2)}
38 execsql {INSERT INTO table1 VALUES(2,4)}
39 execsql {INSERT INTO table1 VALUES(3,8)}
40 execsql {INSERT INTO table1 VALUES(4,16)}
41 execsql {SELECT * FROM table1 ORDER BY f1}
42} {1 2 2 4 3 8 4 16}
43do_test delete-3.1b {
44 execsql {DELETE FROM table1 WHERE f1=3}
45 execsql {SELECT * FROM table1 ORDER BY f1}
46} {1 2 2 4 4 16}
47do_test delete-3.1c {
48 execsql {CREATE INDEX index1 ON table1(f1)}
drh4cfa7932000-06-08 15:10:46 +000049 execsql {DELETE FROM 'table1' WHERE f1=3}
drh7020f652000-06-03 18:06:52 +000050 execsql {SELECT * FROM table1 ORDER BY f1}
51} {1 2 2 4 4 16}
52do_test delete-3.1d {
53 execsql {DELETE FROM table1 WHERE f1=2}
54 execsql {SELECT * FROM table1 ORDER BY f1}
55} {1 2 4 16}
drh62c68192000-05-30 00:51:26 +000056
drh8be51132000-06-03 19:19:41 +000057# Semantic errors in the WHERE clause
58#
59do_test delete-4.1 {
60 execsql {CREATE TABLE table2(f1 int, f2 int)}
61 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
62 lappend v $msg
drh967e8b72000-06-21 13:59:10 +000063} {1 {no such column: f3}}
drh8be51132000-06-03 19:19:41 +000064
65do_test delete-4.2 {
66 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
67 lappend v $msg
68} {1 {no such function: xyzzy}}
drh62c68192000-05-30 00:51:26 +000069
drh3494ffe2001-03-20 12:55:13 +000070# Lots of deletes
71#
72do_test delete-5.1 {
73 execsql {DELETE FROM table1}
74 execsql {SELECT count(*) FROM table1}
75} {}
76do_test delete-5.2 {
77 for {set i 1} {$i<=200} {incr i} {
78 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
79 }
80 execsql {SELECT count(*) FROM table1}
81} {200}
82do_test delete-5.3 {
83 for {set i 1} {$i<=200} {incr i 4} {
84 execsql "DELETE FROM table1 WHERE f1==$i"
85 }
86 execsql {SELECT count(*) FROM table1}
87} {150}
88do_test delete-5.4 {
89 execsql "DELETE FROM table1 WHERE f1>50"
90 execsql {SELECT count(*) FROM table1}
91} {37}
92do_test delete-5.5 {
93 for {set i 1} {$i<=70} {incr i 3} {
94 execsql "DELETE FROM table1 WHERE f1==$i"
95 }
96 execsql {SELECT f1 FROM table1 ORDER BY f1}
97} {2 3 6 8 11 12 14 15 18 20 23 24 26 27 30 32 35 36 38 39 42 44 47 48 50}
98do_test delete-5.6 {
99 for {set i 1} {$i<40} {incr i} {
100 execsql "DELETE FROM table1 WHERE f1==$i"
101 }
102 execsql {SELECT f1 FROM table1 ORDER BY f1}
103} {42 44 47 48 50}
104do_test delete-5.7 {
105 execsql "DELETE FROM table1 WHERE f1!=48"
106 execsql {SELECT f1 FROM table1 ORDER BY f1}
107} {48}
108
drh0353ced2001-03-20 22:05:00 +0000109# Delete large quantities of data. We want to test the List overflow
110# mechanism in the vdbe.
111#
112do_test delete-6.1 {
113 set fd [open data1.txt w]
114 for {set i 1} {$i<=3000} {incr i} {
115 puts $fd "[expr {$i}]\t[expr {$i*$i}]"
116 }
117 close $fd
118 execsql {DELETE FROM table1}
119 execsql {COPY table1 FROM 'data1.txt'}
120 execsql {DELETE FROM table2}
121 execsql {COPY table2 FROM 'data1.txt'}
122 file delete data1.txt
123 execsql {SELECT count(*) FROM table1}
124} {3000}
125do_test delete-6.2 {
126 execsql {SELECT count(*) FROM table2}
127} {3000}
128do_test delete-6.3 {
129 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
130} {1 2 3 4 5 6 7 8 9}
131do_test delete-6.4 {
132 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
133} {1 2 3 4 5 6 7 8 9}
134do_test delete-6.5 {
135 execsql {DELETE FROM table1 WHERE f1>7}
136 execsql {SELECT f1 FROM table1 ORDER BY f1}
137} {1 2 3 4 5 6 7}
138do_test delete-6.6 {
139 execsql {DELETE FROM table2 WHERE f1>7}
140 execsql {SELECT f1 FROM table2 ORDER BY f1}
141} {1 2 3 4 5 6 7}
142do_test delete-6.7 {
143 execsql {DELETE FROM table1}
144 execsql {SELECT f1 FROM table1}
145} {}
146do_test delete-6.8 {
147 execsql {INSERT INTO table1 VALUES(2,3)}
148 execsql {SELECT f1 FROM table1}
149} {2}
150do_test delete-6.9 {
151 execsql {DELETE FROM table2}
152 execsql {SELECT f1 FROM table2}
153} {}
154do_test delete-6.10 {
155 execsql {INSERT INTO table2 VALUES(2,3)}
156 execsql {SELECT f1 FROM table2}
157} {2}
158
159
160
drh62c68192000-05-30 00:51:26 +0000161finish_test