blob: 1d90de1b9bbc3cec0ce6d18e19ee60de5f95f5b9 [file] [log] [blame]
drhedb04ed2015-09-04 12:54:01 +00001# 2005-02-14
drhaee128d2005-02-14 20:48:18 +00002#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# 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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the CREATE INDEX statement.
13#
drhaee128d2005-02-14 20:48:18 +000014
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Ticket #1115. Make sure that when a UNIQUE index is created on a
20# non-unique column (or columns) that it fails and that it leaves no
21# residue behind.
22#
23do_test index3-1.1 {
24 execsql {
25 CREATE TABLE t1(a);
26 INSERT INTO t1 VALUES(1);
27 INSERT INTO t1 VALUES(1);
28 SELECT * FROM t1;
29 }
30} {1 1}
31do_test index3-1.2 {
32 catchsql {
33 BEGIN;
34 CREATE UNIQUE INDEX i1 ON t1(a);
35 }
drhf9c8ce32013-11-05 13:33:55 +000036} {1 {UNIQUE constraint failed: t1.a}}
drhaee128d2005-02-14 20:48:18 +000037do_test index3-1.3 {
38 catchsql COMMIT;
39} {0 {}}
40integrity_check index3-1.4
41
drhedb04ed2015-09-04 12:54:01 +000042# Backwards compatibility test:
43#
44# Verify that CREATE INDEX statements that use strings instead of
45# identifiers for the the column names continue to work correctly.
46# This is undocumented behavior retained for backwards compatiblity.
47#
48do_execsql_test index3-2.1 {
49 DROP TABLE t1;
50 CREATE TABLE t1(a, b, c, d, e,
51 PRIMARY KEY('a'), UNIQUE('b' COLLATE nocase DESC));
52 CREATE INDEX t1c ON t1('c');
53 CREATE INDEX t1d ON t1('d' COLLATE binary ASC);
54 WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<30)
55 INSERT INTO t1(a,b,c,d,e)
56 SELECT x, printf('ab%03xxy',x), x, x, x FROM c;
57} {}
58do_execsql_test index3-2.2 {
59 SELECT a FROM t1 WHERE b='ab005xy' COLLATE nocase;
60} {5}
61do_execsql_test index3-2.2eqp {
62 EXPLAIN QUERY PLAN
63 SELECT a FROM t1 WHERE b='ab005xy' COLLATE nocase;
64} {/USING INDEX/}
65
66
drh85c23c62005-08-20 03:03:04 +000067# This test corrupts the database file so it must be the last test
68# in the series.
69#
70do_test index3-99.1 {
71 execsql {
72 PRAGMA writable_schema=on;
drhedb04ed2015-09-04 12:54:01 +000073 UPDATE sqlite_master SET sql='nonsense' WHERE name='t1d'
drh85c23c62005-08-20 03:03:04 +000074 }
75 db close
dancb354602010-07-08 09:44:42 +000076 catch { sqlite3 db test.db }
drhedb04ed2015-09-04 12:54:01 +000077 catchsql { DROP INDEX t1c }
78} {1 {malformed database schema (t1d)}}
drh85c23c62005-08-20 03:03:04 +000079
drhaee128d2005-02-14 20:48:18 +000080finish_test