fts4check.test 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # 2012 March 26
  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. The
  12. # focus of this script is testing the FTS 'integrity-check' function,
  13. # used to check if the current FTS index accurately reflects the content
  14. # of the table.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. source $testdir/fts3_common.tcl
  19. set ::testprefix fts4check
  20. # If SQLITE_ENABLE_FTS3 is defined, omit this file.
  21. ifcapable !fts3 {
  22. finish_test
  23. return
  24. }
  25. # Run the integrity-check on FTS table $tbl using database handle $db. If
  26. # the integrity-check passes, return "ok". Otherwise, throw an exception.
  27. #
  28. proc fts_integrity {db tbl} {
  29. $db eval "INSERT INTO $tbl ($tbl) VALUES('integrity-check')"
  30. return "ok"
  31. }
  32. #-------------------------------------------------------------------------
  33. # Test cases 1.*
  34. #
  35. # 1.0: Build a reasonably sized FTS table (5000 rows).
  36. #
  37. # 1.1: Run the integrity check code to check it passes.
  38. #
  39. # 1.2: Make a series of minor changes to the underlying FTS data structures
  40. # (e.g. delete or insert a row from the %_content table). Check that
  41. # this causes the integrity-check code to fail.
  42. #
  43. # Build an FTS table and check the integrity-check passes.
  44. #
  45. do_test 1.0 { fts3_build_db_1 5000 } {}
  46. do_test 1.1 { fts_integrity db t1 } {ok}
  47. # Mess around with the underlying tables. Check that this causes the
  48. # integrity-check test to fail.
  49. #
  50. foreach {tn disruption} {
  51. 1 {
  52. INSERT INTO t1_content(docid, c0x, c1y) VALUES(NULL, 'a', 'b');
  53. }
  54. 2 {
  55. DELETE FROM t1_content WHERE docid = (SELECT max(docid) FROM t1_content);
  56. }
  57. 3 {
  58. DELETE FROM t1_segdir WHERE level=0 AND idx=(
  59. SELECT max(idx) FROM t1_segdir WHERE level=0
  60. );
  61. }
  62. } {
  63. do_execsql_test 1.2.1.$tn "BEGIN; $disruption"
  64. do_catchsql_test 1.2.2.$tn {
  65. INSERT INTO t1 (t1) VALUES('integrity-check')
  66. } {1 {database disk image is malformed}}
  67. do_execsql_test 1.2.3.$tn "ROLLBACK"
  68. }
  69. do_test 1.3 { fts_integrity db t1 } {ok}
  70. #-------------------------------------------------------------------------
  71. # Test cases 2.*
  72. #
  73. # 2.0: Build a reasonably sized FTS table (20000 rows) that includes
  74. # prefix indexes.
  75. #
  76. # 2.1: Run the integrity check code to check it passes.
  77. #
  78. # 2.2: Make a series of minor changes to the underlying FTS data structures
  79. # (e.g. delete or insert a row from the %_content table). Check that
  80. # this causes the integrity-check code to fail.
  81. #
  82. do_test 2.0 { fts3_build_db_2 -extra {prefix="3,1"} 20000 } {}
  83. do_test 2.1 { fts_integrity db t2 } {ok}
  84. foreach {tn disruption} {
  85. 1 {
  86. INSERT INTO t2_content VALUES(NULL, 'xyz')
  87. }
  88. 3 {
  89. DELETE FROM t2_segdir WHERE level=0 AND idx=(
  90. SELECT max(idx) FROM t2_segdir WHERE level=1024
  91. );
  92. }
  93. } {
  94. do_execsql_test 2.2.1.$tn "BEGIN; $disruption"
  95. do_catchsql_test 2.2.2.$tn {
  96. INSERT INTO t2 (t2) VALUES('integrity-check')
  97. } {1 {database disk image is malformed}}
  98. do_execsql_test 2.2.3.$tn "ROLLBACK"
  99. }
  100. #-------------------------------------------------------------------------
  101. # Test cases 3.*
  102. #
  103. # 3.0: Build a reasonably sized FTS table (5000 rows) that includes
  104. # prefix indexes and uses the languageid= feature.
  105. #
  106. # 3.1: Run the integrity check code to check it passes.
  107. #
  108. # 3.2: Make a series of minor changes to the underlying FTS data structures
  109. # (e.g. delete or insert a row from the %_content table). Check that
  110. # this causes the integrity-check code to fail.
  111. #
  112. do_test 3.0 {
  113. reset_db
  114. fts3_build_db_1 5000
  115. execsql {
  116. CREATE VIRTUAL TABLE t3 USING fts4(x, y, prefix="2,3", languageid=langid);
  117. }
  118. foreach docid [execsql {SELECT docid FROM t1 ORDER BY 1 ASC}] {
  119. execsql {
  120. INSERT INTO t3(x, y, langid)
  121. SELECT x, y, (docid%9)*4 FROM t1 WHERE docid=$docid;
  122. }
  123. }
  124. } {}
  125. do_test 3.1 { fts_integrity db t3 } {ok}
  126. foreach {tn disruption} {
  127. 1 {
  128. INSERT INTO t3_content(c0x, c1y, langid) VALUES(NULL, 'a', 0);
  129. }
  130. 2 {
  131. UPDATE t3_content SET langid=langid+1 WHERE rowid = (
  132. SELECT max(rowid) FROM t3_content
  133. )
  134. }
  135. } {
  136. do_execsql_test 3.2.1.$tn "BEGIN; $disruption"
  137. do_catchsql_test 3.2.2.$tn {
  138. INSERT INTO t3 (t3) VALUES('integrity-check')
  139. } {1 {database disk image is malformed}}
  140. do_execsql_test 3.2.3.$tn "ROLLBACK"
  141. }
  142. finish_test