sl@0: # 2008 June 26 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #************************************************************************* sl@0: # This file exercises some new testing functions in the FTS2 module, sl@0: # and then uses them to do some basic tests that FTS2 is internally sl@0: # working as expected. sl@0: # sl@0: # $Id: fts2p.test,v 1.1 2008/07/22 23:32:28 shess Exp $ sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # If SQLITE_ENABLE_FTS2 is not defined, omit this file. sl@0: ifcapable !fts2 { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: #************************************************************************* sl@0: # Probe to see if support for these functions is compiled in. sl@0: # TODO(shess): Change main.mk to do the right thing and remove this test. sl@0: db eval { sl@0: DROP TABLE IF EXISTS t1; sl@0: CREATE VIRTUAL TABLE t1 USING fts2(c); sl@0: INSERT INTO t1 (rowid, c) VALUES (1, 'x'); sl@0: } sl@0: sl@0: set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1} sl@0: set r {1 {unable to use function dump_terms in the requested context}} sl@0: if {[catchsql $s]==$r} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: #************************************************************************* sl@0: # Test that the new functions give appropriate errors. sl@0: do_test fts2p-0.0 { sl@0: catchsql { sl@0: SELECT dump_terms(t1, 1) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_terms: incorrect arguments}} sl@0: sl@0: do_test fts2p-0.1 { sl@0: catchsql { sl@0: SELECT dump_terms(t1, 0, 0, 0) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_terms: incorrect arguments}} sl@0: sl@0: do_test fts2p-0.2 { sl@0: catchsql { sl@0: SELECT dump_terms(1, t1) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {unable to use function dump_terms in the requested context}} sl@0: sl@0: do_test fts2p-0.3 { sl@0: catchsql { sl@0: SELECT dump_terms(t1, 16, 16) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_terms: segment not found}} sl@0: sl@0: do_test fts2p-0.4 { sl@0: catchsql { sl@0: SELECT dump_doclist(t1) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_doclist: incorrect arguments}} sl@0: sl@0: do_test fts2p-0.5 { sl@0: catchsql { sl@0: SELECT dump_doclist(t1, NULL) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_doclist: empty second argument}} sl@0: sl@0: do_test fts2p-0.6 { sl@0: catchsql { sl@0: SELECT dump_doclist(t1, '') FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_doclist: empty second argument}} sl@0: sl@0: do_test fts2p-0.7 { sl@0: catchsql { sl@0: SELECT dump_doclist(t1, 'a', 0) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_doclist: incorrect arguments}} sl@0: sl@0: do_test fts2p-0.8 { sl@0: catchsql { sl@0: SELECT dump_doclist(t1, 'a', 0, 0, 0) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_doclist: incorrect arguments}} sl@0: sl@0: do_test fts2p-0.9 { sl@0: catchsql { sl@0: SELECT dump_doclist(t1, 'a', 16, 16) FROM t1 LIMIT 1; sl@0: } sl@0: } {1 {dump_doclist: segment not found}} sl@0: sl@0: #************************************************************************* sl@0: # Utility function to check for the expected terms in the segment sl@0: # level/index. _all version does same but for entire index. sl@0: proc check_terms {test level index terms} { sl@0: # TODO(shess): Figure out why uplevel in do_test can't catch sl@0: # $level and $index directly. sl@0: set ::level $level sl@0: set ::index $index sl@0: do_test $test.terms { sl@0: execsql { sl@0: SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1; sl@0: } sl@0: } [list $terms] sl@0: } sl@0: proc check_terms_all {test terms} { sl@0: do_test $test.terms { sl@0: execsql { sl@0: SELECT dump_terms(t1) FROM t1 LIMIT 1; sl@0: } sl@0: } [list $terms] sl@0: } sl@0: sl@0: # Utility function to check for the expected doclist for the term in sl@0: # segment level/index. _all version does same for entire index. sl@0: proc check_doclist {test level index term doclist} { sl@0: # TODO(shess): Again, why can't the non-:: versions work? sl@0: set ::term $term sl@0: set ::level $level sl@0: set ::index $index sl@0: do_test $test { sl@0: execsql { sl@0: SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1; sl@0: } sl@0: } [list $doclist] sl@0: } sl@0: proc check_doclist_all {test term doclist} { sl@0: set ::term $term sl@0: do_test $test { sl@0: execsql { sl@0: SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1; sl@0: } sl@0: } [list $doclist] sl@0: } sl@0: sl@0: #************************************************************************* sl@0: # Test the segments resulting from straight-forward inserts. sl@0: db eval { sl@0: DROP TABLE IF EXISTS t1; sl@0: CREATE VIRTUAL TABLE t1 USING fts2(c); sl@0: INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); sl@0: } sl@0: sl@0: # Check for expected segments and expected matches. sl@0: do_test fts2p-1.0.segments { sl@0: execsql { sl@0: SELECT level, idx FROM t1_segdir ORDER BY level, idx; sl@0: } sl@0: } {0 0 0 1 0 2} sl@0: do_test fts2p-1.0.matches { sl@0: execsql { sl@0: SELECT OFFSETS(t1) FROM t1 sl@0: WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; sl@0: } sl@0: } [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \ sl@0: {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \ sl@0: {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}] sl@0: sl@0: # Check the specifics of the segments constructed. sl@0: # Logical view of entire index. sl@0: check_terms_all fts2p-1.0.1 {a is test that this was} sl@0: check_doclist_all fts2p-1.0.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]} sl@0: check_doclist_all fts2p-1.0.1.2 is {[1 0[1]] [3 0[1]]} sl@0: check_doclist_all fts2p-1.0.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]} sl@0: check_doclist_all fts2p-1.0.1.4 that {[2 0[0]]} sl@0: check_doclist_all fts2p-1.0.1.5 this {[1 0[0]] [3 0[0]]} sl@0: check_doclist_all fts2p-1.0.1.6 was {[2 0[1]]} sl@0: sl@0: # Segment 0,0 sl@0: check_terms fts2p-1.0.2 0 0 {a is test this} sl@0: check_doclist fts2p-1.0.2.1 0 0 a {[1 0[2]]} sl@0: check_doclist fts2p-1.0.2.2 0 0 is {[1 0[1]]} sl@0: check_doclist fts2p-1.0.2.3 0 0 test {[1 0[3]]} sl@0: check_doclist fts2p-1.0.2.4 0 0 this {[1 0[0]]} sl@0: sl@0: # Segment 0,1 sl@0: check_terms fts2p-1.0.3 0 1 {a test that was} sl@0: check_doclist fts2p-1.0.3.1 0 1 a {[2 0[2]]} sl@0: check_doclist fts2p-1.0.3.2 0 1 test {[2 0[3]]} sl@0: check_doclist fts2p-1.0.3.3 0 1 that {[2 0[0]]} sl@0: check_doclist fts2p-1.0.3.4 0 1 was {[2 0[1]]} sl@0: sl@0: # Segment 0,2 sl@0: check_terms fts2p-1.0.4 0 2 {a is test this} sl@0: check_doclist fts2p-1.0.4.1 0 2 a {[3 0[2]]} sl@0: check_doclist fts2p-1.0.4.2 0 2 is {[3 0[1]]} sl@0: check_doclist fts2p-1.0.4.3 0 2 test {[3 0[3]]} sl@0: check_doclist fts2p-1.0.4.4 0 2 this {[3 0[0]]} sl@0: sl@0: #************************************************************************* sl@0: # Test the segments resulting from inserts followed by a delete. sl@0: db eval { sl@0: DROP TABLE IF EXISTS t1; sl@0: CREATE VIRTUAL TABLE t1 USING fts2(c); sl@0: INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); sl@0: DELETE FROM t1 WHERE rowid = 1; sl@0: } sl@0: sl@0: do_test fts2p-1.1.segments { sl@0: execsql { sl@0: SELECT level, idx FROM t1_segdir ORDER BY level, idx; sl@0: } sl@0: } {0 0 0 1 0 2 0 3} sl@0: do_test fts2p-1.1.matches { sl@0: execsql { sl@0: SELECT OFFSETS(t1) FROM t1 sl@0: WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; sl@0: } sl@0: } {{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}} sl@0: sl@0: check_terms_all fts2p-1.1.1 {a is test that this was} sl@0: check_doclist_all fts2p-1.1.1.1 a {[2 0[2]] [3 0[2]]} sl@0: check_doclist_all fts2p-1.1.1.2 is {[3 0[1]]} sl@0: check_doclist_all fts2p-1.1.1.3 test {[2 0[3]] [3 0[3]]} sl@0: check_doclist_all fts2p-1.1.1.4 that {[2 0[0]]} sl@0: check_doclist_all fts2p-1.1.1.5 this {[3 0[0]]} sl@0: check_doclist_all fts2p-1.1.1.6 was {[2 0[1]]} sl@0: sl@0: check_terms fts2p-1.1.2 0 0 {a is test this} sl@0: check_doclist fts2p-1.1.2.1 0 0 a {[1 0[2]]} sl@0: check_doclist fts2p-1.1.2.2 0 0 is {[1 0[1]]} sl@0: check_doclist fts2p-1.1.2.3 0 0 test {[1 0[3]]} sl@0: check_doclist fts2p-1.1.2.4 0 0 this {[1 0[0]]} sl@0: sl@0: check_terms fts2p-1.1.3 0 1 {a test that was} sl@0: check_doclist fts2p-1.1.3.1 0 1 a {[2 0[2]]} sl@0: check_doclist fts2p-1.1.3.2 0 1 test {[2 0[3]]} sl@0: check_doclist fts2p-1.1.3.3 0 1 that {[2 0[0]]} sl@0: check_doclist fts2p-1.1.3.4 0 1 was {[2 0[1]]} sl@0: sl@0: check_terms fts2p-1.1.4 0 2 {a is test this} sl@0: check_doclist fts2p-1.1.4.1 0 2 a {[3 0[2]]} sl@0: check_doclist fts2p-1.1.4.2 0 2 is {[3 0[1]]} sl@0: check_doclist fts2p-1.1.4.3 0 2 test {[3 0[3]]} sl@0: check_doclist fts2p-1.1.4.4 0 2 this {[3 0[0]]} sl@0: sl@0: check_terms fts2p-1.1.5 0 3 {a is test this} sl@0: check_doclist fts2p-1.1.5.1 0 3 a {[1]} sl@0: check_doclist fts2p-1.1.5.2 0 3 is {[1]} sl@0: check_doclist fts2p-1.1.5.3 0 3 test {[1]} sl@0: check_doclist fts2p-1.1.5.4 0 3 this {[1]} sl@0: sl@0: #************************************************************************* sl@0: # Test results when all references to certain tokens are deleted. sl@0: db eval { sl@0: DROP TABLE IF EXISTS t1; sl@0: CREATE VIRTUAL TABLE t1 USING fts2(c); sl@0: INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); sl@0: DELETE FROM t1 WHERE rowid IN (1,3); sl@0: } sl@0: sl@0: # Still 4 segments because 0,3 will contain deletes for rowid 1 and 3. sl@0: do_test fts2p-1.2.segments { sl@0: execsql { sl@0: SELECT level, idx FROM t1_segdir ORDER BY level, idx; sl@0: } sl@0: } {0 0 0 1 0 2 0 3} sl@0: do_test fts2p-1.2.matches { sl@0: execsql { sl@0: SELECT OFFSETS(t1) FROM t1 sl@0: WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; sl@0: } sl@0: } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}} sl@0: sl@0: check_terms_all fts2p-1.2.1 {a is test that this was} sl@0: check_doclist_all fts2p-1.2.1.1 a {[2 0[2]]} sl@0: check_doclist_all fts2p-1.2.1.2 is {} sl@0: check_doclist_all fts2p-1.2.1.3 test {[2 0[3]]} sl@0: check_doclist_all fts2p-1.2.1.4 that {[2 0[0]]} sl@0: check_doclist_all fts2p-1.2.1.5 this {} sl@0: check_doclist_all fts2p-1.2.1.6 was {[2 0[1]]} sl@0: sl@0: check_terms fts2p-1.2.2 0 0 {a is test this} sl@0: check_doclist fts2p-1.2.2.1 0 0 a {[1 0[2]]} sl@0: check_doclist fts2p-1.2.2.2 0 0 is {[1 0[1]]} sl@0: check_doclist fts2p-1.2.2.3 0 0 test {[1 0[3]]} sl@0: check_doclist fts2p-1.2.2.4 0 0 this {[1 0[0]]} sl@0: sl@0: check_terms fts2p-1.2.3 0 1 {a test that was} sl@0: check_doclist fts2p-1.2.3.1 0 1 a {[2 0[2]]} sl@0: check_doclist fts2p-1.2.3.2 0 1 test {[2 0[3]]} sl@0: check_doclist fts2p-1.2.3.3 0 1 that {[2 0[0]]} sl@0: check_doclist fts2p-1.2.3.4 0 1 was {[2 0[1]]} sl@0: sl@0: check_terms fts2p-1.2.4 0 2 {a is test this} sl@0: check_doclist fts2p-1.2.4.1 0 2 a {[3 0[2]]} sl@0: check_doclist fts2p-1.2.4.2 0 2 is {[3 0[1]]} sl@0: check_doclist fts2p-1.2.4.3 0 2 test {[3 0[3]]} sl@0: check_doclist fts2p-1.2.4.4 0 2 this {[3 0[0]]} sl@0: sl@0: check_terms fts2p-1.2.5 0 3 {a is test this} sl@0: check_doclist fts2p-1.2.5.1 0 3 a {[1] [3]} sl@0: check_doclist fts2p-1.2.5.2 0 3 is {[1] [3]} sl@0: check_doclist fts2p-1.2.5.3 0 3 test {[1] [3]} sl@0: check_doclist fts2p-1.2.5.4 0 3 this {[1] [3]} sl@0: sl@0: #************************************************************************* sl@0: # Test results when everything is optimized manually. sl@0: db eval { sl@0: DROP TABLE IF EXISTS t1; sl@0: CREATE VIRTUAL TABLE t1 USING fts2(c); sl@0: INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); sl@0: INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); sl@0: DELETE FROM t1 WHERE rowid IN (1,3); sl@0: DROP TABLE IF EXISTS t1old; sl@0: ALTER TABLE t1 RENAME TO t1old; sl@0: CREATE VIRTUAL TABLE t1 USING fts2(c); sl@0: INSERT INTO t1 (rowid, c) SELECT rowid, c FROM t1old; sl@0: DROP TABLE t1old; sl@0: } sl@0: sl@0: # Should be a single optimal segment with the same logical results. sl@0: do_test fts2p-1.3.segments { sl@0: execsql { sl@0: SELECT level, idx FROM t1_segdir ORDER BY level, idx; sl@0: } sl@0: } {0 0} sl@0: do_test fts2p-1.3.matches { sl@0: execsql { sl@0: SELECT OFFSETS(t1) FROM t1 sl@0: WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; sl@0: } sl@0: } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}} sl@0: sl@0: check_terms_all fts2p-1.3.1 {a test that was} sl@0: check_doclist_all fts2p-1.3.1.1 a {[2 0[2]]} sl@0: check_doclist_all fts2p-1.3.1.2 test {[2 0[3]]} sl@0: check_doclist_all fts2p-1.3.1.3 that {[2 0[0]]} sl@0: check_doclist_all fts2p-1.3.1.4 was {[2 0[1]]} sl@0: sl@0: check_terms fts2p-1.3.2 0 0 {a test that was} sl@0: check_doclist fts2p-1.3.2.1 0 0 a {[2 0[2]]} sl@0: check_doclist fts2p-1.3.2.2 0 0 test {[2 0[3]]} sl@0: check_doclist fts2p-1.3.2.3 0 0 that {[2 0[0]]} sl@0: check_doclist fts2p-1.3.2.4 0 0 was {[2 0[1]]} sl@0: sl@0: finish_test