1
0

fts3c.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # 2008 June 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 exercises some new testing functions in the FTS3 module,
  12. # and then uses them to do some basic tests that FTS3 is internally
  13. # working as expected.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. source $testdir/fts3_common.tcl
  18. # If SQLITE_ENABLE_FTS3 is not defined, omit this file.
  19. ifcapable !fts3 {
  20. finish_test
  21. return
  22. }
  23. #*************************************************************************
  24. # Utility function to check for the expected terms in the segment
  25. # level/index. _all version does same but for entire index.
  26. proc check_terms {test level index terms} {
  27. set where "level = $level AND idx = $index"
  28. do_test $test.terms [list fts3_terms t1 $where] $terms
  29. }
  30. proc check_terms_all {test terms} {
  31. do_test $test.terms [list fts3_terms t1 1] $terms
  32. }
  33. # Utility function to check for the expected doclist for the term in
  34. # segment level/index. _all version does same for entire index.
  35. proc check_doclist {test level index term doclist} {
  36. set where "level = $level AND idx = $index"
  37. do_test $test [list fts3_doclist t1 $term $where] $doclist
  38. }
  39. proc check_doclist_all {test term doclist} {
  40. do_test $test [list fts3_doclist t1 $term 1] $doclist
  41. }
  42. #*************************************************************************
  43. # Test the segments resulting from straight-forward inserts.
  44. db eval {
  45. DROP TABLE IF EXISTS t1;
  46. CREATE VIRTUAL TABLE t1 USING fts3(c);
  47. INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
  48. INSERT INTO t1 (docid, c) VALUES (2, 'That was a test');
  49. INSERT INTO t1 (docid, c) VALUES (3, 'This is a test');
  50. }
  51. # Check for expected segments and expected matches.
  52. do_test fts3c-1.0.segments {
  53. execsql {
  54. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  55. }
  56. } {0 0 0 1 0 2}
  57. do_test fts3c-1.0.matches {
  58. execsql {
  59. SELECT OFFSETS(t1) FROM t1
  60. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
  61. }
  62. } [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
  63. {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
  64. {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
  65. # Check the specifics of the segments constructed.
  66. # Logical view of entire index.
  67. check_terms_all fts3c-1.0.1 {a is test that this was}
  68. check_doclist_all fts3c-1.0.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
  69. check_doclist_all fts3c-1.0.1.2 is {[1 0[1]] [3 0[1]]}
  70. check_doclist_all fts3c-1.0.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]}
  71. check_doclist_all fts3c-1.0.1.4 that {[2 0[0]]}
  72. check_doclist_all fts3c-1.0.1.5 this {[1 0[0]] [3 0[0]]}
  73. check_doclist_all fts3c-1.0.1.6 was {[2 0[1]]}
  74. # Segment 0,0
  75. check_terms fts3c-1.0.2 0 0 {a is test this}
  76. check_doclist fts3c-1.0.2.1 0 0 a {[1 0[2]]}
  77. check_doclist fts3c-1.0.2.2 0 0 is {[1 0[1]]}
  78. check_doclist fts3c-1.0.2.3 0 0 test {[1 0[3]]}
  79. check_doclist fts3c-1.0.2.4 0 0 this {[1 0[0]]}
  80. # Segment 0,1
  81. check_terms fts3c-1.0.3 0 1 {a test that was}
  82. check_doclist fts3c-1.0.3.1 0 1 a {[2 0[2]]}
  83. check_doclist fts3c-1.0.3.2 0 1 test {[2 0[3]]}
  84. check_doclist fts3c-1.0.3.3 0 1 that {[2 0[0]]}
  85. check_doclist fts3c-1.0.3.4 0 1 was {[2 0[1]]}
  86. # Segment 0,2
  87. check_terms fts3c-1.0.4 0 2 {a is test this}
  88. check_doclist fts3c-1.0.4.1 0 2 a {[3 0[2]]}
  89. check_doclist fts3c-1.0.4.2 0 2 is {[3 0[1]]}
  90. check_doclist fts3c-1.0.4.3 0 2 test {[3 0[3]]}
  91. check_doclist fts3c-1.0.4.4 0 2 this {[3 0[0]]}
  92. #*************************************************************************
  93. # Test the segments resulting from inserts followed by a delete.
  94. db eval {
  95. DROP TABLE IF EXISTS t1;
  96. CREATE VIRTUAL TABLE t1 USING fts3(c);
  97. INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
  98. INSERT INTO t1 (docid, c) VALUES (2, 'That was a test');
  99. INSERT INTO t1 (docid, c) VALUES (3, 'This is a test');
  100. DELETE FROM t1 WHERE docid = 1;
  101. }
  102. do_test fts3c-1.1.segments {
  103. execsql {
  104. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  105. }
  106. } {0 0 0 1 0 2 0 3}
  107. do_test fts3c-1.1.matches {
  108. execsql {
  109. SELECT OFFSETS(t1) FROM t1
  110. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
  111. }
  112. } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}}
  113. check_terms_all fts3c-1.1.1 {a is test that this was}
  114. check_doclist_all fts3c-1.1.1.1 a {[2 0[2]] [3 0[2]]}
  115. check_doclist_all fts3c-1.1.1.2 is {[3 0[1]]}
  116. check_doclist_all fts3c-1.1.1.3 test {[2 0[3]] [3 0[3]]}
  117. check_doclist_all fts3c-1.1.1.4 that {[2 0[0]]}
  118. check_doclist_all fts3c-1.1.1.5 this {[3 0[0]]}
  119. check_doclist_all fts3c-1.1.1.6 was {[2 0[1]]}
  120. check_terms fts3c-1.1.2 0 0 {a is test this}
  121. check_doclist fts3c-1.1.2.1 0 0 a {[1 0[2]]}
  122. check_doclist fts3c-1.1.2.2 0 0 is {[1 0[1]]}
  123. check_doclist fts3c-1.1.2.3 0 0 test {[1 0[3]]}
  124. check_doclist fts3c-1.1.2.4 0 0 this {[1 0[0]]}
  125. check_terms fts3c-1.1.3 0 1 {a test that was}
  126. check_doclist fts3c-1.1.3.1 0 1 a {[2 0[2]]}
  127. check_doclist fts3c-1.1.3.2 0 1 test {[2 0[3]]}
  128. check_doclist fts3c-1.1.3.3 0 1 that {[2 0[0]]}
  129. check_doclist fts3c-1.1.3.4 0 1 was {[2 0[1]]}
  130. check_terms fts3c-1.1.4 0 2 {a is test this}
  131. check_doclist fts3c-1.1.4.1 0 2 a {[3 0[2]]}
  132. check_doclist fts3c-1.1.4.2 0 2 is {[3 0[1]]}
  133. check_doclist fts3c-1.1.4.3 0 2 test {[3 0[3]]}
  134. check_doclist fts3c-1.1.4.4 0 2 this {[3 0[0]]}
  135. check_terms fts3c-1.1.5 0 3 {a is test this}
  136. check_doclist fts3c-1.1.5.1 0 3 a {[1]}
  137. check_doclist fts3c-1.1.5.2 0 3 is {[1]}
  138. check_doclist fts3c-1.1.5.3 0 3 test {[1]}
  139. check_doclist fts3c-1.1.5.4 0 3 this {[1]}
  140. #*************************************************************************
  141. # Test results when all references to certain tokens are deleted.
  142. db eval {
  143. DROP TABLE IF EXISTS t1;
  144. CREATE VIRTUAL TABLE t1 USING fts3(c);
  145. INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
  146. INSERT INTO t1 (docid, c) VALUES (2, 'That was a test');
  147. INSERT INTO t1 (docid, c) VALUES (3, 'This is a test');
  148. DELETE FROM t1 WHERE docid IN (1,3);
  149. }
  150. # Still 4 segments because 0,3 will contain deletes for docid 1 and 3.
  151. do_test fts3c-1.2.segments {
  152. execsql {
  153. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  154. }
  155. } {0 0 0 1 0 2 0 3}
  156. do_test fts3c-1.2.matches {
  157. execsql {
  158. SELECT OFFSETS(t1) FROM t1
  159. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
  160. }
  161. } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
  162. check_terms_all fts3c-1.2.1 {a is test that this was}
  163. check_doclist_all fts3c-1.2.1.1 a {[2 0[2]]}
  164. check_doclist_all fts3c-1.2.1.2 is {}
  165. check_doclist_all fts3c-1.2.1.3 test {[2 0[3]]}
  166. check_doclist_all fts3c-1.2.1.4 that {[2 0[0]]}
  167. check_doclist_all fts3c-1.2.1.5 this {}
  168. check_doclist_all fts3c-1.2.1.6 was {[2 0[1]]}
  169. check_terms fts3c-1.2.2 0 0 {a is test this}
  170. check_doclist fts3c-1.2.2.1 0 0 a {[1 0[2]]}
  171. check_doclist fts3c-1.2.2.2 0 0 is {[1 0[1]]}
  172. check_doclist fts3c-1.2.2.3 0 0 test {[1 0[3]]}
  173. check_doclist fts3c-1.2.2.4 0 0 this {[1 0[0]]}
  174. check_terms fts3c-1.2.3 0 1 {a test that was}
  175. check_doclist fts3c-1.2.3.1 0 1 a {[2 0[2]]}
  176. check_doclist fts3c-1.2.3.2 0 1 test {[2 0[3]]}
  177. check_doclist fts3c-1.2.3.3 0 1 that {[2 0[0]]}
  178. check_doclist fts3c-1.2.3.4 0 1 was {[2 0[1]]}
  179. check_terms fts3c-1.2.4 0 2 {a is test this}
  180. check_doclist fts3c-1.2.4.1 0 2 a {[3 0[2]]}
  181. check_doclist fts3c-1.2.4.2 0 2 is {[3 0[1]]}
  182. check_doclist fts3c-1.2.4.3 0 2 test {[3 0[3]]}
  183. check_doclist fts3c-1.2.4.4 0 2 this {[3 0[0]]}
  184. check_terms fts3c-1.2.5 0 3 {a is test this}
  185. check_doclist fts3c-1.2.5.1 0 3 a {[1] [3]}
  186. check_doclist fts3c-1.2.5.2 0 3 is {[1] [3]}
  187. check_doclist fts3c-1.2.5.3 0 3 test {[1] [3]}
  188. check_doclist fts3c-1.2.5.4 0 3 this {[1] [3]}
  189. #*************************************************************************
  190. # Test results when everything is optimized manually.
  191. db eval {
  192. DROP TABLE IF EXISTS t1;
  193. CREATE VIRTUAL TABLE t1 USING fts3(c);
  194. INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
  195. INSERT INTO t1 (docid, c) VALUES (2, 'That was a test');
  196. INSERT INTO t1 (docid, c) VALUES (3, 'This is a test');
  197. DELETE FROM t1 WHERE docid IN (1,3);
  198. DROP TABLE IF EXISTS t1old;
  199. ALTER TABLE t1 RENAME TO t1old;
  200. CREATE VIRTUAL TABLE t1 USING fts3(c);
  201. INSERT INTO t1 (docid, c) SELECT docid, c FROM t1old;
  202. DROP TABLE t1old;
  203. }
  204. # Should be a single optimal segment with the same logical results.
  205. do_test fts3c-1.3.segments {
  206. execsql {
  207. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  208. }
  209. } {0 0}
  210. do_test fts3c-1.3.matches {
  211. execsql {
  212. SELECT OFFSETS(t1) FROM t1
  213. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
  214. }
  215. } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
  216. check_terms_all fts3c-1.3.1 {a test that was}
  217. check_doclist_all fts3c-1.3.1.1 a {[2 0[2]]}
  218. check_doclist_all fts3c-1.3.1.2 test {[2 0[3]]}
  219. check_doclist_all fts3c-1.3.1.3 that {[2 0[0]]}
  220. check_doclist_all fts3c-1.3.1.4 was {[2 0[1]]}
  221. check_terms fts3c-1.3.2 0 0 {a test that was}
  222. check_doclist fts3c-1.3.2.1 0 0 a {[2 0[2]]}
  223. check_doclist fts3c-1.3.2.2 0 0 test {[2 0[3]]}
  224. check_doclist fts3c-1.3.2.3 0 0 that {[2 0[0]]}
  225. check_doclist fts3c-1.3.2.4 0 0 was {[2 0[1]]}
  226. finish_test