def test_minus(self): tree = (AndOperation(Prohibit(Word("test", tail=" ")), Prohibit(Word("foo", tail=" "), head=" "), Not(Word("bar", head=" "), head=" "))) parsed = parser.parse("-test AND -foo AND NOT bar") self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_set_children_raises(self): test = self._test_set_children_raises test(Word("foo"), [Word("foo")]) test(Phrase('"foo"'), [Word("foo")]) test(Regex("/foo/"), [Word("foo")]) test(SearchField("foo", Word("bar")), []) test(SearchField("foo", Word("bar")), [Word("bar"), Word("baz")]) test(Group(Word("foo")), []) test(Group(Word("foo")), [Word("foo"), Word("bar")]) test(FieldGroup(Word("foo")), []) test(FieldGroup(Word("foo")), [Word("foo"), Word("bar")]) test(Range(Word("20"), Word("30")), []) test(Range(Word("20"), Word("30")), [Word("20"), Word("30"), Word("40")]) test(Proximity(Word("foo")), []) test(Proximity(Word("foo")), [Word("foo"), Word("bar")]) test(Fuzzy(Word("foo")), []) test(Fuzzy(Word("foo")), [Word("foo"), Word("bar")]) test(Boost(Word("foo"), force=1), []) test(Boost(Word("foo"), force=1), [Word("foo"), Word("bar")]) test(Plus(Word("foo")), []) test(Plus(Word("foo")), [Word("foo"), Word("bar")]) test(Not(Word("foo")), []) test(Not(Word("foo")), [Word("foo"), Word("bar")]) test(Prohibit(Word("foo")), []) test(Prohibit(Word("foo")), [Word("foo"), Word("bar")])
def test_named_queries_not(self): tree = Not(SearchField("text", Word("foo"))) set_name(tree, "a") result = self.transformer(tree) self.assertEqual( result, { 'bool': { 'must_not': [{ 'term': { 'text': { '_name': 'a', 'value': 'foo' } } }] } }) tree = Prohibit(SearchField("text", Word("foo"))) set_name(tree, "a") result = self.transformer(tree) self.assertEqual( result, { 'bool': { 'must_not': [{ 'term': { 'text': { '_name': 'a', 'value': 'foo' } } }] } })
def test_zealous_or_not_prohibit(self): query = (OrOperation(Prohibit(Word("foo")), Word("bar"))) check_zealous = LuceneCheck(zeal=1) self.assertFalse(check_zealous(query)) self.assertIn("inconsistent", check_zealous.errors(query)[0]) check_easy_going = LuceneCheck() self.assertTrue(check_easy_going(query))
def test_negation(self): for tree in [Prohibit(Word("foo")), Not(Word("foo"))]: with self.subTest("%r" % type(tree)): paths_ok, paths_ko = self.propagate_matching( tree, set(), {(0, )}) self.assertEqual(paths_ok, {()}) self.assertEqual(paths_ko, {(0, )}) paths_ok, paths_ko = self.propagate_matching( tree, {(0, )}, set()) self.assertEqual(paths_ok, {(0, )}) self.assertEqual(paths_ko, {()})
def test_minus(self): tree = parser.parse("\t-\rfoo\n") self.assertEqual(tree, Prohibit(Word("foo"))) self.assertEqual(tree.head, "\t") self.assertEqual(tree.tail, "") self.assertEqual(tree.pos, 1) self.assertEqual(tree.size, 6) foo, = tree.children self.assertEqual(foo.head, "\r") self.assertEqual(foo.tail, "\n") self.assertEqual(foo.pos, 3) self.assertEqual(foo.size, 3) self.assertEqual(str(tree), "-\rfoo\n") self.assertEqual(tree.__str__(head_tail=True), "\t-\rfoo\n")
def test_check_ok(self): query = (AndOperation( SearchField( "f", FieldGroup( AndOperation( Boost(Proximity(Phrase('"foo bar"'), 4), "4.2"), Prohibit(Range("100", "200"))))), Group(OrOperation(Fuzzy(Word("baz"), ".8"), Plus(Word("fizz")))))) check = LuceneCheck() self.assertTrue(check(query)) self.assertEqual(check.errors(query), []) check = LuceneCheck(zeal=1) self.assertTrue(check(query)) self.assertEqual(check.errors(query), [])
def test_set_children(self): test = self._test_set_children test(Word("foo"), []) test(Phrase('"foo"'), []) test(Regex("/foo/"), []) test(SearchField("foo", Word("bar")), [Word("baz")]) test(Group(Word("foo")), [Word("foo")]) test(FieldGroup(Word("foo")), [Word("foo")]) test(Range(Word("20"), Word("30")), [Word("40"), Word("50")]) test(Proximity(Word("foo")), [Word("foo")]) test(Fuzzy(Word("foo")), [Word("foo")]) test(Boost(Word("foo"), force=1), [Word("foo")]) many_terms = tuple(Word(f"foo_{i}") for i in range(5)) test(UnknownOperation(Word("foo"), Word("bar")), (Word("foo"), Word("bar"))) test(UnknownOperation(*many_terms), many_terms) test(AndOperation(Word("foo"), Word("bar")), (Word("foo"), Word("bar"))) test(AndOperation(*many_terms), many_terms) test(OrOperation(Word("foo"), Word("bar")), (Word("foo"), Word("bar"))) test(OrOperation(*many_terms), many_terms) test(Plus(Word("foo")), [Word("foo")]) test(Not(Word("foo")), [Word("foo")]) test(Prohibit(Word("foo")), [Word("foo")])
def test_combination(self): tree = AndOperation( OrOperation( SearchField( "mine", FieldGroup( Plus(AndOperation( Word("foo"), Regex("/fizz/"), ), ), ), ), Boost( Group( AndOperation( Phrase('"ham"'), Word("spam"), Prohibit(Fuzzy(Word("fuzz"))), ), ), force=2, ), ), Not(OrOperation( Word('"bar"'), Word('"baz"'), ), ), ) to_path = simple_naming(tree) paths_ok, paths_ko = self.propagate_matching(tree, set()) self.assertEqual( paths_to_names(tree, paths_ok), {"prohibit", "not"}, ) self.assertEqual( paths_to_names(tree, paths_ko), { "and", "or", "searchfield", "fieldgroup", "plus", "and2", "foo", "fizz", "boost", "group", "and3", "ham", "spam", "fuzzy", "or2", "bar", "baz" }, ) # adding matching just enough positive expressions, so that complete expression matches paths_ok, paths_ko = self.propagate_matching( tree, {to_path["foo"], to_path["fizz"], to_path["ham"]}, ) self.assertEqual( paths_to_names(tree, paths_ok), { "and", "or", "searchfield", "fieldgroup", "plus", "and2", "foo", "fizz", "ham", "prohibit", "not" }, ) self.assertEqual( paths_to_names(tree, paths_ko), {"boost", "group", "and3", "spam", "fuzzy", "or2", "bar", "baz"}, ) # making everything match paths_ok, paths_ko = self.propagate_matching( tree, {to_path["foo"], to_path["fizz"], to_path["ham"], to_path["spam"]}, ) self.assertEqual( paths_to_names(tree, paths_ok), { "and", "or", "searchfield", "fieldgroup", "plus", "and2", "foo", "fizz", "ham", "prohibit", "boost", "group", "and3", "spam", "not" }) self.assertEqual(paths_to_names(tree, paths_ko), {"fuzzy", "or2", "bar", "baz"}) # making everything match, but some negative expression paths_ok, paths_ko = self.propagate_matching( tree, { to_path["foo"], to_path["fizz"], to_path["ham"], to_path["spam"], to_path["fuzzy"], to_path["bar"], }, ) self.assertEqual( paths_to_names(tree, paths_ok), { "or", "searchfield", "fieldgroup", "plus", "and2", "foo", "fizz", "ham", "spam", "fuzzy", "or2", "bar", }, ) self.assertEqual( paths_to_names(tree, paths_ko), { "and", "boost", "group", "and3", "prohibit", "boost", "group", "and3", "not", "baz", }, )
def test_bad_field_expr(self): check = LuceneCheck() query = SearchField("foo", Prohibit(Word("bar"))) self.assertFalse(check(query)) self.assertEqual(len(check.errors(query)), 1) self.assertIn("not valid", check.errors(query)[0])