blob: 24c6bb45af0d8aac7c0817be4ce65346599eeaa7 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001# Copyright (c) 1999, 2000 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA 02111-1307, USA.
17#
18# Author contact information:
19# drh@hwaci.com
20# http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file implements regression tests for SQLite library. The
24# focus of this file is testing the CREATE TABLE statement.
25#
drh1b6a71f2000-05-29 23:58:11 +000026# $Id: table.test,v 1.1 2000/05/29 23:58:12 drh Exp $
drh348784e2000-05-29 20:41:49 +000027
28set testdir [file dirname $argv0]
29source $testdir/tester.tcl
30
31# Create a basic table and verify it is added to sqlite_master
32#
drh1b6a71f2000-05-29 23:58:11 +000033do_test table-1.1 {
drh348784e2000-05-29 20:41:49 +000034 execsql {
35 CREATE TABLE test1 (
36 one varchar(10),
37 two text
38 )
39 }
40 execsql {
41 SELECT sql FROM sqlite_master
42 }
43} {{CREATE TABLE test1 (
44 one varchar(10),
45 two text
46 )}}
47
48# Verify that both table files exists in the database directory
49#
drh1b6a71f2000-05-29 23:58:11 +000050do_test table-1.2 {
drh348784e2000-05-29 20:41:49 +000051 execsql {INSERT INTO test1 VALUES('hi', 'y''all')}
52 lsort [glob -nocomplain testdb/*.tbl]
53} {testdb/sqlite_master.tbl testdb/test1.tbl}
54
55# Verify the other fields of the sqlite_master file.
56#
drh1b6a71f2000-05-29 23:58:11 +000057do_test table-1.3 {
drh348784e2000-05-29 20:41:49 +000058 execsql {SELECT name, tbl_name, type FROM sqlite_master}
59} {test1 test1 table}
60
61# Close and reopen the database. Verify that everything is
62# still the same.
63#
drh1b6a71f2000-05-29 23:58:11 +000064do_test table-1.4 {
drh348784e2000-05-29 20:41:49 +000065 db close
66 sqlite db testdb
67 execsql {SELECT name, tbl_name, type from sqlite_master}
68} {test1 test1 table}
69
70# Drop the database and make sure it disappears.
71#
drh1b6a71f2000-05-29 23:58:11 +000072do_test table-1.5 {
drh348784e2000-05-29 20:41:49 +000073 execsql {DROP TABLE test1}
74 execsql {SELECT * FROM sqlite_master}
75} {}
76
77# Verify that the file associated with the database is gone.
78#
drh1b6a71f2000-05-29 23:58:11 +000079do_test table-1.5 {
drh348784e2000-05-29 20:41:49 +000080 lsort [glob -nocomplain testdb/*.tbl]
81} {testdb/sqlite_master.tbl}
82
83# Close and reopen the database. Verify that the table is
84# still gone.
85#
drh1b6a71f2000-05-29 23:58:11 +000086do_test table-1.6 {
drh348784e2000-05-29 20:41:49 +000087 db close
88 sqlite db testdb
89 execsql {SELECT name FROM sqlite_master}
90} {}
91
drhb24fcbe2000-05-29 23:30:50 +000092
93# Verify that we cannot make two tables with the same name
94#
drh1b6a71f2000-05-29 23:58:11 +000095do_test table-2.1 {
drhb24fcbe2000-05-29 23:30:50 +000096 execsql {CREATE TABLE test2(one text)}
97 set v [catch {execsql {CREATE TABLE test2(two text)}} msg]
98 lappend v $msg
99} {1 {table "test2" already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000100do_test table-2.1b {
drhb24fcbe2000-05-29 23:30:50 +0000101 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
102 lappend v $msg
103} {1 {table "sqlite_master" already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000104do_test table-2.1c {
drhb24fcbe2000-05-29 23:30:50 +0000105 db close
106 sqlite db testdb
107 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
108 lappend v $msg
109} {1 {table "sqlite_master" already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000110do_test table-2.1d {
drhb24fcbe2000-05-29 23:30:50 +0000111 execsql {DROP TABLE test2; SELECT name FROM sqlite_master}
112} {}
113
114# Verify that we cannot make a table with the same name as an index
115#
drh1b6a71f2000-05-29 23:58:11 +0000116do_test table-2.2 {
drhb24fcbe2000-05-29 23:30:50 +0000117 execsql {CREATE TABLE test2(one text); CREATE INDEX test3 ON test2(one)}
118 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
119 lappend v $msg
120} {1 {there is already an index named "test3"}}
drh1b6a71f2000-05-29 23:58:11 +0000121do_test table-2.2b {
drhb24fcbe2000-05-29 23:30:50 +0000122 db close
123 sqlite db testdb
124 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
125 lappend v $msg
126} {1 {there is already an index named "test3"}}
drh1b6a71f2000-05-29 23:58:11 +0000127do_test table-2.2c {
drhb24fcbe2000-05-29 23:30:50 +0000128 execsql {DROP INDEX test3}
129 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
130 lappend v $msg
131} {0 {}}
drh1b6a71f2000-05-29 23:58:11 +0000132do_test table-2.2d {
drhb24fcbe2000-05-29 23:30:50 +0000133 execsql {SELECT name FROM sqlite_master ORDER BY name}
134} {test2 test3}
drh1b6a71f2000-05-29 23:58:11 +0000135do_test table-2.2e {
drhb24fcbe2000-05-29 23:30:50 +0000136 execsql {DROP TABLE test2; DROP TABLE test3}
137 execsql {SELECT name FROM sqlite_master ORDER BY name}
138} {}
139
140# Create a table with many field names
141#
142set big_table \
143{CREATE TABLE big(
144 f1 varchar(20),
145 f2 char(10),
146 f3 varchar(30),
147 f4 text,
148 f5 text,
149 f6 text,
150 f7 text,
151 f8 text,
152 f9 text,
153 f10 text,
154 f11 text,
155 f12 text,
156 f13 text,
157 f14 text,
158 f15 text,
159 f16 text,
160 f17 text,
161 f18 text,
162 f19 text,
163 f20 text
164)}
drh1b6a71f2000-05-29 23:58:11 +0000165do_test table-3.1 {
drhb24fcbe2000-05-29 23:30:50 +0000166 execsql $big_table
167 execsql {SELECT sql FROM sqlite_master}
168} \{$big_table\}
drh1b6a71f2000-05-29 23:58:11 +0000169do_test table-3.2 {
drhb24fcbe2000-05-29 23:30:50 +0000170 set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
171 lappend v $msg
172} {1 {table "BIG" already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000173do_test table-3.3 {
drhb24fcbe2000-05-29 23:30:50 +0000174 set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
175 lappend v $msg
176} {1 {table "biG" already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000177do_test table-3.4 {
drhb24fcbe2000-05-29 23:30:50 +0000178 set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
179 lappend v $msg
180} {1 {table "bIg" already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000181do_test table-3.5 {
drhb24fcbe2000-05-29 23:30:50 +0000182 db close
183 sqlite db testdb
184 set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
185 lappend v $msg
186} {1 {table "Big" already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000187do_test table-3.6 {
drhb24fcbe2000-05-29 23:30:50 +0000188 execsql {DROP TABLE big}
189 execsql {SELECT name FROM sqlite_master}
190} {}
191
192# Try creating large numbers of tables
193#
194set r {}
195for {set i 1} {$i<=100} {incr i} {
196 lappend r test$i
197}
drh1b6a71f2000-05-29 23:58:11 +0000198do_test table-4.1 {
drhb24fcbe2000-05-29 23:30:50 +0000199 for {set i 1} {$i<=100} {incr i} {
200 set sql "CREATE TABLE test$i ("
201 for {set k 1} {$k<$i} {incr k} {
202 append sql "field$k text,"
203 }
204 append sql "last_field text)"
205 execsql $sql
206 }
207 execsql {SELECT name FROM sqlite_master ORDER BY name}
208} $r
drh1b6a71f2000-05-29 23:58:11 +0000209do_test table-4.1b {
drhb24fcbe2000-05-29 23:30:50 +0000210 db close
211 sqlite db testdb
212 execsql {SELECT name FROM sqlite_master ORDER BY name}
213} $r
214
215# Drop the even number tables
216#
217set r {}
218for {set i 1} {$i<=100} {incr i 2} {
219 lappend r test$i
220}
drh1b6a71f2000-05-29 23:58:11 +0000221do_test table-4.2 {
drhb24fcbe2000-05-29 23:30:50 +0000222 for {set i 2} {$i<=100} {incr i 2} {
223 set sql "DROP TABLE TEST$i"
224 execsql $sql
225 }
226 execsql {SELECT name FROM sqlite_master ORDER BY name}
227} $r
228
229# Drop the odd number tables
230#
drh1b6a71f2000-05-29 23:58:11 +0000231do_test table-4.3 {
drhb24fcbe2000-05-29 23:30:50 +0000232 for {set i 1} {$i<=100} {incr i 2} {
233 set sql "DROP TABLE test$i"
234 execsql $sql
235 }
236 execsql {SELECT name FROM sqlite_master ORDER BY name}
237} {}
238
drh1b6a71f2000-05-29 23:58:11 +0000239# Try to drop a table that does not exist
240#
241do_test table-5.1 {
242 set v [catch {execsql {DROP TABLE test9}} msg]
243 lappend v $msg
244} {1 {no such table: test9}}
245
246# Try to drop sqlite_master
247#
248do_test table-5.2 {
249 set v [catch {execsql {DROP TABLE sqlite_master}} msg]
250 lappend v $msg
251} {1 {table "sqlite_master" may not be dropped}}
252
253# Make sure an EXPLAIN does not really create a new table
254#
255do_test table-5.3 {
256 execsql {EXPLAIN CREATE TABLE test1(f1 int)}
257 execsql {SELECT name FROM sqlite_master}
258} {}
259
260# Make sure an EXPLAIN does not really drop an existing table
261#
262do_test table-5.4 {
263 execsql {CREATE TABLE test1(f1 int)}
264 execsql {EXPLAIN DROP TABLE test1}
265 execsql {SELECT name FROM sqlite_master}
266} {test1}
267
drh348784e2000-05-29 20:41:49 +0000268finish_test