1# 2006 September 9 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 FTS3 module. 13# 14# $Id: fts3expr.test,v 1.4 2009/01/01 07:42:49 danielk1977 Exp $ 15# 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# If SQLITE_ENABLE_FTS3 is defined, omit this file. 21ifcapable !fts3 { 22 finish_test 23 return 24} 25 26set sqlite_fts3_enable_parentheses 1 27 28proc test_fts3expr {expr} { 29 db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')} 30} 31do_test fts3expr-1.0 { 32 test_fts3expr "abcd" 33} {PHRASE 3 0 abcd} 34do_test fts3expr-1.1 { 35 test_fts3expr " tag " 36} {PHRASE 3 0 tag} 37 38do_test fts3expr-1.2 { 39 test_fts3expr "ab AND cd" 40} {AND {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 41do_test fts3expr-1.3 { 42 test_fts3expr "ab OR cd" 43} {OR {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 44do_test fts3expr-1.4 { 45 test_fts3expr "ab NOT cd" 46} {NOT {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 47do_test fts3expr-1.5 { 48 test_fts3expr "ab NEAR cd" 49} {NEAR/10 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 50do_test fts3expr-1.6 { 51 test_fts3expr "ab NEAR/5 cd" 52} {NEAR/5 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 53 54do_test fts3expr-1.7 { 55 test_fts3expr {"one two three"} 56} {PHRASE 3 0 one two three} 57do_test fts3expr-1.8 { 58 test_fts3expr {zero "one two three" four} 59} {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}} 60do_test fts3expr-1.9 { 61 test_fts3expr {"one* two three*"} 62} {PHRASE 3 0 one+ two three+} 63 64do_test fts3expr-1.10 { 65 test_fts3expr {one* two} 66} {AND {PHRASE 3 0 one+} {PHRASE 3 0 two}} 67do_test fts3expr-1.11 { 68 test_fts3expr {one two*} 69} {AND {PHRASE 3 0 one} {PHRASE 3 0 two+}} 70 71do_test fts3expr-1.14 { 72 test_fts3expr {a:one two} 73} {AND {PHRASE 0 0 one} {PHRASE 3 0 two}} 74do_test fts3expr-1.15 { 75 test_fts3expr {one b:two} 76} {AND {PHRASE 3 0 one} {PHRASE 1 0 two}} 77 78proc strip_phrase_data {L} { 79 if {[lindex $L 0] eq "PHRASE"} { 80 return [lrange $L 3 end] 81 } 82 return [list \ 83 [lindex $L 0] \ 84 [strip_phrase_data [lindex $L 1]] \ 85 [strip_phrase_data [lindex $L 2]] \ 86 ] 87} 88proc test_fts3expr2 {expr} { 89 strip_phrase_data [ 90 db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')} 91 ] 92} 93do_test fts3expr-2.1 { 94 test_fts3expr2 "ab OR cd AND ef" 95} {OR ab {AND cd ef}} 96do_test fts3expr-2.2 { 97 test_fts3expr2 "cd AND ef OR ab" 98} {OR {AND cd ef} ab} 99do_test fts3expr-2.3 { 100 test_fts3expr2 "ab AND cd AND ef OR gh" 101} {OR {AND {AND ab cd} ef} gh} 102do_test fts3expr-2.4 { 103 test_fts3expr2 "ab AND cd OR ef AND gh" 104} {OR {AND ab cd} {AND ef gh}} 105do_test fts3expr-2.5 { 106 test_fts3expr2 "ab cd" 107} {AND ab cd} 108 109do_test fts3expr-3.1 { 110 test_fts3expr2 "(ab OR cd) AND ef" 111} {AND {OR ab cd} ef} 112do_test fts3expr-3.2 { 113 test_fts3expr2 "ef AND (ab OR cd)" 114} {AND ef {OR ab cd}} 115do_test fts3expr-3.3 { 116 test_fts3expr2 "(ab OR cd)" 117} {OR ab cd} 118do_test fts3expr-3.4 { 119 test_fts3expr2 "(((ab OR cd)))" 120} {OR ab cd} 121 122do_test fts3expr-3.5 { 123 test_fts3expr2 "one AND (two NEAR three)" 124} {AND one {NEAR/10 two three}} 125do_test fts3expr-3.6 { 126 test_fts3expr2 "one (two NEAR three)" 127} {AND one {NEAR/10 two three}} 128do_test fts3expr-3.7 { 129 test_fts3expr2 "(two NEAR three) one" 130} {AND {NEAR/10 two three} one} 131do_test fts3expr-3.8 { 132 test_fts3expr2 "(two NEAR three) AND one" 133} {AND {NEAR/10 two three} one} 134do_test fts3expr-3.9 { 135 test_fts3expr2 "(two NEAR three) (four five)" 136} {AND {NEAR/10 two three} {AND four five}} 137do_test fts3expr-3.10 { 138 test_fts3expr2 "(two NEAR three) AND (four five)" 139} {AND {NEAR/10 two three} {AND four five}} 140do_test fts3expr-3.11 { 141 test_fts3expr2 "(two NEAR three) (four NEAR five)" 142} {AND {NEAR/10 two three} {NEAR/10 four five}} 143do_test fts3expr-3.12 { 144 test_fts3expr2 "(two NEAR three) OR (four NEAR five)" 145} {OR {NEAR/10 two three} {NEAR/10 four five}} 146 147do_test fts3expr-3.13 { 148 test_fts3expr2 "(two NEAR/1a three)" 149} {AND {AND {AND two near} 1a} three} 150 151do_test fts3expr-3.14 { 152 test_fts3expr2 "(two NEAR// three)" 153} {AND {AND two near} three} 154do_test fts3expr-3.15 { 155 test_fts3expr2 "(two NEAR/: three)" 156} {AND {AND two near} three} 157 158do_test fts3expr-3.16 { 159 test_fts3expr2 "(two NEAR three)OR(four NEAR five)" 160} {OR {NEAR/10 two three} {NEAR/10 four five}} 161do_test fts3expr-3.17 { 162 test_fts3expr2 "(two NEAR three)OR\"four five\"" 163} {OR {NEAR/10 two three} {four five}} 164do_test fts3expr-3.18 { 165 test_fts3expr2 "one \u0080wo" 166} "AND one \u0080wo" 167 168 169 170#------------------------------------------------------------------------ 171# The following tests, fts3expr-4.*, test the parsers response to syntax 172# errors in query expressions. This is done using a real fts3 table and 173# MATCH clauses, not the parser test interface. 174# 175do_test fts3expr-4.1 { 176 execsql { CREATE VIRTUAL TABLE t1 USING fts3(a, b, c) } 177} {} 178 179# Mismatched parenthesis: 180do_test fts3expr-4.2.1 { 181 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world))' } 182} {1 {SQL logic error or missing database}} 183do_test fts3expr-4.2.2 { 184 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world' } 185} {1 {SQL logic error or missing database}} 186 187# Unterminated quotation marks: 188do_test fts3expr-4.3.1 { 189 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR "hello world' } 190} {1 {SQL logic error or missing database}} 191do_test fts3expr-4.3.2 { 192 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR hello world"' } 193} {1 {SQL logic error or missing database}} 194 195# Binary operators without the required operands. 196do_test fts3expr-4.4.1 { 197 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'OR hello world' } 198} {1 {SQL logic error or missing database}} 199do_test fts3expr-4.4.2 { 200 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'hello world OR' } 201} {1 {SQL logic error or missing database}} 202do_test fts3expr-4.4.3 { 203 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (hello world OR) two' } 204} {1 {SQL logic error or missing database}} 205do_test fts3expr-4.4.4 { 206 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (OR hello world) two' } 207} {1 {SQL logic error or missing database}} 208 209# NEAR operators with something other than phrases as arguments. 210do_test fts3expr-4.5.1 { 211 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello OR world) NEAR one' } 212} {1 {SQL logic error or missing database}} 213do_test fts3expr-4.5.2 { 214 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one NEAR (hello OR world)' } 215} {1 {SQL logic error or missing database}} 216 217#------------------------------------------------------------------------ 218# The following OOM tests are designed to cover cases in fts3_expr.c. 219# 220source $testdir/malloc_common.tcl 221do_malloc_test fts3expr-malloc-1 -sqlbody { 222 SELECT fts3_exprtest('simple', 'a b c "d e f"', 'a', 'b', 'c') 223} 224do_malloc_test fts3expr-malloc-2 -tclprep { 225 set sqlite_fts3_enable_parentheses 0 226} -sqlbody { 227 SELECT fts3_exprtest('simple', 'a -b', 'a', 'b', 'c') 228} -cleanup { 229 set sqlite_fts3_enable_parentheses 1 230} 231 232#------------------------------------------------------------------------ 233# The following tests are not very important. They cover error handling 234# cases in the test code, which makes test coverage easier to measure. 235# 236do_test fts3expr-5.1 { 237 catchsql { SELECT fts3_exprtest('simple', 'a b') } 238} {1 {Usage: fts3_exprtest(tokenizer, expr, col1, ...}} 239do_test fts3expr-5.2 { 240 catchsql { SELECT fts3_exprtest('doesnotexist', 'a b', 'c') } 241} {1 {No such tokenizer module}} 242do_test fts3expr-5.3 { 243 catchsql { SELECT fts3_exprtest('simple', 'a b OR', 'c') } 244} {1 {Error parsing expression}} 245 246#------------------------------------------------------------------------ 247# The next set of tests verifies that things actually work as they are 248# supposed to when using the new syntax. 249# 250do_test fts3expr-6.1 { 251 execsql { 252 CREATE VIRTUAL TABLE t1 USING fts3(a); 253 } 254 for {set ii 1} {$ii < 32} {incr ii} { 255 set v [list] 256 if {$ii & 1} { lappend v one } 257 if {$ii & 2} { lappend v two } 258 if {$ii & 4} { lappend v three } 259 if {$ii & 8} { lappend v four } 260 if {$ii & 16} { lappend v five } 261 execsql { INSERT INTO t1 VALUES($v) } 262 } 263 264 execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'five four one' ORDER BY rowid} 265} {25 27 29 31} 266 267foreach {id expr res} { 268 269 2 "five four NOT one" {24 26 28 30} 270 271 3 "five AND four OR one" 272 {1 3 5 7 9 11 13 15 17 19 21 23 24 25 26 27 28 29 30 31} 273 274 4 "five AND (four OR one)" {17 19 21 23 24 25 26 27 28 29 30 31} 275 276 5 "five NOT (four OR one)" {16 18 20 22} 277 278 6 "(five NOT (four OR one)) OR (five AND (four OR one))" 279 {16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 280 281 7 "(five OR one) AND two AND three" {7 15 22 23 30 31} 282 283 8 "five OR one AND two AND three" 284 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 285 286 9 "five OR one two three" 287 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 288 289 10 "five OR \"one two three\"" 290 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 291 292 11 "one two OR four five NOT three" {3 7 11 15 19 23 24 25 26 27 31} 293 294 12 "(one two OR four five) NOT three" {3 11 19 24 25 26 27} 295 296 13 "((((((one two OR four five)))))) NOT three" {3 11 19 24 25 26 27} 297 298} { 299 do_test fts3expr-6.$id { 300 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid } 301 } $res 302} 303 304set sqlite_fts3_enable_parentheses 0 305finish_test 306 307