1edb04ed9Sdrh# 2005-02-14 2aee128dcSdrh# 3aee128dcSdrh# The author disclaims copyright to this source code. In place of 4aee128dcSdrh# a legal notice, here is a blessing: 5aee128dcSdrh# 6aee128dcSdrh# May you do good and not evil. 7aee128dcSdrh# May you find forgiveness for yourself and forgive others. 8aee128dcSdrh# May you share freely, never taking more than you give. 9aee128dcSdrh# 10aee128dcSdrh#*********************************************************************** 11aee128dcSdrh# This file implements regression tests for SQLite library. The 12aee128dcSdrh# focus of this file is testing the CREATE INDEX statement. 13aee128dcSdrh# 14aee128dcSdrh 15aee128dcSdrh 16aee128dcSdrhset testdir [file dirname $argv0] 17aee128dcSdrhsource $testdir/tester.tcl 18aee128dcSdrh 19aee128dcSdrh# Ticket #1115. Make sure that when a UNIQUE index is created on a 20aee128dcSdrh# non-unique column (or columns) that it fails and that it leaves no 21aee128dcSdrh# residue behind. 22aee128dcSdrh# 23aee128dcSdrhdo_test index3-1.1 { 24aee128dcSdrh execsql { 25aee128dcSdrh CREATE TABLE t1(a); 26aee128dcSdrh INSERT INTO t1 VALUES(1); 27aee128dcSdrh INSERT INTO t1 VALUES(1); 28aee128dcSdrh SELECT * FROM t1; 29aee128dcSdrh } 30aee128dcSdrh} {1 1} 31aee128dcSdrhdo_test index3-1.2 { 32aee128dcSdrh catchsql { 33aee128dcSdrh BEGIN; 34aee128dcSdrh CREATE UNIQUE INDEX i1 ON t1(a); 35aee128dcSdrh } 36f9c8ce3cSdrh} {1 {UNIQUE constraint failed: t1.a}} 37aee128dcSdrhdo_test index3-1.3 { 38aee128dcSdrh catchsql COMMIT; 39aee128dcSdrh} {0 {}} 40aee128dcSdrhintegrity_check index3-1.4 41aee128dcSdrh 42edb04ed9Sdrh# Backwards compatibility test: 43edb04ed9Sdrh# 44edb04ed9Sdrh# Verify that CREATE INDEX statements that use strings instead of 45edb04ed9Sdrh# identifiers for the the column names continue to work correctly. 46edb04ed9Sdrh# This is undocumented behavior retained for backwards compatiblity. 47edb04ed9Sdrh# 48edb04ed9Sdrhdo_execsql_test index3-2.1 { 49edb04ed9Sdrh DROP TABLE t1; 50edb04ed9Sdrh CREATE TABLE t1(a, b, c, d, e, 51edb04ed9Sdrh PRIMARY KEY('a'), UNIQUE('b' COLLATE nocase DESC)); 52edb04ed9Sdrh CREATE INDEX t1c ON t1('c'); 53edb04ed9Sdrh CREATE INDEX t1d ON t1('d' COLLATE binary ASC); 54edb04ed9Sdrh WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<30) 55edb04ed9Sdrh INSERT INTO t1(a,b,c,d,e) 56edb04ed9Sdrh SELECT x, printf('ab%03xxy',x), x, x, x FROM c; 57edb04ed9Sdrh} {} 58edb04ed9Sdrhdo_execsql_test index3-2.2 { 59edb04ed9Sdrh SELECT a FROM t1 WHERE b='ab005xy' COLLATE nocase; 60edb04ed9Sdrh} {5} 61edb04ed9Sdrhdo_execsql_test index3-2.2eqp { 62edb04ed9Sdrh EXPLAIN QUERY PLAN 63edb04ed9Sdrh SELECT a FROM t1 WHERE b='ab005xy' COLLATE nocase; 64edb04ed9Sdrh} {/USING INDEX/} 65153110a7Sdrhdo_execsql_test index3-2.3 { 66153110a7Sdrh SELECT name FROM sqlite_master WHERE tbl_name='t1' ORDER BY name 67153110a7Sdrh} {sqlite_autoindex_t1_1 sqlite_autoindex_t1_2 t1 t1c t1d} 68153110a7Sdrhdo_execsql_test index3-2.4 { 69153110a7Sdrh CREATE TABLE t2a(a integer, b, PRIMARY KEY(a)); 70153110a7Sdrh CREATE TABLE t2b("a" integer, b, PRIMARY KEY("a")); 71153110a7Sdrh CREATE TABLE t2c([a] integer, b, PRIMARY KEY([a])); 72153110a7Sdrh CREATE TABLE t2d('a' integer, b, PRIMARY KEY('a')); 73153110a7Sdrh} 74153110a7Sdrhdo_execsql_test index3-2.5 { 75153110a7Sdrh SELECT name FROM sqlite_master WHERE tbl_name LIKE 't2_' ORDER BY name 76153110a7Sdrh} {t2a t2b t2c t2d} 77153110a7Sdrh 78153110a7Sdrh 79153110a7Sdrh 80edb04ed9Sdrh 81edb04ed9Sdrh 8285c23c61Sdrh# This test corrupts the database file so it must be the last test 8385c23c61Sdrh# in the series. 8485c23c61Sdrh# 8585c23c61Sdrhdo_test index3-99.1 { 86*6ab91a7aSdrh sqlite3_db_config db DEFENSIVE 0 8785c23c61Sdrh execsql { 8885c23c61Sdrh PRAGMA writable_schema=on; 89edb04ed9Sdrh UPDATE sqlite_master SET sql='nonsense' WHERE name='t1d' 9085c23c61Sdrh } 9185c23c61Sdrh db close 92cb354603Sdan catch { sqlite3 db test.db } 93edb04ed9Sdrh catchsql { DROP INDEX t1c } 94edb04ed9Sdrh} {1 {malformed database schema (t1d)}} 9585c23c61Sdrh 96aee128dcSdrhfinish_test 97