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