trigger6.test 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # 2004 December 07
  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 to make sure expression of an INSERT
  14. # and UPDATE statement are only evaluated once. See ticket #980.
  15. # If an expression uses a function that has side-effects or which
  16. # is not deterministic (ex: random()) then we want to make sure
  17. # that the same evaluation occurs for the actual INSERT/UPDATE and
  18. # for the NEW.* fields of any triggers that fire.
  19. #
  20. # $Id: trigger6.test,v 1.2 2005/05/05 11:04:50 drh Exp $
  21. set testdir [file dirname $argv0]
  22. source $testdir/tester.tcl
  23. ifcapable {!trigger} {
  24. finish_test
  25. return
  26. }
  27. do_test trigger6-1.1 {
  28. execsql {
  29. CREATE TABLE t1(x, y);
  30. CREATE TABLE log(a, b, c);
  31. CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN
  32. INSERT INTO log VALUES(1, new.x, new.y);
  33. END;
  34. CREATE TRIGGER r2 BEFORE UPDATE ON t1 BEGIN
  35. INSERT INTO log VALUES(2, new.x, new.y);
  36. END;
  37. }
  38. set ::trigger6_cnt 0
  39. proc trigger6_counter {args} {
  40. incr ::trigger6_cnt
  41. return $::trigger6_cnt
  42. }
  43. db function counter trigger6_counter
  44. execsql {
  45. INSERT INTO t1 VALUES(1,counter());
  46. SELECT * FROM t1;
  47. }
  48. } {1 1}
  49. do_test trigger6-1.2 {
  50. execsql {
  51. SELECT * FROM log;
  52. }
  53. } {1 1 1}
  54. do_test trigger6-1.3 {
  55. execsql {
  56. DELETE FROM t1;
  57. DELETE FROM log;
  58. INSERT INTO t1 VALUES(2,counter(2,3)+4);
  59. SELECT * FROM t1;
  60. }
  61. } {2 6}
  62. do_test trigger6-1.4 {
  63. execsql {
  64. SELECT * FROM log;
  65. }
  66. } {1 2 6}
  67. do_test trigger6-1.5 {
  68. execsql {
  69. DELETE FROM log;
  70. UPDATE t1 SET y=counter(5);
  71. SELECT * FROM t1;
  72. }
  73. } {2 3}
  74. do_test trigger6-1.6 {
  75. execsql {
  76. SELECT * FROM log;
  77. }
  78. } {2 2 3}
  79. finish_test