xref: /sqlite-3.40.0/test/fkey4.test (revision 433dccfb)
1# 2011 Feb 04
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 test deferred foreign key constraint processing to make
14# sure that when a statement not within BEGIN...END fails a constraint,
15# that statement doesn't hold the transaction open thus allowing
16# a subsequent statement to fail a deferred constraint with impunity.
17#
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22ifcapable {!foreignkey||!trigger} {
23  finish_test
24  return
25}
26
27# Create a table and some data to work with.
28#
29do_test fkey4-1.1 {
30  execsql {
31    PRAGMA foreign_keys = ON;
32    CREATE TABLE t1(a PRIMARY KEY, b);
33    CREATE TABLE t2(c REFERENCES t1 DEFERRABLE INITIALLY DEFERRED, d);
34    INSERT INTO t1 VALUES(1,2);
35    INSERT INTO t2 VALUES(1,3);
36  }
37} {}
38
39do_test fkey4-1.2 {
40  set ::DB [sqlite3_connection_pointer db]
41  set ::SQL {INSERT INTO t2 VALUES(2,4)}
42  set ::STMT1 [sqlite3_prepare_v2 $::DB $::SQL -1 TAIL]
43  sqlite3_step $::STMT1
44} {SQLITE_CONSTRAINT}
45verify_ex_errcode fkey4-1.2b SQLITE_CONSTRAINT_FOREIGNKEY
46do_test fkey4-1.3 {
47  set ::STMT2 [sqlite3_prepare_v2 $::DB $::SQL -1 TAIL]
48  sqlite3_step $::STMT2
49} {SQLITE_CONSTRAINT}
50verify_ex_errcode fkey4-1.3b SQLITE_CONSTRAINT_FOREIGNKEY
51do_test fkey4-1.4 {
52  db eval {SELECT * FROM t2}
53} {1 3}
54sqlite3_finalize $::STMT1
55sqlite3_finalize $::STMT2
56
57finish_test
58