2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 27
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing the CREATE UNIQUE INDEX statement,
2N/A# and primary keys, and the UNIQUE constraint on table columns
2N/A#
2N/A# $Id: unique.test,v 1.7 2003/08/05 13:13:39 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Try to create a table with two primary keys.
2N/A# (This is allowed in SQLite even that it is not valid SQL)
2N/A#
2N/Ado_test unique-1.1 {
2N/A catchsql {
2N/A CREATE TABLE t1(
2N/A a int PRIMARY KEY,
2N/A b int PRIMARY KEY,
2N/A c text
2N/A );
2N/A }
2N/A} {1 {table "t1" has more than one primary key}}
2N/Ado_test unique-1.1b {
2N/A catchsql {
2N/A CREATE TABLE t1(
2N/A a int PRIMARY KEY,
2N/A b int UNIQUE,
2N/A c text
2N/A );
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-1.2 {
2N/A catchsql {
2N/A INSERT INTO t1(a,b,c) VALUES(1,2,3)
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-1.3 {
2N/A catchsql {
2N/A INSERT INTO t1(a,b,c) VALUES(1,3,4)
2N/A }
2N/A} {1 {column a is not unique}}
2N/Ado_test unique-1.4 {
2N/A execsql {
2N/A SELECT * FROM t1 ORDER BY a;
2N/A }
2N/A} {1 2 3}
2N/Ado_test unique-1.5 {
2N/A catchsql {
2N/A INSERT INTO t1(a,b,c) VALUES(3,2,4)
2N/A }
2N/A} {1 {column b is not unique}}
2N/Ado_test unique-1.6 {
2N/A execsql {
2N/A SELECT * FROM t1 ORDER BY a;
2N/A }
2N/A} {1 2 3}
2N/Ado_test unique-1.7 {
2N/A catchsql {
2N/A INSERT INTO t1(a,b,c) VALUES(3,4,5)
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-1.8 {
2N/A execsql {
2N/A SELECT * FROM t1 ORDER BY a;
2N/A }
2N/A} {1 2 3 3 4 5}
2N/Aintegrity_check unique-1.9
2N/A
2N/Ado_test unique-2.0 {
2N/A execsql {
2N/A DROP TABLE t1;
2N/A CREATE TABLE t2(a int, b int);
2N/A INSERT INTO t2(a,b) VALUES(1,2);
2N/A INSERT INTO t2(a,b) VALUES(3,4);
2N/A SELECT * FROM t2 ORDER BY a;
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test unique-2.1 {
2N/A catchsql {
2N/A CREATE UNIQUE INDEX i2 ON t2(a)
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-2.2 {
2N/A catchsql {
2N/A SELECT * FROM t2 ORDER BY a
2N/A }
2N/A} {0 {1 2 3 4}}
2N/Ado_test unique-2.3 {
2N/A catchsql {
2N/A INSERT INTO t2 VALUES(1,5);
2N/A }
2N/A} {1 {column a is not unique}}
2N/Ado_test unique-2.4 {
2N/A catchsql {
2N/A SELECT * FROM t2 ORDER BY a
2N/A }
2N/A} {0 {1 2 3 4}}
2N/Ado_test unique-2.5 {
2N/A catchsql {
2N/A DROP INDEX i2;
2N/A SELECT * FROM t2 ORDER BY a;
2N/A }
2N/A} {0 {1 2 3 4}}
2N/Ado_test unique-2.6 {
2N/A catchsql {
2N/A INSERT INTO t2 VALUES(1,5)
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-2.7 {
2N/A catchsql {
2N/A SELECT * FROM t2 ORDER BY a, b;
2N/A }
2N/A} {0 {1 2 1 5 3 4}}
2N/Ado_test unique-2.8 {
2N/A catchsql {
2N/A CREATE UNIQUE INDEX i2 ON t2(a);
2N/A }
2N/A} {1 {indexed columns are not unique}}
2N/Ado_test unique-2.9 {
2N/A catchsql {
2N/A CREATE INDEX i2 ON t2(a);
2N/A }
2N/A} {0 {}}
2N/Aintegrity_check unique-2.10
2N/A
2N/A# Test the UNIQUE keyword as used on two or more fields.
2N/A#
2N/Ado_test unique-3.1 {
2N/A catchsql {
2N/A CREATE TABLE t3(
2N/A a int,
2N/A b int,
2N/A c int,
2N/A d int,
2N/A unique(a,c,d)
2N/A );
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-3.2 {
2N/A catchsql {
2N/A INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4);
2N/A SELECT * FROM t3 ORDER BY a,b,c,d;
2N/A }
2N/A} {0 {1 2 3 4}}
2N/Ado_test unique-3.3 {
2N/A catchsql {
2N/A INSERT INTO t3(a,b,c,d) VALUES(1,2,3,5);
2N/A SELECT * FROM t3 ORDER BY a,b,c,d;
2N/A }
2N/A} {0 {1 2 3 4 1 2 3 5}}
2N/Ado_test unique-3.4 {
2N/A catchsql {
2N/A INSERT INTO t3(a,b,c,d) VALUES(1,4,3,5);
2N/A SELECT * FROM t3 ORDER BY a,b,c,d;
2N/A }
2N/A} {1 {columns a, c, d are not unique}}
2N/Aintegrity_check unique-3.5
2N/A
2N/A# Make sure NULLs are distinct as far as the UNIQUE tests are
2N/A# concerned.
2N/A#
2N/Ado_test unique-4.1 {
2N/A execsql {
2N/A CREATE TABLE t4(a UNIQUE, b, c, UNIQUE(b,c));
2N/A INSERT INTO t4 VALUES(1,2,3);
2N/A INSERT INTO t4 VALUES(NULL, 2, NULL);
2N/A SELECT * FROM t4;
2N/A }
2N/A} {1 2 3 {} 2 {}}
2N/Ado_test unique-4.2 {
2N/A catchsql {
2N/A INSERT INTO t4 VALUES(NULL, 3, 4);
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-4.3 {
2N/A execsql {
2N/A SELECT * FROM t4
2N/A }
2N/A} {1 2 3 {} 2 {} {} 3 4}
2N/Ado_test unique-4.4 {
2N/A catchsql {
2N/A INSERT INTO t4 VALUES(2, 2, NULL);
2N/A }
2N/A} {0 {}}
2N/Ado_test unique-4.5 {
2N/A execsql {
2N/A SELECT * FROM t4
2N/A }
2N/A} {1 2 3 {} 2 {} {} 3 4 2 2 {}}
2N/Aintegrity_check unique-4.6
2N/A
2N/A# Test the error message generation logic. In particular, make sure we
2N/A# do not overflow the static buffer used to generate the error message.
2N/A#
2N/Ado_test unique-5.1 {
2N/A execsql {
2N/A CREATE TABLE t5(
2N/A first_column_with_long_name,
2N/A second_column_with_long_name,
2N/A third_column_with_long_name,
2N/A fourth_column_with_long_name,
2N/A fifth_column_with_long_name,
2N/A sixth_column_with_long_name,
2N/A UNIQUE(
2N/A first_column_with_long_name,
2N/A second_column_with_long_name,
2N/A third_column_with_long_name,
2N/A fourth_column_with_long_name,
2N/A fifth_column_with_long_name,
2N/A sixth_column_with_long_name
2N/A )
2N/A );
2N/A INSERT INTO t5 VALUES(1,2,3,4,5,6);
2N/A SELECT * FROM t5;
2N/A }
2N/A} {1 2 3 4 5 6}
2N/Ado_test unique-5.2 {
2N/A catchsql {
2N/A INSERT INTO t5 VALUES(1,2,3,4,5,6);
2N/A }
2N/A} {1 {columns first_column_with_long_name, second_column_with_long_name, third_column_with_long_name, fourth_column_with_long_name, fifth_column_with_long_name, ... are not unique}}
2N/A
2N/Afinish_test