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.3 2009/01/01 07:08:55 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 147 148#------------------------------------------------------------------------ 149# The following tests, fts3expr-4.*, test the parsers response to syntax 150# errors in query expressions. This is done using a real fts3 table and 151# MATCH clauses, not the parser test interface. 152# 153do_test fts3expr-4.1 { 154 execsql { CREATE VIRTUAL TABLE t1 USING fts3(a, b, c) } 155} {} 156 157# Mismatched parenthesis: 158do_test fts3expr-4.2.1 { 159 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world))' } 160} {1 {SQL logic error or missing database}} 161do_test fts3expr-4.2.2 { 162 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world' } 163} {1 {SQL logic error or missing database}} 164 165# Unterminated quotation marks: 166do_test fts3expr-4.3.1 { 167 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR "hello world' } 168} {1 {SQL logic error or missing database}} 169do_test fts3expr-4.3.2 { 170 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR hello world"' } 171} {1 {SQL logic error or missing database}} 172 173# Binary operators without the required operands. 174do_test fts3expr-4.4.1 { 175 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'OR hello world' } 176} {1 {SQL logic error or missing database}} 177do_test fts3expr-4.4.2 { 178 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'hello world OR' } 179} {1 {SQL logic error or missing database}} 180do_test fts3expr-4.4.3 { 181 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (hello world OR) two' } 182} {1 {SQL logic error or missing database}} 183do_test fts3expr-4.4.4 { 184 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (OR hello world) two' } 185} {1 {SQL logic error or missing database}} 186 187# NEAR operators with something other than phrases as arguments. 188do_test fts3expr-4.5.1 { 189 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello OR world) NEAR one' } 190} {1 {SQL logic error or missing database}} 191do_test fts3expr-4.5.2 { 192 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one NEAR (hello OR world)' } 193} {1 {SQL logic error or missing database}} 194 195#------------------------------------------------------------------------ 196# The following OOM tests are designed to cover cases in fts3_expr.c. 197# 198source $testdir/malloc_common.tcl 199do_malloc_test fts3expr-malloc-1 -sqlbody { 200 SELECT fts3_exprtest('simple', 'a b c "d e f"', 'a', 'b', 'c') 201} 202do_malloc_test fts3expr-malloc-2 -tclprep { 203 set sqlite_fts3_enable_parentheses 0 204} -sqlbody { 205 SELECT fts3_exprtest('simple', 'a -b', 'a', 'b', 'c') 206} -cleanup { 207 set sqlite_fts3_enable_parentheses 1 208} 209 210#------------------------------------------------------------------------ 211# The following tests are not very important. They cover error handling 212# cases in the test code, which makes test coverage easier to measure. 213# 214do_test fts3expr-5.1 { 215 catchsql { SELECT fts3_exprtest('simple', 'a b') } 216} {1 {Usage: fts3_exprtest(tokenizer, expr, col1, ...}} 217do_test fts3expr-5.2 { 218 catchsql { SELECT fts3_exprtest('doesnotexist', 'a b', 'c') } 219} {1 {No such tokenizer module}} 220do_test fts3expr-5.3 { 221 catchsql { SELECT fts3_exprtest('simple', 'a b OR', 'c') } 222} {1 {Error parsing expression}} 223 224#------------------------------------------------------------------------ 225# The next set of tests verifies that things actually work as they are 226# supposed to when using the new syntax. 227# 228do_test fts3expr-6.1 { 229 execsql { 230 CREATE VIRTUAL TABLE t1 USING fts3(a); 231 } 232 for {set ii 1} {$ii < 32} {incr ii} { 233 set v [list] 234 if {$ii & 1} { lappend v one } 235 if {$ii & 2} { lappend v two } 236 if {$ii & 4} { lappend v three } 237 if {$ii & 8} { lappend v four } 238 if {$ii & 16} { lappend v five } 239 execsql { INSERT INTO t1 VALUES($v) } 240 } 241 242 execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'five four one' ORDER BY rowid} 243} {25 27 29 31} 244 245foreach {id expr res} { 246 247 2 "five four NOT one" {24 26 28 30} 248 249 3 "five AND four OR one" 250 {1 3 5 7 9 11 13 15 17 19 21 23 24 25 26 27 28 29 30 31} 251 252 4 "five AND (four OR one)" {17 19 21 23 24 25 26 27 28 29 30 31} 253 254 5 "five NOT (four OR one)" {16 18 20 22} 255 256 6 "(five NOT (four OR one)) OR (five AND (four OR one))" 257 {16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 258 259 7 "(five OR one) AND two AND three" {7 15 22 23 30 31} 260 261 8 "five OR one AND two AND three" 262 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 263 264 9 "five OR one two three" 265 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 266 267 10 "five OR \"one two three\"" 268 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31} 269 270 11 "one two OR four five NOT three" {3 7 11 15 19 23 24 25 26 27 31} 271 272 12 "(one two OR four five) NOT three" {3 11 19 24 25 26 27} 273 274 13 "((((((one two OR four five)))))) NOT three" {3 11 19 24 25 26 27} 275 276} { 277 do_test fts3expr-6.$id { 278 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid } 279 } $res 280} 281 282set sqlite_fts3_enable_parentheses 0 283finish_test 284 285