• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19ifcapable {!foreignkey||!trigger} {
20  finish_test
21  return
22}
23
24# Create a table and some data to work with.
25#
26do_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
40do_test fkey3-1.2 {
41  catchsql {
42    DELETE FROM t1 WHERE x=100;
43  }
44} {1 {foreign key constraint failed}}
45
46do_test fkey3-1.3 {
47  catchsql {
48    DROP TABLE t1;
49  }
50} {1 {foreign key constraint failed}}
51
52do_test fkey3-1.4 {
53  execsql {
54    DROP TABLE t2;
55  }
56} {}
57
58do_test fkey3-1.5 {
59  execsql {
60    DROP TABLE t1;
61  }
62} {}
63
64do_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
80finish_test
81