sessiondiff.test 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # 2015-07-31
  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. #
  12. # Tests for the [sqldiff --changeset] command.
  13. #
  14. #
  15. if {![info exists testdir]} {
  16. set testdir [file join [file dirname [info script]] .. .. test]
  17. }
  18. source $testdir/tester.tcl
  19. ifcapable !session {finish_test; return}
  20. set testprefix sessiondiff
  21. set PROG [test_find_sqldiff]
  22. db close
  23. proc sqlesc {id} {
  24. set ret "'[string map {' ''} $id]'"
  25. set ret
  26. }
  27. proc database_cksum {db1} {
  28. set txt ""
  29. sqlite3 dbtmp $db1
  30. foreach tbl [dbtmp eval {SELECT name FROM sqlite_master WHERE type='table'}] {
  31. set cols [list]
  32. dbtmp eval "PRAGMA table_info = [sqlesc $tbl]" {
  33. lappend cols "quote( $name )"
  34. }
  35. append txt [dbtmp eval \
  36. "SELECT [join $cols {||'.'||}] FROM [sqlesc $tbl] ORDER BY 1"
  37. ]
  38. }
  39. dbtmp close
  40. md5 $txt
  41. }
  42. proc readfile {filename} {
  43. set fd [open $filename]
  44. fconfigure $fd -translation binary
  45. set data [read $fd]
  46. close $fd
  47. set data
  48. }
  49. proc get_changeset {db1 db2} {
  50. exec $::PROG --changeset changeset.bin $db1 $db2
  51. set bin [readfile changeset.bin]
  52. return $bin
  53. }
  54. proc xConflict {args} {
  55. return ""
  56. }
  57. proc do_changeset_test {tn sql1 sql2} {
  58. forcedelete test.db123 test.db124
  59. sqlite3 db test.db123
  60. db eval $sql1
  61. db close
  62. sqlite3 db test.db124
  63. db eval $sql2
  64. set cs [get_changeset test.db124 test.db123]
  65. sqlite3changeset_apply db $cs xConflict
  66. db close
  67. set database_cksum1 [database_cksum test.db123]
  68. set database_cksum2 [database_cksum test.db124]
  69. uplevel [list \
  70. do_test $tn [list string compare $database_cksum1 $database_cksum2] 0
  71. ]
  72. }
  73. do_changeset_test 1.0 {
  74. CREATE TABLE t1(x PRIMARY KEY);
  75. } {
  76. CREATE TABLE t1(x PRIMARY KEY);
  77. }
  78. do_changeset_test 1.1 {
  79. CREATE TABLE t1(x PRIMARY KEY);
  80. CREATE TABLE t2(x PRIMARY KEY, y);
  81. INSERT INTO t2 VALUES(1, 2);
  82. } {
  83. CREATE TABLE t1(x PRIMARY KEY);
  84. CREATE TABLE t2(x PRIMARY KEY, y);
  85. INSERT INTO t2 VALUES(3, 4);
  86. }
  87. do_changeset_test 1.2 {
  88. CREATE TABLE t2(a, b, c, PRIMARY KEY(b, c));
  89. INSERT INTO t2 VALUES(1, 2, 3);
  90. INSERT INTO t2 VALUES(4, 5, 6);
  91. } {
  92. CREATE TABLE t2(a, b, c, PRIMARY KEY(b, c));
  93. INSERT INTO t2 VALUES(1, 2, 11);
  94. INSERT INTO t2 VALUES(7, 8, 9);
  95. }
  96. finish_test