1# 2005 November 2 2# 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 CHECK constraints 13# 14# $Id: check.test,v 1.12 2009/03/24 15:08:10 drh Exp $ 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19# Only run these tests if the build includes support for CHECK constraints 20ifcapable !check { 21 finish_test 22 return 23} 24 25do_test check-1.1 { 26 execsql { 27 CREATE TABLE t1( 28 x INTEGER CHECK( x<5 ), 29 y REAL CHECK( y>x ) 30 ); 31 } 32} {} 33do_test check-1.2 { 34 execsql { 35 INSERT INTO t1 VALUES(3,4); 36 SELECT * FROM t1; 37 } 38} {3 4.0} 39do_test check-1.3 { 40 catchsql { 41 INSERT INTO t1 VALUES(6,7); 42 } 43} {1 {constraint failed}} 44do_test check-1.4 { 45 execsql { 46 SELECT * FROM t1; 47 } 48} {3 4.0} 49do_test check-1.5 { 50 catchsql { 51 INSERT INTO t1 VALUES(4,3); 52 } 53} {1 {constraint failed}} 54do_test check-1.6 { 55 execsql { 56 SELECT * FROM t1; 57 } 58} {3 4.0} 59do_test check-1.7 { 60 catchsql { 61 INSERT INTO t1 VALUES(NULL,6); 62 } 63} {0 {}} 64do_test check-1.8 { 65 execsql { 66 SELECT * FROM t1; 67 } 68} {3 4.0 {} 6.0} 69do_test check-1.9 { 70 catchsql { 71 INSERT INTO t1 VALUES(2,NULL); 72 } 73} {0 {}} 74do_test check-1.10 { 75 execsql { 76 SELECT * FROM t1; 77 } 78} {3 4.0 {} 6.0 2 {}} 79do_test check-1.11 { 80 execsql { 81 DELETE FROM t1 WHERE x IS NULL OR x!=3; 82 UPDATE t1 SET x=2 WHERE x==3; 83 SELECT * FROM t1; 84 } 85} {2 4.0} 86do_test check-1.12 { 87 catchsql { 88 UPDATE t1 SET x=7 WHERE x==2 89 } 90} {1 {constraint failed}} 91do_test check-1.13 { 92 execsql { 93 SELECT * FROM t1; 94 } 95} {2 4.0} 96do_test check-1.14 { 97 catchsql { 98 UPDATE t1 SET x=5 WHERE x==2 99 } 100} {1 {constraint failed}} 101do_test check-1.15 { 102 execsql { 103 SELECT * FROM t1; 104 } 105} {2 4.0} 106do_test check-1.16 { 107 catchsql { 108 UPDATE t1 SET x=4, y=11 WHERE x==2 109 } 110} {0 {}} 111do_test check-1.17 { 112 execsql { 113 SELECT * FROM t1; 114 } 115} {4 11.0} 116 117do_test check-2.1 { 118breakpoint 119 execsql { 120 CREATE TABLE t2( 121 x INTEGER CHECK( typeof(coalesce(x,0))=="integer" ), 122 y REAL CHECK( typeof(coalesce(y,0.1))=='real' ), 123 z TEXT CHECK( typeof(coalesce(z,''))=='text' ) 124 ); 125 } 126} {} 127do_test check-2.2 { 128 execsql { 129 INSERT INTO t2 VALUES(1,2.2,'three'); 130 SELECT * FROM t2; 131 } 132} {1 2.2 three} 133db close 134sqlite3 db test.db 135do_test check-2.3 { 136 execsql { 137 INSERT INTO t2 VALUES(NULL, NULL, NULL); 138 SELECT * FROM t2; 139 } 140} {1 2.2 three {} {} {}} 141do_test check-2.4 { 142 catchsql { 143 INSERT INTO t2 VALUES(1.1, NULL, NULL); 144 } 145} {1 {constraint failed}} 146do_test check-2.5 { 147 catchsql { 148 INSERT INTO t2 VALUES(NULL, 5, NULL); 149 } 150} {1 {constraint failed}} 151do_test check-2.6 { 152 catchsql { 153 INSERT INTO t2 VALUES(NULL, NULL, 3.14159); 154 } 155} {1 {constraint failed}} 156 157ifcapable subquery { 158 do_test check-3.1 { 159 catchsql { 160 CREATE TABLE t3( 161 x, y, z, 162 CHECK( x<(SELECT min(x) FROM t1) ) 163 ); 164 } 165 } {1 {subqueries prohibited in CHECK constraints}} 166} 167 168do_test check-3.2 { 169 execsql { 170 SELECT name FROM sqlite_master ORDER BY name 171 } 172} {t1 t2} 173do_test check-3.3 { 174 catchsql { 175 CREATE TABLE t3( 176 x, y, z, 177 CHECK( q<x ) 178 ); 179 } 180} {1 {no such column: q}} 181do_test check-3.4 { 182 execsql { 183 SELECT name FROM sqlite_master ORDER BY name 184 } 185} {t1 t2} 186do_test check-3.5 { 187 catchsql { 188 CREATE TABLE t3( 189 x, y, z, 190 CHECK( t2.x<x ) 191 ); 192 } 193} {1 {no such column: t2.x}} 194do_test check-3.6 { 195 execsql { 196 SELECT name FROM sqlite_master ORDER BY name 197 } 198} {t1 t2} 199do_test check-3.7 { 200 catchsql { 201 CREATE TABLE t3( 202 x, y, z, 203 CHECK( t3.x<25 ) 204 ); 205 } 206} {0 {}} 207do_test check-3.8 { 208 execsql { 209 INSERT INTO t3 VALUES(1,2,3); 210 SELECT * FROM t3; 211 } 212} {1 2 3} 213do_test check-3.9 { 214 catchsql { 215 INSERT INTO t3 VALUES(111,222,333); 216 } 217} {1 {constraint failed}} 218 219do_test check-4.1 { 220 execsql { 221 CREATE TABLE t4(x, y, 222 CHECK ( 223 x+y==11 224 OR x*y==12 225 OR x/y BETWEEN 5 AND 8 226 OR -x==y+10 227 ) 228 ); 229 } 230} {} 231do_test check-4.2 { 232 execsql { 233 INSERT INTO t4 VALUES(1,10); 234 SELECT * FROM t4 235 } 236} {1 10} 237do_test check-4.3 { 238 execsql { 239 UPDATE t4 SET x=4, y=3; 240 SELECT * FROM t4 241 } 242} {4 3} 243do_test check-4.3 { 244 execsql { 245 UPDATE t4 SET x=12, y=2; 246 SELECT * FROM t4 247 } 248} {12 2} 249do_test check-4.4 { 250 execsql { 251 UPDATE t4 SET x=12, y=-22; 252 SELECT * FROM t4 253 } 254} {12 -22} 255do_test check-4.5 { 256 catchsql { 257 UPDATE t4 SET x=0, y=1; 258 } 259} {1 {constraint failed}} 260do_test check-4.6 { 261 execsql { 262 SELECT * FROM t4; 263 } 264} {12 -22} 265do_test check-4.7 { 266 execsql { 267 PRAGMA ignore_check_constraints=ON; 268 UPDATE t4 SET x=0, y=1; 269 SELECT * FROM t4; 270 } 271} {0 1} 272do_test check-4.8 { 273 catchsql { 274 PRAGMA ignore_check_constraints=OFF; 275 UPDATE t4 SET x=0, y=2; 276 } 277} {1 {constraint failed}} 278ifcapable vacuum { 279 do_test check_4.9 { 280 catchsql { 281 VACUUM 282 } 283 } {0 {}} 284} 285 286do_test check-5.1 { 287 catchsql { 288 CREATE TABLE t5(x, y, 289 CHECK( x*y<:abc ) 290 ); 291 } 292} {1 {parameters prohibited in CHECK constraints}} 293do_test check-5.2 { 294 catchsql { 295 CREATE TABLE t5(x, y, 296 CHECK( x*y<? ) 297 ); 298 } 299} {1 {parameters prohibited in CHECK constraints}} 300 301ifcapable conflict { 302 303do_test check-6.1 { 304 execsql {SELECT * FROM t1} 305} {4 11.0} 306do_test check-6.2 { 307 execsql { 308 UPDATE OR IGNORE t1 SET x=5; 309 SELECT * FROM t1; 310 } 311} {4 11.0} 312do_test check-6.3 { 313 execsql { 314 INSERT OR IGNORE INTO t1 VALUES(5,4.0); 315 SELECT * FROM t1; 316 } 317} {4 11.0} 318do_test check-6.4 { 319 execsql { 320 INSERT OR IGNORE INTO t1 VALUES(2,20.0); 321 SELECT * FROM t1; 322 } 323} {4 11.0 2 20.0} 324do_test check-6.5 { 325 catchsql { 326 UPDATE OR FAIL t1 SET x=7-x, y=y+1; 327 } 328} {1 {constraint failed}} 329do_test check-6.6 { 330 execsql { 331 SELECT * FROM t1; 332 } 333} {3 12.0 2 20.0} 334do_test check-6.7 { 335 catchsql { 336 BEGIN; 337 INSERT INTO t1 VALUES(1,30.0); 338 INSERT OR ROLLBACK INTO t1 VALUES(8,40.0); 339 } 340} {1 {constraint failed}} 341do_test check-6.8 { 342 catchsql { 343 COMMIT; 344 } 345} {1 {cannot commit - no transaction is active}} 346do_test check-6.9 { 347 execsql { 348 SELECT * FROM t1 349 } 350} {3 12.0 2 20.0} 351 352do_test check-6.11 { 353 execsql {SELECT * FROM t1} 354} {3 12.0 2 20.0} 355do_test check-6.12 { 356 catchsql { 357 REPLACE INTO t1 VALUES(6,7); 358 } 359} {1 {constraint failed}} 360do_test check-6.13 { 361 execsql {SELECT * FROM t1} 362} {3 12.0 2 20.0} 363do_test check-6.14 { 364 catchsql { 365 INSERT OR IGNORE INTO t1 VALUES(6,7); 366 } 367} {0 {}} 368do_test check-6.15 { 369 execsql {SELECT * FROM t1} 370} {3 12.0 2 20.0} 371 372 373} 374 375finish_test 376