• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FTS2 module,
12# and then uses them to do some basic tests that FTS2 is internally
13# working as expected.
14#
15# $Id: fts2p.test,v 1.1 2008/07/22 23:32:28 shess Exp $
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# If SQLITE_ENABLE_FTS2 is not defined, omit this file.
22ifcapable !fts2 {
23  finish_test
24  return
25}
26
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.
30db eval {
31  DROP TABLE IF EXISTS t1;
32  CREATE VIRTUAL TABLE t1 USING fts2(c);
33  INSERT INTO t1 (rowid, c) VALUES (1, 'x');
34}
35
36set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1}
37set r {1 {unable to use function dump_terms in the requested context}}
38if {[catchsql $s]==$r} {
39  finish_test
40  return
41}
42
43#*************************************************************************
44# Test that the new functions give appropriate errors.
45do_test fts2p-0.0 {
46  catchsql {
47    SELECT dump_terms(t1, 1) FROM t1 LIMIT 1;
48  }
49} {1 {dump_terms: incorrect arguments}}
50
51do_test fts2p-0.1 {
52  catchsql {
53    SELECT dump_terms(t1, 0, 0, 0) FROM t1 LIMIT 1;
54  }
55} {1 {dump_terms: incorrect arguments}}
56
57do_test fts2p-0.2 {
58  catchsql {
59    SELECT dump_terms(1, t1) FROM t1 LIMIT 1;
60  }
61} {1 {unable to use function dump_terms in the requested context}}
62
63do_test fts2p-0.3 {
64  catchsql {
65    SELECT dump_terms(t1, 16, 16) FROM t1 LIMIT 1;
66  }
67} {1 {dump_terms: segment not found}}
68
69do_test fts2p-0.4 {
70  catchsql {
71    SELECT dump_doclist(t1) FROM t1 LIMIT 1;
72  }
73} {1 {dump_doclist: incorrect arguments}}
74
75do_test fts2p-0.5 {
76  catchsql {
77    SELECT dump_doclist(t1, NULL) FROM t1 LIMIT 1;
78  }
79} {1 {dump_doclist: empty second argument}}
80
81do_test fts2p-0.6 {
82  catchsql {
83    SELECT dump_doclist(t1, '') FROM t1 LIMIT 1;
84  }
85} {1 {dump_doclist: empty second argument}}
86
87do_test fts2p-0.7 {
88  catchsql {
89    SELECT dump_doclist(t1, 'a', 0) FROM t1 LIMIT 1;
90  }
91} {1 {dump_doclist: incorrect arguments}}
92
93do_test fts2p-0.8 {
94  catchsql {
95    SELECT dump_doclist(t1, 'a', 0, 0, 0) FROM t1 LIMIT 1;
96  }
97} {1 {dump_doclist: incorrect arguments}}
98
99do_test fts2p-0.9 {
100  catchsql {
101    SELECT dump_doclist(t1, 'a', 16, 16) FROM t1 LIMIT 1;
102  }
103} {1 {dump_doclist: segment not found}}
104
105#*************************************************************************
106# Utility function to check for the expected terms in the segment
107# level/index.  _all version does same but for entire index.
108proc check_terms {test level index terms} {
109  # TODO(shess): Figure out why uplevel in do_test can't catch
110  # $level and $index directly.
111  set ::level $level
112  set ::index $index
113  do_test $test.terms {
114    execsql {
115      SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1;
116    }
117  } [list $terms]
118}
119proc check_terms_all {test terms} {
120  do_test $test.terms {
121    execsql {
122      SELECT dump_terms(t1) FROM t1 LIMIT 1;
123    }
124  } [list $terms]
125}
126
127# Utility function to check for the expected doclist for the term in
128# segment level/index.  _all version does same for entire index.
129proc check_doclist {test level index term doclist} {
130  # TODO(shess): Again, why can't the non-:: versions work?
131  set ::term $term
132  set ::level $level
133  set ::index $index
134  do_test $test {
135    execsql {
136      SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1;
137    }
138  } [list $doclist]
139}
140proc check_doclist_all {test term doclist} {
141  set ::term $term
142  do_test $test {
143    execsql {
144      SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1;
145    }
146  } [list $doclist]
147}
148
149#*************************************************************************
150# Test the segments resulting from straight-forward inserts.
151db eval {
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');
157}
158
159# Check for expected segments and expected matches.
160do_test fts2p-1.0.segments {
161  execsql {
162    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
163  }
164} {0 0 0 1 0 2}
165do_test fts2p-1.0.matches {
166  execsql {
167    SELECT OFFSETS(t1) FROM t1
168     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
169  }
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}]
173
174# Check the specifics of the segments constructed.
175# Logical view of entire index.
176check_terms_all   fts2p-1.0.1   {a is test that this was}
177check_doclist_all fts2p-1.0.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
178check_doclist_all fts2p-1.0.1.2 is {[1 0[1]] [3 0[1]]}
179check_doclist_all fts2p-1.0.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]}
180check_doclist_all fts2p-1.0.1.4 that {[2 0[0]]}
181check_doclist_all fts2p-1.0.1.5 this {[1 0[0]] [3 0[0]]}
182check_doclist_all fts2p-1.0.1.6 was {[2 0[1]]}
183
184# Segment 0,0
185check_terms   fts2p-1.0.2   0 0 {a is test this}
186check_doclist fts2p-1.0.2.1 0 0 a {[1 0[2]]}
187check_doclist fts2p-1.0.2.2 0 0 is {[1 0[1]]}
188check_doclist fts2p-1.0.2.3 0 0 test {[1 0[3]]}
189check_doclist fts2p-1.0.2.4 0 0 this {[1 0[0]]}
190
191# Segment 0,1
192check_terms   fts2p-1.0.3   0 1 {a test that was}
193check_doclist fts2p-1.0.3.1 0 1 a {[2 0[2]]}
194check_doclist fts2p-1.0.3.2 0 1 test {[2 0[3]]}
195check_doclist fts2p-1.0.3.3 0 1 that {[2 0[0]]}
196check_doclist fts2p-1.0.3.4 0 1 was {[2 0[1]]}
197
198# Segment 0,2
199check_terms   fts2p-1.0.4   0 2 {a is test this}
200check_doclist fts2p-1.0.4.1 0 2 a {[3 0[2]]}
201check_doclist fts2p-1.0.4.2 0 2 is {[3 0[1]]}
202check_doclist fts2p-1.0.4.3 0 2 test {[3 0[3]]}
203check_doclist fts2p-1.0.4.4 0 2 this {[3 0[0]]}
204
205#*************************************************************************
206# Test the segments resulting from inserts followed by a delete.
207db eval {
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;
214}
215
216do_test fts2p-1.1.segments {
217  execsql {
218    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
219  }
220} {0 0 0 1 0 2 0 3}
221do_test fts2p-1.1.matches {
222  execsql {
223    SELECT OFFSETS(t1) FROM t1
224     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
225  }
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}}
227
228check_terms_all fts2p-1.1.1 {a is test that this was}
229check_doclist_all fts2p-1.1.1.1 a {[2 0[2]] [3 0[2]]}
230check_doclist_all fts2p-1.1.1.2 is {[3 0[1]]}
231check_doclist_all fts2p-1.1.1.3 test {[2 0[3]] [3 0[3]]}
232check_doclist_all fts2p-1.1.1.4 that {[2 0[0]]}
233check_doclist_all fts2p-1.1.1.5 this {[3 0[0]]}
234check_doclist_all fts2p-1.1.1.6 was {[2 0[1]]}
235
236check_terms fts2p-1.1.2 0 0 {a is test this}
237check_doclist fts2p-1.1.2.1 0 0 a {[1 0[2]]}
238check_doclist fts2p-1.1.2.2 0 0 is {[1 0[1]]}
239check_doclist fts2p-1.1.2.3 0 0 test {[1 0[3]]}
240check_doclist fts2p-1.1.2.4 0 0 this {[1 0[0]]}
241
242check_terms fts2p-1.1.3 0 1 {a test that was}
243check_doclist fts2p-1.1.3.1 0 1 a {[2 0[2]]}
244check_doclist fts2p-1.1.3.2 0 1 test {[2 0[3]]}
245check_doclist fts2p-1.1.3.3 0 1 that {[2 0[0]]}
246check_doclist fts2p-1.1.3.4 0 1 was {[2 0[1]]}
247
248check_terms fts2p-1.1.4 0 2 {a is test this}
249check_doclist fts2p-1.1.4.1 0 2 a {[3 0[2]]}
250check_doclist fts2p-1.1.4.2 0 2 is {[3 0[1]]}
251check_doclist fts2p-1.1.4.3 0 2 test {[3 0[3]]}
252check_doclist fts2p-1.1.4.4 0 2 this {[3 0[0]]}
253
254check_terms fts2p-1.1.5 0 3 {a is test this}
255check_doclist fts2p-1.1.5.1 0 3 a {[1]}
256check_doclist fts2p-1.1.5.2 0 3 is {[1]}
257check_doclist fts2p-1.1.5.3 0 3 test {[1]}
258check_doclist fts2p-1.1.5.4 0 3 this {[1]}
259
260#*************************************************************************
261# Test results when all references to certain tokens are deleted.
262db eval {
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);
269}
270
271# Still 4 segments because 0,3 will contain deletes for rowid 1 and 3.
272do_test fts2p-1.2.segments {
273  execsql {
274    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
275  }
276} {0 0 0 1 0 2 0 3}
277do_test fts2p-1.2.matches {
278  execsql {
279    SELECT OFFSETS(t1) FROM t1
280     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
281  }
282} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
283
284check_terms_all fts2p-1.2.1 {a is test that this was}
285check_doclist_all fts2p-1.2.1.1 a {[2 0[2]]}
286check_doclist_all fts2p-1.2.1.2 is {}
287check_doclist_all fts2p-1.2.1.3 test {[2 0[3]]}
288check_doclist_all fts2p-1.2.1.4 that {[2 0[0]]}
289check_doclist_all fts2p-1.2.1.5 this {}
290check_doclist_all fts2p-1.2.1.6 was {[2 0[1]]}
291
292check_terms fts2p-1.2.2 0 0 {a is test this}
293check_doclist fts2p-1.2.2.1 0 0 a {[1 0[2]]}
294check_doclist fts2p-1.2.2.2 0 0 is {[1 0[1]]}
295check_doclist fts2p-1.2.2.3 0 0 test {[1 0[3]]}
296check_doclist fts2p-1.2.2.4 0 0 this {[1 0[0]]}
297
298check_terms fts2p-1.2.3 0 1 {a test that was}
299check_doclist fts2p-1.2.3.1 0 1 a {[2 0[2]]}
300check_doclist fts2p-1.2.3.2 0 1 test {[2 0[3]]}
301check_doclist fts2p-1.2.3.3 0 1 that {[2 0[0]]}
302check_doclist fts2p-1.2.3.4 0 1 was {[2 0[1]]}
303
304check_terms fts2p-1.2.4 0 2 {a is test this}
305check_doclist fts2p-1.2.4.1 0 2 a {[3 0[2]]}
306check_doclist fts2p-1.2.4.2 0 2 is {[3 0[1]]}
307check_doclist fts2p-1.2.4.3 0 2 test {[3 0[3]]}
308check_doclist fts2p-1.2.4.4 0 2 this {[3 0[0]]}
309
310check_terms fts2p-1.2.5 0 3 {a is test this}
311check_doclist fts2p-1.2.5.1 0 3 a {[1] [3]}
312check_doclist fts2p-1.2.5.2 0 3 is {[1] [3]}
313check_doclist fts2p-1.2.5.3 0 3 test {[1] [3]}
314check_doclist fts2p-1.2.5.4 0 3 this {[1] [3]}
315
316#*************************************************************************
317# Test results when everything is optimized manually.
318db eval {
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;
329  DROP TABLE t1old;
330}
331
332# Should be a single optimal segment with the same logical results.
333do_test fts2p-1.3.segments {
334  execsql {
335    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
336  }
337} {0 0}
338do_test fts2p-1.3.matches {
339  execsql {
340    SELECT OFFSETS(t1) FROM t1
341     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
342  }
343} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
344
345check_terms_all fts2p-1.3.1 {a test that was}
346check_doclist_all fts2p-1.3.1.1 a {[2 0[2]]}
347check_doclist_all fts2p-1.3.1.2 test {[2 0[3]]}
348check_doclist_all fts2p-1.3.1.3 that {[2 0[0]]}
349check_doclist_all fts2p-1.3.1.4 was {[2 0[1]]}
350
351check_terms fts2p-1.3.2 0 0 {a test that was}
352check_doclist fts2p-1.3.2.1 0 0 a {[2 0[2]]}
353check_doclist fts2p-1.3.2.2 0 0 test {[2 0[3]]}
354check_doclist fts2p-1.3.2.3 0 0 that {[2 0[0]]}
355check_doclist fts2p-1.3.2.4 0 0 was {[2 0[1]]}
356
357finish_test
358