3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #*************************************************************************
11 # This file exercises some new testing functions in the FTS2 module,
12 # and then uses them to do some basic tests that FTS2 is internally
13 # working as expected.
15 # $Id: fts2p.test,v 1.1 2008/07/22 23:32:28 shess Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # If SQLITE_ENABLE_FTS2 is not defined, omit this file.
27 #*************************************************************************
28 # Probe to see if support for these functions is compiled in.
29 # TODO(shess): Change main.mk to do the right thing and remove this test.
31 DROP TABLE IF EXISTS t1;
32 CREATE VIRTUAL TABLE t1 USING fts2(c);
33 INSERT INTO t1 (rowid, c) VALUES (1, 'x');
36 set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1}
37 set r {1 {unable to use function dump_terms in the requested context}}
38 if {[catchsql $s]==$r} {
43 #*************************************************************************
44 # Test that the new functions give appropriate errors.
47 SELECT dump_terms(t1, 1) FROM t1 LIMIT 1;
49 } {1 {dump_terms: incorrect arguments}}
53 SELECT dump_terms(t1, 0, 0, 0) FROM t1 LIMIT 1;
55 } {1 {dump_terms: incorrect arguments}}
59 SELECT dump_terms(1, t1) FROM t1 LIMIT 1;
61 } {1 {unable to use function dump_terms in the requested context}}
65 SELECT dump_terms(t1, 16, 16) FROM t1 LIMIT 1;
67 } {1 {dump_terms: segment not found}}
71 SELECT dump_doclist(t1) FROM t1 LIMIT 1;
73 } {1 {dump_doclist: incorrect arguments}}
77 SELECT dump_doclist(t1, NULL) FROM t1 LIMIT 1;
79 } {1 {dump_doclist: empty second argument}}
83 SELECT dump_doclist(t1, '') FROM t1 LIMIT 1;
85 } {1 {dump_doclist: empty second argument}}
89 SELECT dump_doclist(t1, 'a', 0) FROM t1 LIMIT 1;
91 } {1 {dump_doclist: incorrect arguments}}
95 SELECT dump_doclist(t1, 'a', 0, 0, 0) FROM t1 LIMIT 1;
97 } {1 {dump_doclist: incorrect arguments}}
101 SELECT dump_doclist(t1, 'a', 16, 16) FROM t1 LIMIT 1;
103 } {1 {dump_doclist: segment not found}}
105 #*************************************************************************
106 # Utility function to check for the expected terms in the segment
107 # level/index. _all version does same but for entire index.
108 proc check_terms {test level index terms} {
109 # TODO(shess): Figure out why uplevel in do_test can't catch
110 # $level and $index directly.
113 do_test $test.terms {
115 SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1;
119 proc check_terms_all {test terms} {
120 do_test $test.terms {
122 SELECT dump_terms(t1) FROM t1 LIMIT 1;
127 # Utility function to check for the expected doclist for the term in
128 # segment level/index. _all version does same for entire index.
129 proc check_doclist {test level index term doclist} {
130 # TODO(shess): Again, why can't the non-:: versions work?
136 SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1;
140 proc check_doclist_all {test term doclist} {
144 SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1;
149 #*************************************************************************
150 # Test the segments resulting from straight-forward inserts.
152 DROP TABLE IF EXISTS t1;
153 CREATE VIRTUAL TABLE t1 USING fts2(c);
154 INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
155 INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
156 INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
159 # Check for expected segments and expected matches.
160 do_test fts2p-1.0.segments {
162 SELECT level, idx FROM t1_segdir ORDER BY level, idx;
165 do_test fts2p-1.0.matches {
167 SELECT OFFSETS(t1) FROM t1
168 WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
170 } [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
171 {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
172 {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
174 # Check the specifics of the segments constructed.
175 # Logical view of entire index.
176 check_terms_all fts2p-1.0.1 {a is test that this was}
177 check_doclist_all fts2p-1.0.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
178 check_doclist_all fts2p-1.0.1.2 is {[1 0[1]] [3 0[1]]}
179 check_doclist_all fts2p-1.0.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]}
180 check_doclist_all fts2p-1.0.1.4 that {[2 0[0]]}
181 check_doclist_all fts2p-1.0.1.5 this {[1 0[0]] [3 0[0]]}
182 check_doclist_all fts2p-1.0.1.6 was {[2 0[1]]}
185 check_terms fts2p-1.0.2 0 0 {a is test this}
186 check_doclist fts2p-1.0.2.1 0 0 a {[1 0[2]]}
187 check_doclist fts2p-1.0.2.2 0 0 is {[1 0[1]]}
188 check_doclist fts2p-1.0.2.3 0 0 test {[1 0[3]]}
189 check_doclist fts2p-1.0.2.4 0 0 this {[1 0[0]]}
192 check_terms fts2p-1.0.3 0 1 {a test that was}
193 check_doclist fts2p-1.0.3.1 0 1 a {[2 0[2]]}
194 check_doclist fts2p-1.0.3.2 0 1 test {[2 0[3]]}
195 check_doclist fts2p-1.0.3.3 0 1 that {[2 0[0]]}
196 check_doclist fts2p-1.0.3.4 0 1 was {[2 0[1]]}
199 check_terms fts2p-1.0.4 0 2 {a is test this}
200 check_doclist fts2p-1.0.4.1 0 2 a {[3 0[2]]}
201 check_doclist fts2p-1.0.4.2 0 2 is {[3 0[1]]}
202 check_doclist fts2p-1.0.4.3 0 2 test {[3 0[3]]}
203 check_doclist fts2p-1.0.4.4 0 2 this {[3 0[0]]}
205 #*************************************************************************
206 # Test the segments resulting from inserts followed by a delete.
208 DROP TABLE IF EXISTS t1;
209 CREATE VIRTUAL TABLE t1 USING fts2(c);
210 INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
211 INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
212 INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
213 DELETE FROM t1 WHERE rowid = 1;
216 do_test fts2p-1.1.segments {
218 SELECT level, idx FROM t1_segdir ORDER BY level, idx;
221 do_test fts2p-1.1.matches {
223 SELECT OFFSETS(t1) FROM t1
224 WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
226 } {{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}}
228 check_terms_all fts2p-1.1.1 {a is test that this was}
229 check_doclist_all fts2p-1.1.1.1 a {[2 0[2]] [3 0[2]]}
230 check_doclist_all fts2p-1.1.1.2 is {[3 0[1]]}
231 check_doclist_all fts2p-1.1.1.3 test {[2 0[3]] [3 0[3]]}
232 check_doclist_all fts2p-1.1.1.4 that {[2 0[0]]}
233 check_doclist_all fts2p-1.1.1.5 this {[3 0[0]]}
234 check_doclist_all fts2p-1.1.1.6 was {[2 0[1]]}
236 check_terms fts2p-1.1.2 0 0 {a is test this}
237 check_doclist fts2p-1.1.2.1 0 0 a {[1 0[2]]}
238 check_doclist fts2p-1.1.2.2 0 0 is {[1 0[1]]}
239 check_doclist fts2p-1.1.2.3 0 0 test {[1 0[3]]}
240 check_doclist fts2p-1.1.2.4 0 0 this {[1 0[0]]}
242 check_terms fts2p-1.1.3 0 1 {a test that was}
243 check_doclist fts2p-1.1.3.1 0 1 a {[2 0[2]]}
244 check_doclist fts2p-1.1.3.2 0 1 test {[2 0[3]]}
245 check_doclist fts2p-1.1.3.3 0 1 that {[2 0[0]]}
246 check_doclist fts2p-1.1.3.4 0 1 was {[2 0[1]]}
248 check_terms fts2p-1.1.4 0 2 {a is test this}
249 check_doclist fts2p-1.1.4.1 0 2 a {[3 0[2]]}
250 check_doclist fts2p-1.1.4.2 0 2 is {[3 0[1]]}
251 check_doclist fts2p-1.1.4.3 0 2 test {[3 0[3]]}
252 check_doclist fts2p-1.1.4.4 0 2 this {[3 0[0]]}
254 check_terms fts2p-1.1.5 0 3 {a is test this}
255 check_doclist fts2p-1.1.5.1 0 3 a {[1]}
256 check_doclist fts2p-1.1.5.2 0 3 is {[1]}
257 check_doclist fts2p-1.1.5.3 0 3 test {[1]}
258 check_doclist fts2p-1.1.5.4 0 3 this {[1]}
260 #*************************************************************************
261 # Test results when all references to certain tokens are deleted.
263 DROP TABLE IF EXISTS t1;
264 CREATE VIRTUAL TABLE t1 USING fts2(c);
265 INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
266 INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
267 INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
268 DELETE FROM t1 WHERE rowid IN (1,3);
271 # Still 4 segments because 0,3 will contain deletes for rowid 1 and 3.
272 do_test fts2p-1.2.segments {
274 SELECT level, idx FROM t1_segdir ORDER BY level, idx;
277 do_test fts2p-1.2.matches {
279 SELECT OFFSETS(t1) FROM t1
280 WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
282 } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
284 check_terms_all fts2p-1.2.1 {a is test that this was}
285 check_doclist_all fts2p-1.2.1.1 a {[2 0[2]]}
286 check_doclist_all fts2p-1.2.1.2 is {}
287 check_doclist_all fts2p-1.2.1.3 test {[2 0[3]]}
288 check_doclist_all fts2p-1.2.1.4 that {[2 0[0]]}
289 check_doclist_all fts2p-1.2.1.5 this {}
290 check_doclist_all fts2p-1.2.1.6 was {[2 0[1]]}
292 check_terms fts2p-1.2.2 0 0 {a is test this}
293 check_doclist fts2p-1.2.2.1 0 0 a {[1 0[2]]}
294 check_doclist fts2p-1.2.2.2 0 0 is {[1 0[1]]}
295 check_doclist fts2p-1.2.2.3 0 0 test {[1 0[3]]}
296 check_doclist fts2p-1.2.2.4 0 0 this {[1 0[0]]}
298 check_terms fts2p-1.2.3 0 1 {a test that was}
299 check_doclist fts2p-1.2.3.1 0 1 a {[2 0[2]]}
300 check_doclist fts2p-1.2.3.2 0 1 test {[2 0[3]]}
301 check_doclist fts2p-1.2.3.3 0 1 that {[2 0[0]]}
302 check_doclist fts2p-1.2.3.4 0 1 was {[2 0[1]]}
304 check_terms fts2p-1.2.4 0 2 {a is test this}
305 check_doclist fts2p-1.2.4.1 0 2 a {[3 0[2]]}
306 check_doclist fts2p-1.2.4.2 0 2 is {[3 0[1]]}
307 check_doclist fts2p-1.2.4.3 0 2 test {[3 0[3]]}
308 check_doclist fts2p-1.2.4.4 0 2 this {[3 0[0]]}
310 check_terms fts2p-1.2.5 0 3 {a is test this}
311 check_doclist fts2p-1.2.5.1 0 3 a {[1] [3]}
312 check_doclist fts2p-1.2.5.2 0 3 is {[1] [3]}
313 check_doclist fts2p-1.2.5.3 0 3 test {[1] [3]}
314 check_doclist fts2p-1.2.5.4 0 3 this {[1] [3]}
316 #*************************************************************************
317 # Test results when everything is optimized manually.
319 DROP TABLE IF EXISTS t1;
320 CREATE VIRTUAL TABLE t1 USING fts2(c);
321 INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
322 INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
323 INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
324 DELETE FROM t1 WHERE rowid IN (1,3);
325 DROP TABLE IF EXISTS t1old;
326 ALTER TABLE t1 RENAME TO t1old;
327 CREATE VIRTUAL TABLE t1 USING fts2(c);
328 INSERT INTO t1 (rowid, c) SELECT rowid, c FROM t1old;
332 # Should be a single optimal segment with the same logical results.
333 do_test fts2p-1.3.segments {
335 SELECT level, idx FROM t1_segdir ORDER BY level, idx;
338 do_test fts2p-1.3.matches {
340 SELECT OFFSETS(t1) FROM t1
341 WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
343 } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
345 check_terms_all fts2p-1.3.1 {a test that was}
346 check_doclist_all fts2p-1.3.1.1 a {[2 0[2]]}
347 check_doclist_all fts2p-1.3.1.2 test {[2 0[3]]}
348 check_doclist_all fts2p-1.3.1.3 that {[2 0[0]]}
349 check_doclist_all fts2p-1.3.1.4 was {[2 0[1]]}
351 check_terms fts2p-1.3.2 0 0 {a test that was}
352 check_doclist fts2p-1.3.2.1 0 0 a {[2 0[2]]}
353 check_doclist fts2p-1.3.2.2 0 0 test {[2 0[3]]}
354 check_doclist fts2p-1.3.2.3 0 0 that {[2 0[0]]}
355 check_doclist fts2p-1.3.2.4 0 0 was {[2 0[1]]}