Home | History | Annotate | Download | only in test
      1 # 2009 September 15
      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.
     12 #
     13 # This file implements tests for foreign keys.
     14 #
     15 
     16 set testdir [file dirname $argv0]
     17 source $testdir/tester.tcl
     18 
     19 ifcapable {!foreignkey||!trigger} {
     20   finish_test
     21   return
     22 }
     23 
     24 # Create a table and some data to work with.
     25 #
     26 do_test fkey3-1.1 {
     27   execsql {
     28     PRAGMA foreign_keys=ON;
     29     CREATE TABLE t1(x INTEGER PRIMARY KEY);
     30     INSERT INTO t1 VALUES(100);
     31     INSERT INTO t1 VALUES(101);
     32     CREATE TABLE t2(y INTEGER REFERENCES t1 (x));
     33     INSERT INTO t2 VALUES(100);
     34     INSERT INTO t2 VALUES(101);
     35     SELECT 1, x FROM t1;
     36     SELECT 2, y FROM t2;
     37   }
     38 } {1 100 1 101 2 100 2 101}
     39 
     40 do_test fkey3-1.2 {
     41   catchsql {
     42     DELETE FROM t1 WHERE x=100;
     43   }
     44 } {1 {foreign key constraint failed}}
     45 
     46 do_test fkey3-1.3 {
     47   catchsql {
     48     DROP TABLE t1;
     49   }
     50 } {1 {foreign key constraint failed}}
     51 
     52 do_test fkey3-1.4 {
     53   execsql {
     54     DROP TABLE t2;
     55   }
     56 } {}
     57 
     58 do_test fkey3-1.5 {
     59   execsql {
     60     DROP TABLE t1;
     61   }
     62 } {}
     63 
     64 do_test fkey3-2.1 {
     65   execsql {
     66     PRAGMA foreign_keys=ON;
     67     CREATE TABLE t1(x INTEGER PRIMARY KEY);
     68     INSERT INTO t1 VALUES(100);
     69     INSERT INTO t1 VALUES(101);
     70     CREATE TABLE t2(y INTEGER PRIMARY KEY REFERENCES t1 (x) ON UPDATE SET NULL);
     71   }
     72   execsql {
     73     INSERT INTO t2 VALUES(100);
     74     INSERT INTO t2 VALUES(101);
     75     SELECT 1, x FROM t1;
     76     SELECT 2, y FROM t2;
     77   }
     78 } {1 100 1 101 2 100 2 101}
     79 
     80 finish_test
     81