3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing CHECK constraints
14 # $Id: check.test,v 1.13 2009/06/05 17:09:12 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Only run these tests if the build includes support for CHECK constraints
28 x INTEGER CHECK( x<5 ),
35 INSERT INTO t1 VALUES(3,4);
41 INSERT INTO t1 VALUES(6,7);
43 } {1 {constraint failed}}
51 INSERT INTO t1 VALUES(4,3);
53 } {1 {constraint failed}}
61 INSERT INTO t1 VALUES(NULL,6);
71 INSERT INTO t1 VALUES(2,NULL);
81 DELETE FROM t1 WHERE x IS NULL OR x!=3;
82 UPDATE t1 SET x=2 WHERE x==3;
88 UPDATE t1 SET x=7 WHERE x==2
90 } {1 {constraint failed}}
98 UPDATE t1 SET x=5 WHERE x==2
100 } {1 {constraint failed}}
108 UPDATE t1 SET x=4, y=11 WHERE x==2
120 x INTEGER CHECK( typeof(coalesce(x,0))=="integer" ),
121 y REAL CHECK( typeof(coalesce(y,0.1))=='real' ),
122 z TEXT CHECK( typeof(coalesce(z,''))=='text' )
128 INSERT INTO t2 VALUES(1,2.2,'three');
136 INSERT INTO t2 VALUES(NULL, NULL, NULL);
139 } {1 2.2 three {} {} {}}
142 INSERT INTO t2 VALUES(1.1, NULL, NULL);
144 } {1 {constraint failed}}
147 INSERT INTO t2 VALUES(NULL, 5, NULL);
149 } {1 {constraint failed}}
152 INSERT INTO t2 VALUES(NULL, NULL, 3.14159);
154 } {1 {constraint failed}}
161 CHECK( x<(SELECT min(x) FROM t1) )
164 } {1 {subqueries prohibited in CHECK constraints}}
169 SELECT name FROM sqlite_master ORDER BY name
179 } {1 {no such column: q}}
182 SELECT name FROM sqlite_master ORDER BY name
192 } {1 {no such column: t2.x}}
195 SELECT name FROM sqlite_master ORDER BY name
208 INSERT INTO t3 VALUES(1,2,3);
214 INSERT INTO t3 VALUES(111,222,333);
216 } {1 {constraint failed}}
220 CREATE TABLE t4(x, y,
224 OR x/y BETWEEN 5 AND 8
232 INSERT INTO t4 VALUES(1,10);
238 UPDATE t4 SET x=4, y=3;
244 UPDATE t4 SET x=12, y=2;
250 UPDATE t4 SET x=12, y=-22;
256 UPDATE t4 SET x=0, y=1;
258 } {1 {constraint failed}}
266 PRAGMA ignore_check_constraints=ON;
267 UPDATE t4 SET x=0, y=1;
273 PRAGMA ignore_check_constraints=OFF;
274 UPDATE t4 SET x=0, y=2;
276 } {1 {constraint failed}}
287 CREATE TABLE t5(x, y,
291 } {1 {parameters prohibited in CHECK constraints}}
294 CREATE TABLE t5(x, y,
298 } {1 {parameters prohibited in CHECK constraints}}
303 execsql {SELECT * FROM t1}
307 UPDATE OR IGNORE t1 SET x=5;
313 INSERT OR IGNORE INTO t1 VALUES(5,4.0);
319 INSERT OR IGNORE INTO t1 VALUES(2,20.0);
325 UPDATE OR FAIL t1 SET x=7-x, y=y+1;
327 } {1 {constraint failed}}
336 INSERT INTO t1 VALUES(1,30.0);
337 INSERT OR ROLLBACK INTO t1 VALUES(8,40.0);
339 } {1 {constraint failed}}
344 } {1 {cannot commit - no transaction is active}}
352 execsql {SELECT * FROM t1}
356 REPLACE INTO t1 VALUES(6,7);
358 } {1 {constraint failed}}
360 execsql {SELECT * FROM t1}
364 INSERT OR IGNORE INTO t1 VALUES(6,7);
368 execsql {SELECT * FROM t1}