session8.test 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # 2011 July 13
  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. if {![info exists testdir]} {
  14. set testdir [file join [file dirname [info script]] .. .. test]
  15. }
  16. source [file join [file dirname [info script]] session_common.tcl]
  17. source $testdir/tester.tcl
  18. ifcapable !session {finish_test; return}
  19. set testprefix session8
  20. proc noop {args} {}
  21. # Like [dbcksum] in tester.tcl. Except this version is not sensitive
  22. # to changes in the value of implicit IPK columns.
  23. #
  24. proc udbcksum {db dbname} {
  25. if {$dbname=="temp"} {
  26. set master sqlite_temp_master
  27. } else {
  28. set master $dbname.sqlite_master
  29. }
  30. set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
  31. set txt [$db eval "SELECT * FROM $master"]\n
  32. foreach tab $alltab {
  33. append txt [lsort [$db eval "SELECT * FROM $dbname.$tab"]]\n
  34. }
  35. return [md5 $txt]
  36. }
  37. proc do_then_undo {tn sql} {
  38. set ck1 [udbcksum db main]
  39. sqlite3session S db main
  40. S attach *
  41. db eval $sql
  42. set ck2 [udbcksum db main]
  43. set invert [sqlite3changeset_invert [S changeset]]
  44. S delete
  45. sqlite3changeset_apply db $invert noop
  46. set ck3 [udbcksum db main]
  47. set a [expr {$ck1==$ck2}]
  48. set b [expr {$ck1==$ck3}]
  49. uplevel [list do_test $tn.1 "set {} $a" 0]
  50. uplevel [list do_test $tn.2 "set {} $b" 1]
  51. }
  52. do_execsql_test 1.1 {
  53. CREATE TABLE t1(a PRIMARY KEY, b);
  54. INSERT INTO t1 VALUES(1, 2);
  55. INSERT INTO t1 VALUES('abc', 'xyz');
  56. }
  57. do_then_undo 1.2 { INSERT INTO t1 VALUES(3, 4); }
  58. do_then_undo 1.3 { DELETE FROM t1 WHERE b=2; }
  59. do_then_undo 1.4 { UPDATE t1 SET b = 3 WHERE a = 1; }
  60. do_execsql_test 2.1 {
  61. CREATE TABLE t2(a, b PRIMARY KEY);
  62. INSERT INTO t2 VALUES(1, 2);
  63. INSERT INTO t2 VALUES('abc', 'xyz');
  64. }
  65. do_then_undo 1.2 { INSERT INTO t2 VALUES(3, 4); }
  66. do_then_undo 1.3 { DELETE FROM t2 WHERE b=2; }
  67. do_then_undo 1.4 { UPDATE t1 SET a = '123' WHERE b = 'xyz'; }
  68. do_execsql_test 3.1 {
  69. CREATE TABLE t3(a, b, c, d, e, PRIMARY KEY(c, e));
  70. INSERT INTO t3 VALUES('x', 45, 0.0, 'abcdef', 12);
  71. INSERT INTO t3 VALUES(45, 0.0, 'abcdef', 12, 'x');
  72. INSERT INTO t3 VALUES(0.0, 'abcdef', 12, 'x', 45);
  73. }
  74. do_then_undo 3.2 { UPDATE t3 SET b=b||b WHERE e!='x' }
  75. do_then_undo 3.3 { UPDATE t3 SET a = 46 }
  76. finish_test