drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1 | # 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 | # |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame^] | 26 | # $Id: table.test,v 1.4 2000/05/30 16:27:05 drh Exp $ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 27 | |
| 28 | set testdir [file dirname $argv0] |
| 29 | source $testdir/tester.tcl |
| 30 | |
| 31 | # Create a basic table and verify it is added to sqlite_master |
| 32 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 33 | do_test table-1.1 { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 34 | 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 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 50 | do_test table-1.2 { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 51 | 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 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 57 | do_test table-1.3 { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 58 | 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 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 64 | do_test table-1.4 { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 65 | 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 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 72 | do_test table-1.5 { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 73 | execsql {DROP TABLE test1} |
| 74 | execsql {SELECT * FROM sqlite_master} |
| 75 | } {} |
| 76 | |
| 77 | # Verify that the file associated with the database is gone. |
| 78 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 79 | do_test table-1.5 { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 80 | 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 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 86 | do_test table-1.6 { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 87 | db close |
| 88 | sqlite db testdb |
| 89 | execsql {SELECT name FROM sqlite_master} |
| 90 | } {} |
| 91 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame^] | 92 | # Repeat the above steps, but this time quote the table name. |
| 93 | # |
| 94 | do_test table-1.10 { |
| 95 | execsql {CREATE TABLE "create" (f1 int)} |
| 96 | execsql {SELECT name FROM sqlite_master} |
| 97 | } {create} |
| 98 | do_test table-1.11 { |
| 99 | execsql {DROP TABLE "create"} |
| 100 | execsql {SELECT name FROM "sqlite_master"} |
| 101 | } {} |
| 102 | do_test table-1.12 { |
| 103 | execsql {CREATE TABLE test1("f1 ho" int)} |
| 104 | execsql {SELECT name as "X" FROM sqlite_master} |
| 105 | } {test1} |
| 106 | do_test table-1.13 { |
| 107 | execsql {DROP TABLE "TEST1"} |
| 108 | execsql {SELECT name FROM "sqlite_master"} |
| 109 | } {} |
| 110 | |
| 111 | |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 112 | |
| 113 | # Verify that we cannot make two tables with the same name |
| 114 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 115 | do_test table-2.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 116 | execsql {CREATE TABLE test2(one text)} |
| 117 | set v [catch {execsql {CREATE TABLE test2(two text)}} msg] |
| 118 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 119 | } {1 {table test2 already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 120 | do_test table-2.1b { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 121 | set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg] |
| 122 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 123 | } {1 {table sqlite_master already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 124 | do_test table-2.1c { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 125 | db close |
| 126 | sqlite db testdb |
| 127 | set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg] |
| 128 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 129 | } {1 {table sqlite_master already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 130 | do_test table-2.1d { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 131 | execsql {DROP TABLE test2; SELECT name FROM sqlite_master} |
| 132 | } {} |
| 133 | |
| 134 | # Verify that we cannot make a table with the same name as an index |
| 135 | # |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 136 | do_test table-2.2a { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 137 | execsql {CREATE TABLE test2(one text); CREATE INDEX test3 ON test2(one)} |
| 138 | set v [catch {execsql {CREATE TABLE test3(two text)}} msg] |
| 139 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 140 | } {1 {there is already an index named test3}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 141 | do_test table-2.2b { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 142 | db close |
| 143 | sqlite db testdb |
| 144 | set v [catch {execsql {CREATE TABLE test3(two text)}} msg] |
| 145 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 146 | } {1 {there is already an index named test3}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 147 | do_test table-2.2c { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 148 | execsql {DROP INDEX test3} |
| 149 | set v [catch {execsql {CREATE TABLE test3(two text)}} msg] |
| 150 | lappend v $msg |
| 151 | } {0 {}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 152 | do_test table-2.2d { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 153 | execsql {SELECT name FROM sqlite_master ORDER BY name} |
| 154 | } {test2 test3} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 155 | do_test table-2.2e { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 156 | execsql {DROP TABLE test2; DROP TABLE test3} |
| 157 | execsql {SELECT name FROM sqlite_master ORDER BY name} |
| 158 | } {} |
| 159 | |
| 160 | # Create a table with many field names |
| 161 | # |
| 162 | set big_table \ |
| 163 | {CREATE TABLE big( |
| 164 | f1 varchar(20), |
| 165 | f2 char(10), |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 166 | f3 varchar(30) primary key, |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 167 | f4 text, |
| 168 | f5 text, |
| 169 | f6 text, |
| 170 | f7 text, |
| 171 | f8 text, |
| 172 | f9 text, |
| 173 | f10 text, |
| 174 | f11 text, |
| 175 | f12 text, |
| 176 | f13 text, |
| 177 | f14 text, |
| 178 | f15 text, |
| 179 | f16 text, |
| 180 | f17 text, |
| 181 | f18 text, |
| 182 | f19 text, |
| 183 | f20 text |
| 184 | )} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 185 | do_test table-3.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 186 | execsql $big_table |
| 187 | execsql {SELECT sql FROM sqlite_master} |
| 188 | } \{$big_table\} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 189 | do_test table-3.2 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 190 | set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg] |
| 191 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 192 | } {1 {table BIG already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 193 | do_test table-3.3 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 194 | set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg] |
| 195 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 196 | } {1 {table biG already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 197 | do_test table-3.4 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 198 | set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg] |
| 199 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 200 | } {1 {table bIg already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 201 | do_test table-3.5 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 202 | db close |
| 203 | sqlite db testdb |
| 204 | set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg] |
| 205 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 206 | } {1 {table Big already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 207 | do_test table-3.6 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 208 | execsql {DROP TABLE big} |
| 209 | execsql {SELECT name FROM sqlite_master} |
| 210 | } {} |
| 211 | |
| 212 | # Try creating large numbers of tables |
| 213 | # |
| 214 | set r {} |
| 215 | for {set i 1} {$i<=100} {incr i} { |
| 216 | lappend r test$i |
| 217 | } |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 218 | do_test table-4.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 219 | for {set i 1} {$i<=100} {incr i} { |
| 220 | set sql "CREATE TABLE test$i (" |
| 221 | for {set k 1} {$k<$i} {incr k} { |
| 222 | append sql "field$k text," |
| 223 | } |
| 224 | append sql "last_field text)" |
| 225 | execsql $sql |
| 226 | } |
| 227 | execsql {SELECT name FROM sqlite_master ORDER BY name} |
| 228 | } $r |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 229 | do_test table-4.1b { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 230 | db close |
| 231 | sqlite db testdb |
| 232 | execsql {SELECT name FROM sqlite_master ORDER BY name} |
| 233 | } $r |
| 234 | |
| 235 | # Drop the even number tables |
| 236 | # |
| 237 | set r {} |
| 238 | for {set i 1} {$i<=100} {incr i 2} { |
| 239 | lappend r test$i |
| 240 | } |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 241 | do_test table-4.2 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 242 | for {set i 2} {$i<=100} {incr i 2} { |
| 243 | set sql "DROP TABLE TEST$i" |
| 244 | execsql $sql |
| 245 | } |
| 246 | execsql {SELECT name FROM sqlite_master ORDER BY name} |
| 247 | } $r |
| 248 | |
| 249 | # Drop the odd number tables |
| 250 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 251 | do_test table-4.3 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 252 | for {set i 1} {$i<=100} {incr i 2} { |
| 253 | set sql "DROP TABLE test$i" |
| 254 | execsql $sql |
| 255 | } |
| 256 | execsql {SELECT name FROM sqlite_master ORDER BY name} |
| 257 | } {} |
| 258 | |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 259 | # Try to drop a table that does not exist |
| 260 | # |
| 261 | do_test table-5.1 { |
| 262 | set v [catch {execsql {DROP TABLE test9}} msg] |
| 263 | lappend v $msg |
| 264 | } {1 {no such table: test9}} |
| 265 | |
| 266 | # Try to drop sqlite_master |
| 267 | # |
| 268 | do_test table-5.2 { |
| 269 | set v [catch {execsql {DROP TABLE sqlite_master}} msg] |
| 270 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 271 | } {1 {table sqlite_master may not be dropped}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 272 | |
| 273 | # Make sure an EXPLAIN does not really create a new table |
| 274 | # |
| 275 | do_test table-5.3 { |
| 276 | execsql {EXPLAIN CREATE TABLE test1(f1 int)} |
| 277 | execsql {SELECT name FROM sqlite_master} |
| 278 | } {} |
| 279 | |
| 280 | # Make sure an EXPLAIN does not really drop an existing table |
| 281 | # |
| 282 | do_test table-5.4 { |
| 283 | execsql {CREATE TABLE test1(f1 int)} |
| 284 | execsql {EXPLAIN DROP TABLE test1} |
| 285 | execsql {SELECT name FROM sqlite_master} |
| 286 | } {test1} |
| 287 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 288 | finish_test |