1# 2014 June 17 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 FTS5 module. 13# 14# 15 16source [file join [file dirname [info script]] fts5_common.tcl] 17set testprefix fts5ae 18 19# If SQLITE_ENABLE_FTS5 is defined, omit this file. 20ifcapable !fts5 { 21 finish_test 22 return 23} 24 25foreach_detail_mode $testprefix { 26 27do_execsql_test 1.0 { 28 CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=%DETAIL%); 29 INSERT INTO t1(t1, rank) VALUES('pgsz', 32); 30} 31 32do_execsql_test 1.1 { 33 INSERT INTO t1 VALUES('hello', 'world'); 34 SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC; 35} {1} 36 37do_execsql_test 1.2 { 38 INSERT INTO t1 VALUES('world', 'hello'); 39 SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC; 40} {1 2} 41 42do_execsql_test 1.3 { 43 INSERT INTO t1 VALUES('world', 'world'); 44 SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC; 45} {1 2} 46 47do_execsql_test 1.4.1 { 48 INSERT INTO t1 VALUES('hello', 'hello'); 49} 50 51do_execsql_test 1.4.2 { 52 SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC; 53} {1 2 4} 54 55fts5_aux_test_functions db 56 57#------------------------------------------------------------------------- 58# 59do_execsql_test 2.0 { 60 CREATE VIRTUAL TABLE t2 USING fts5(x, y, detail=%DETAIL%); 61 INSERT INTO t2 VALUES('u t l w w m s', 'm f m o l t k o p e'); 62 INSERT INTO t2 VALUES('f g q e l n d m z x q', 'z s i i i m f w w f n g p'); 63} 64 65do_execsql_test 2.1 { 66 SELECT rowid, fts5_test_poslist(t2) FROM t2 67 WHERE t2 MATCH 'm' ORDER BY rowid; 68} { 69 1 {0.0.5 0.1.0 0.1.2} 70 2 {0.0.7 0.1.5} 71} 72 73do_execsql_test 2.2 { 74 SELECT rowid, fts5_test_poslist(t2) FROM t2 75 WHERE t2 MATCH 'u OR q' ORDER BY rowid; 76} { 77 1 {0.0.0} 78 2 {1.0.2 1.0.10} 79} 80 81if {[detail_is_full]} { 82 do_execsql_test 2.3 { 83 SELECT rowid, fts5_test_poslist(t2) FROM t2 84 WHERE t2 MATCH 'y:o' ORDER BY rowid; 85 } { 86 1 {0.1.3 0.1.7} 87 } 88} 89 90#------------------------------------------------------------------------- 91# 92do_execsql_test 3.0 { 93 CREATE VIRTUAL TABLE t3 USING fts5(x, y, detail=%DETAIL%); 94 INSERT INTO t3 VALUES( 'j f h o x x a z g b a f a m i b', 'j z c z y x w t'); 95 INSERT INTO t3 VALUES( 'r c', ''); 96} 97 98if {[detail_is_full]} { 99 do_execsql_test 3.1 { 100 SELECT rowid, fts5_test_poslist(t3) FROM t3 WHERE t3 MATCH 'NEAR(a b)'; 101 } { 102 1 {0.0.6 1.0.9 0.0.10 0.0.12 1.0.15} 103 } 104 105 do_execsql_test 3.2 { 106 SELECT rowid, fts5_test_poslist(t3) FROM t3 WHERE t3 MATCH 'NEAR(r c)'; 107 } { 108 2 {0.0.0 1.0.1} 109 } 110} 111 112do_execsql_test 3.3 { 113 INSERT INTO t3 114 VALUES('k x j r m a d o i z j', 'r t t t f e b r x i v j v g o'); 115 SELECT rowid, fts5_test_poslist(t3) 116 FROM t3 WHERE t3 MATCH 'a OR b AND c'; 117} { 118 1 {0.0.6 1.0.9 0.0.10 0.0.12 1.0.15 2.1.2} 119 3 0.0.5 120} 121 122#------------------------------------------------------------------------- 123# 124do_execsql_test 4.0 { 125 CREATE VIRTUAL TABLE t4 USING fts5(x, y, detail=%DETAIL%); 126 INSERT INTO t4 127 VALUES('k x j r m a d o i z j', 'r t t t f e b r x i v j v g o'); 128} 129 130do_execsql_test 4.1 { 131 SELECT rowid, fts5_test_poslist(t4) FROM t4 WHERE t4 MATCH 'a OR b AND c'; 132} { 133 1 0.0.5 134} 135 136#------------------------------------------------------------------------- 137# Test that the xColumnSize() and xColumnAvgsize() APIs work. 138# 139reset_db 140fts5_aux_test_functions db 141 142do_execsql_test 5.1 { 143 CREATE VIRTUAL TABLE t5 USING fts5(x, y, detail=%DETAIL%); 144 INSERT INTO t5 VALUES('a b c d', 'e f g h i j'); 145 INSERT INTO t5 VALUES('', 'a'); 146 INSERT INTO t5 VALUES('a', ''); 147} 148do_execsql_test 5.2 { 149 SELECT rowid, fts5_test_columnsize(t5) FROM t5 WHERE t5 MATCH 'a' 150 ORDER BY rowid DESC; 151} { 152 3 {1 0} 153 2 {0 1} 154 1 {4 6} 155} 156 157do_execsql_test 5.3 { 158 SELECT rowid, fts5_test_columntext(t5) FROM t5 WHERE t5 MATCH 'a' 159 ORDER BY rowid DESC; 160} { 161 3 {a {}} 162 2 {{} a} 163 1 {{a b c d} {e f g h i j}} 164} 165 166do_execsql_test 5.4 { 167 SELECT rowid, fts5_test_columntotalsize(t5) FROM t5 WHERE t5 MATCH 'a' 168 ORDER BY rowid DESC; 169} { 170 3 {5 7} 171 2 {5 7} 172 1 {5 7} 173} 174 175do_execsql_test 5.5 { 176 INSERT INTO t5 VALUES('x y z', 'v w x y z'); 177 SELECT rowid, fts5_test_columntotalsize(t5) FROM t5 WHERE t5 MATCH 'a' 178 ORDER BY rowid DESC; 179} { 180 3 {8 12} 181 2 {8 12} 182 1 {8 12} 183} 184 185#------------------------------------------------------------------------- 186# Test the xTokenize() API 187# 188reset_db 189fts5_aux_test_functions db 190do_execsql_test 6.1 { 191 CREATE VIRTUAL TABLE t6 USING fts5(x, y, detail=%DETAIL%); 192 INSERT INTO t6 VALUES('There are more', 'things in heaven and earth'); 193 INSERT INTO t6 VALUES(', Horatio, Than are', 'dreamt of in your philosophy.'); 194} 195 196do_execsql_test 6.2 { 197 SELECT rowid, fts5_test_tokenize(t6) FROM t6 WHERE t6 MATCH 't*' 198} { 199 1 {{there are more} {things in heaven and earth}} 200 2 {{horatio than are} {dreamt of in your philosophy}} 201} 202 203#------------------------------------------------------------------------- 204# Test the xQueryPhrase() API 205# 206reset_db 207fts5_aux_test_functions db 208do_execsql_test 7.1 { 209 CREATE VIRTUAL TABLE t7 USING fts5(x, y, detail=%DETAIL%); 210} 211do_test 7.2 { 212 foreach {x y} { 213 {q i b w s a a e l o} {i b z a l f p t e u} 214 {b a z t a l o x d i} {b p a d b f h d w y} 215 {z m h n p p u i e g} {v h d v b x j j c z} 216 {a g i m v a u c b i} {p k s o t l r t b m} 217 {v v c j o d a s c p} {f f v o k p o f o g} 218 } { 219 execsql {INSERT INTO t7 VALUES($x, $y)} 220 } 221 execsql { SELECT count(*) FROM t7 } 222} {5} 223 224foreach {tn q res} { 225 1 a {{4 2}} 226 2 b {{3 4}} 227 3 c {{2 1}} 228 4 d {{2 2}} 229 5 {a AND b} {{4 2} {3 4}} 230 6 {a OR b OR c OR d} {{4 2} {3 4} {2 1} {2 2}} 231} { 232 do_execsql_test 7.3.$tn { 233 SELECT fts5_test_queryphrase(t7) FROM t7 WHERE t7 MATCH $q LIMIT 1 234 } [list $res] 235} 236 237do_execsql_test 7.4 { 238 SELECT fts5_test_rowcount(t7) FROM t7 WHERE t7 MATCH 'a'; 239} {5 5 5 5} 240 241#do_execsql_test 7.4 { 242# SELECT rowid, bm25debug(t7) FROM t7 WHERE t7 MATCH 'a'; 243#} {5 5 5 5} 244# 245 246#------------------------------------------------------------------------- 247# 248do_test 8.1 { 249 execsql { CREATE VIRTUAL TABLE t8 USING fts5(x, y, detail=%DETAIL%) } 250 foreach {rowid x y} { 251 0 {A o} {o o o C o o o o o o o o} 252 1 {o o B} {o o o C C o o o o o o o} 253 2 {A o o} {o o o o D D o o o o o o} 254 3 {o B} {o o o o o D o o o o o o} 255 4 {E o G} {H o o o o o o o o o o o} 256 5 {F o G} {I o J o o o o o o o o o} 257 6 {E o o} {H o J o o o o o o o o o} 258 7 {o o o} {o o o o o o o o o o o o} 259 9 {o o o} {o o o o o o o o o o o o} 260 } { 261 execsql { INSERT INTO t8(rowid, x, y) VALUES($rowid, $x, $y) } 262 } 263} {} 264 265foreach {tn q res} { 266 1 {a} {0 2} 267 2 {b} {3 1} 268 3 {c} {1 0} 269 4 {d} {2 3} 270 5 {g AND (e OR f)} {5 4} 271 6 {j AND (h OR i)} {5 6} 272} { 273 do_execsql_test 8.2.$tn.1 { 274 SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY bm25(t8); 275 } $res 276 277 do_execsql_test 8.2.$tn.2 { 278 SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY +rank; 279 } $res 280 281 do_execsql_test 8.2.$tn.3 { 282 SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY rank; 283 } $res 284} 285 286#------------------------------------------------------------------------- 287# Test xPhraseCount() for some different queries. 288# 289do_test 9.1 { 290 execsql { CREATE VIRTUAL TABLE t9 USING fts5(x) } 291 foreach x { 292 "a b c" "d e f" 293 } { 294 execsql { INSERT INTO t9 VALUES($x) } 295 } 296} {} 297 298foreach {tn q cnt} { 299 1 {a AND b} 2 300 2 {a OR b} 2 301 3 {a OR b OR c} 3 302 4 {NEAR(a b)} 2 303} { 304 do_execsql_test 9.2.$tn { 305 SELECT fts5_test_phrasecount(t9) FROM t9 WHERE t9 MATCH $q LIMIT 1 306 } $cnt 307} 308 309} 310 311finish_test 312