1
0

fkey4.test 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. ifcapable {!foreignkey||!trigger} {
  21. finish_test
  22. return
  23. }
  24. # Create a table and some data to work with.
  25. #
  26. do_test fkey4-1.1 {
  27. execsql {
  28. PRAGMA foreign_keys = ON;
  29. CREATE TABLE t1(a PRIMARY KEY, b);
  30. CREATE TABLE t2(c REFERENCES t1 DEFERRABLE INITIALLY DEFERRED, d);
  31. INSERT INTO t1 VALUES(1,2);
  32. INSERT INTO t2 VALUES(1,3);
  33. }
  34. } {}
  35. do_test fkey4-1.2 {
  36. set ::DB [sqlite3_connection_pointer db]
  37. set ::SQL {INSERT INTO t2 VALUES(2,4)}
  38. set ::STMT1 [sqlite3_prepare_v2 $::DB $::SQL -1 TAIL]
  39. sqlite3_step $::STMT1
  40. } {SQLITE_CONSTRAINT}
  41. verify_ex_errcode fkey4-1.2b SQLITE_CONSTRAINT_FOREIGNKEY
  42. do_test fkey4-1.3 {
  43. set ::STMT2 [sqlite3_prepare_v2 $::DB $::SQL -1 TAIL]
  44. sqlite3_step $::STMT2
  45. } {SQLITE_CONSTRAINT}
  46. verify_ex_errcode fkey4-1.3b SQLITE_CONSTRAINT_FOREIGNKEY
  47. do_test fkey4-1.4 {
  48. db eval {SELECT * FROM t2}
  49. } {1 3}
  50. sqlite3_finalize $::STMT1
  51. sqlite3_finalize $::STMT2
  52. finish_test