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