1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
import pytest
from lunr.query import Query, QueryPresence
from lunr.query_parser import QueryParser
from lunr.exceptions import QueryParseError
def parse(q):
query = Query(["title", "body"])
parser = QueryParser(q, query)
parser.parse()
return query.clauses
class TestQueryParser:
def test_parse_empty_string(self):
clauses = parse("")
assert len(clauses) == 0
def test_parse_single_term(self):
clauses = parse("foo")
assert len(clauses) == 1
clause = clauses[0]
assert clause.term == "foo"
assert clause.fields == ["title", "body"]
assert clause.use_pipeline is True
assert clause.presence is QueryPresence.OPTIONAL
def test_parse_single_term_uppercase(self):
clauses = parse("FOO")
assert len(clauses) == 1
clause = clauses[0]
assert clause.term == "foo"
assert clause.fields == ["title", "body"]
assert clause.use_pipeline is True
def test_parse_single_term_with_wildcard(self):
clauses = parse("fo*")
assert len(clauses) == 1
clause = clauses[0]
assert clause.term == "fo*"
assert clause.use_pipeline is False
def test_multiple_terms(self):
clauses = parse("foo bar")
assert len(clauses) == 2
assert clauses[0].term == "foo"
assert clauses[1].term == "bar"
def test_term_with_presence_required_adds_required_clause(self):
clauses = parse("+foo")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].boost == 1
assert clauses[0].fields == ["title", "body"]
assert clauses[0].presence == QueryPresence.REQUIRED
def test_term_with_presence_required_adds_prohibited_clause(self):
clauses = parse("-foo")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].boost == 1
assert clauses[0].fields == ["title", "body"]
assert clauses[0].presence == QueryPresence.PROHIBITED
def test_term_scoped_by_field_with_presence_required(self):
clauses = parse("+title:foo")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].boost == 1
assert clauses[0].fields == ["title"]
assert clauses[0].presence == QueryPresence.REQUIRED
def test_term_scoped_by_field_with_presence_prohibited(self):
clauses = parse("-title:foo")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].boost == 1
assert clauses[0].fields == ["title"]
assert clauses[0].presence == QueryPresence.PROHIBITED
def test_multiple_terms_with_presence_creates_two_clauses(self):
clauses = parse("+foo +bar")
assert len(clauses) == 2
assert clauses[0].term == "foo"
assert clauses[1].term == "bar"
assert clauses[0].presence == QueryPresence.REQUIRED
assert clauses[1].presence == QueryPresence.REQUIRED
def test_unknown_field(self):
with pytest.raises(QueryParseError):
parse("unknown:foo")
def test_field_without_a_term(self):
with pytest.raises(QueryParseError):
parse("title:")
def test_field_twice(self):
with pytest.raises(QueryParseError):
parse("title:title:")
def test_term_with_field(self):
clauses = parse("title:foo")
assert len(clauses) == 1
assert clauses[0].fields == ["title"]
def test_uppercase_field_with_uppercase_term(self):
query = Query(["TITLE"])
parser = QueryParser("TITLE:FOO", query)
parser.parse()
clauses = query.clauses
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].fields == ["TITLE"]
def test_multiple_terms_scoped_to_different_fields(self):
clauses = parse("title:foo body:bar")
assert len(clauses) == 2
assert clauses[0].fields == ["title"]
assert clauses[1].fields == ["body"]
assert clauses[0].term == "foo"
assert clauses[1].term == "bar"
def test_single_term_with_edit_distance(self):
clauses = parse("foo~2")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].fields == ["title", "body"]
assert clauses[0].edit_distance == 2
def test_multiple_terms_with_edit_distance(self):
clauses = parse("foo~2 bar~3")
assert len(clauses) == 2
assert clauses[0].fields == ["title", "body"]
assert clauses[1].fields == ["title", "body"]
assert clauses[0].term == "foo"
assert clauses[1].term == "bar"
assert clauses[0].edit_distance == 2
assert clauses[1].edit_distance == 3
def test_single_term_scoped_to_field_with_edit_distance(self):
clauses = parse("title:foo~2")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].fields == ["title"]
assert clauses[0].edit_distance == 2
def test_non_numeric_edit_distance(self):
with pytest.raises(QueryParseError):
parse("foo~a")
def test_edit_distance_without_a_term(self):
with pytest.raises(QueryParseError):
parse("~2")
def test_single_term_with_boost(self):
clauses = parse("foo^2")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].fields == ["title", "body"]
assert clauses[0].boost == 2
def test_non_numeric_boost(self):
with pytest.raises(QueryParseError):
parse("foo^a")
def test_boost_without_a_term(self):
with pytest.raises(QueryParseError):
parse("^2")
def test_multiple_terms_with_boost(self):
clauses = parse("foo^2 bar^3")
assert len(clauses) == 2
assert clauses[0].fields == ["title", "body"]
assert clauses[1].fields == ["title", "body"]
assert clauses[0].term == "foo"
assert clauses[1].term == "bar"
assert clauses[0].boost == 2
assert clauses[1].boost == 3
def test_term_scoped_by_field_with_boost(self):
clauses = parse("title:foo^2")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].fields == ["title"]
assert clauses[0].boost == 2
def test_term_with_boost_and_edit_distance(self):
clauses = parse("foo^2~3")
assert len(clauses) == 1
assert clauses[0].term == "foo"
assert clauses[0].fields == ["title", "body"]
assert clauses[0].edit_distance == 3
assert clauses[0].boost == 2
def test_edit_distance_followed_by_presence(self):
clauses = parse("foo~10 +bar")
assert len(clauses) == 2
assert clauses[0].fields == ["title", "body"]
assert clauses[1].fields == ["title", "body"]
assert clauses[0].term == "foo"
assert clauses[1].term == "bar"
assert clauses[0].edit_distance == 10
assert clauses[1].edit_distance == 0
assert clauses[0].presence == QueryPresence.OPTIONAL
assert clauses[1].presence == QueryPresence.REQUIRED
def test_boost_followed_by_presence(self):
clauses = parse("foo^10 +bar")
assert len(clauses) == 2
assert clauses[0].fields == ["title", "body"]
assert clauses[1].fields == ["title", "body"]
assert clauses[0].term == "foo"
assert clauses[1].term == "bar"
assert clauses[0].boost == 10
assert clauses[1].boost == 1
assert clauses[0].presence == QueryPresence.OPTIONAL
assert clauses[1].presence == QueryPresence.REQUIRED
|