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_repeating_expression(self): # non regression test tree = AndOperation( Group(OrOperation(Word('bar'), Word('foo'))), Group(OrOperation(Word('bar'), Word('foo'), Word('spam'))), ) # basic transformer should not change tree same_tree = TreeTransformer().visit(copy.deepcopy(tree)) self.assertEqual(same_tree, tree)
def test_group(self): orig = Group(Word("bar"), pos=3, head="\n", tail="\t") copy = orig.clone_item() self.assert_equal_tail_head_pos(orig, copy) self.assertEqual(copy.expr, NONE_ITEM) self.assertNotEqual(orig, copy) copy.children = [Word("bar")] self.assertEqual(orig, copy)
def test_lucene_resolution_last_op_with_group(self): tree = (OrOperation( Word("a"), Word("b"), Group( AndOperation(Word("c"), UnknownOperation(Word("d"), Word("e")))), UnknownOperation(Word("f"), Word("g")), Group(UnknownOperation(Word("h"), Word("i"))))) expected = (OrOperation( Word("a"), Word("b"), Group(AndOperation(Word("c"), AndOperation(Word("d"), Word("e")))), OrOperation(Word("f"), Word("g")), Group(AndOperation(Word("h"), Word("i"))))) resolver = UnknownOperationResolver(resolve_to=None) self.assertEqual(resolver(tree), expected)
def test_bad_group(self): check = LuceneCheck() query = SearchField("f", Group(Word("foo"))) self.assertFalse(check(query)) self.assertEqual(len(check.errors(query)), 2) # one for bad expr, one for misuse self.assertIn("Group misuse", "".join(check.errors(query)))
def test_named_queries_group(self): tree = SearchField("text", FieldGroup(Word("bar"))) set_name(tree.children[0], "a") result = self.transformer(tree) self.assertEqual( result, {"term": { "text": { "value": "bar", "_name": "a" } }}, ) tree = Group(SearchField("text", Word("bar"))) set_name(tree, "a") result = self.transformer(tree) self.assertEqual( result, {"term": { "text": { "value": "bar", "_name": "a" } }}, )
def visit_phrase(self, node, parent): if (len(parent) > 0 and (isinstance(parent[-1], luqum.tree.SearchField) or isinstance(parent[-1], luqum.tree.Range))): return node else: search_list = [SearchField(f, node) for f in self.fields] return Group(OrOperation(*search_list))
def test_auto_name_nested(self): tree = AndOperation( OrOperation( SearchField("bar", Word("test")), AndOperation( Proximity(Phrase('"test"'), 2), SearchField("baz", Word("test")), ), ), Group(UnknownOperation( Fuzzy(Word("test")), Phrase('"test"'), ), ), ) names = auto_name(tree) self.assertEqual(sorted(names.keys()), list("abcdefgh")) # and and1 = tree self.assertEqual(get_name(and1), None) # - or or1 = and1.children[0] self.assertEqual(get_name(or1), "a") self.assertEqual(names["a"], (0, )) # -- search field sfield1 = or1.children[0] self.assertEqual(get_name(sfield1), "c") self.assertEqual(names["c"], (0, 0)) self.assertEqual(get_name(sfield1.expr), None) # -- and and2 = or1.children[1] self.assertEqual(get_name(and2), "d") self.assertEqual(names["d"], (0, 1)) # --- proximity phrase self.assertEqual(get_name(and2.children[0]), "e") self.assertEqual(names["e"], (0, 1, 0)) self.assertEqual(get_name(and2.children[0].term), None) # --- search field sfield2 = and2.children[1] self.assertEqual(get_name(sfield2), "f") self.assertEqual(names["f"], (0, 1, 1)) self.assertEqual(get_name(sfield2.expr), None) # - group group1 = and1.children[1] self.assertEqual(get_name(group1), "b") self.assertEqual(names["b"], (1, )) # -- unknown op unknownop1 = group1.children[0] self.assertEqual(get_name(unknownop1), None) # --- fuzzy word self.assertEqual(get_name(unknownop1.children[0]), "g") self.assertEqual(names["g"], (1, 0, 0)) self.assertEqual(get_name(unknownop1.children[0].term), None) # --- phrase self.assertEqual(get_name(unknownop1.children[1]), "h") self.assertEqual(names["h"], (1, 0, 1))
def test_name_index_simple_term(self): tree = Word("bar") set_name(tree, "0") self.assertEqual(name_index(tree), {"0": (0, len(str(tree)))}) phrase = Phrase('"baz"') tree = Group(phrase) set_name(phrase, "0") self.assertEqual(name_index(tree), {"0": (1, len(str(phrase)))}) set_name(phrase, "funny") self.assertEqual(name_index(tree), {"funny": (1, len(str(phrase)))})
def test_visit_complex(self): tree = AndOperation( Group( OrOperation(Word("foo"), Word("bar"), Boost(Fuzzy(Word("baz")), force=2))), Proximity(Phrase('"spam ham"')), SearchField("fizz", Regex("/fuzz/")), ) transformed = self.transform(tree) expected = AndOperation( Group( OrOperation( Word("foo@0-0-0"), Word("bar@0-0-1"), Boost(Fuzzy(Word("baz@0-0-2-0-0")), force=2), )), Proximity(Phrase('"spam ham@1-0"')), SearchField("fizz", Regex("/fuzz@2-0/")), ) self.assertEqual(transformed, expected)
def test_boost(self): tree = (UnknownOperation( Boost(Phrase('"foo bar"'), Decimal("3.0"), tail=" "), Boost( Group( AndOperation(Word('baz', tail=" "), Word('bar', head=" "))), Decimal("2.1")))) parsed = parser.parse('"foo bar"^3 (baz AND bar)^2.1') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_groups(self): tree = (OrOperation( Word('test', tail=" "), Group(AndOperation( SearchField("subject", FieldGroup( OrOperation(Word('foo', tail=" "), Word('bar', head=" "))), tail=" "), Word('baz', head=" ")), head=" "))) parsed = parser.parse('test OR (subject:(foo OR bar) AND baz)') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_named_queries_complex(self): tree = (AndOperation( SearchField("text", Phrase('"foo bar"')), Group(OrOperation( Word("bar"), SearchField("spam", Word("baz")), ), ), )) and_op = tree search_text = and_op.operands[0] or_op = and_op.operands[1].children[0] bar = or_op.operands[0] search_spam = or_op.operands[1] set_name(search_text, "foo_bar") set_name(bar, "bar") set_name(search_spam, "baz") expected = { 'bool': { 'must': [{ 'term': { 'text': { '_name': 'foo_bar', 'value': 'foo bar' } } }, { 'bool': { 'should': [{ 'term': { 'text': { '_name': 'bar', 'value': 'bar' } } }, { 'match': { 'spam': { '_name': 'baz', 'query': 'baz', 'zero_terms_query': 'none' } } }] } }] } } result = self.transformer(tree) self.assertEqual(result, expected)
def test_group(self): tree = parser.parse("\t(\rfoo )\n") self.assertEqual(tree, Group(Word("foo"))) self.assertEqual(tree.head, "\t") self.assertEqual(tree.tail, "\n") self.assertEqual(tree.pos, 1) self.assertEqual(tree.size, 8) foo, = tree.children self.assertEqual(foo.head, "\r") self.assertEqual(foo.tail, " ") self.assertEqual(foo.pos, 3) self.assertEqual(foo.size, 3) self.assertEqual(str(tree), "(\rfoo )") self.assertEqual(tree.__str__(head_tail=True), "\t(\rfoo )\n")
def test_simple_propagation(self): # propagation in nodes with only one children tree = Boost(Group(SearchField("foo", FieldGroup(Plus(Word("bar"))))), force=2) paths_ok, paths_ko = self.propagate_matching(tree, set(), {()}) self.assertEqual(paths_ok, set()) self.assertEqual(paths_ko, {(), (0, ), (0, 0), (0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0, 0)}) paths_ok, paths_ko = self.propagate_matching(tree, {()}, set()) self.assertEqual(paths_ok, {(), (0, ), (0, 0), (0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0, 0)}) self.assertEqual(paths_ko, set())
def test_with_unknown_op(self): prettify = Prettifier(indent=8, max_len=20) tree = UnknownOperation( Group( UnknownOperation(Word("baaaaaaaaaar"), Word("baaaaaaaaaaaaaz"))), Word("fooooooooooo")) self.assertEqual( "\n" + prettify(tree), """ ( baaaaaaaaaar baaaaaaaaaaaaaz ) fooooooooooo""")
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_visit_complex(self): tree = AndOperation( Group( OrOperation(Word("foo"), Word("bar"), Boost(Fuzzy(Word("baz")), force=2))), Proximity(Phrase('"spam ham"')), SearchField("fizz", Regex("/fuzz/")), ) paths = self.visit(tree) self.assertEqual(sorted(paths), [ ((0, 0, 0), "foo"), ((0, 0, 1), "bar"), ((0, 0, 2, 0, 0), "baz"), ((1, 0), '"spam ham"'), ((2, 0), '/fuzz/'), ])
def test_auto_name_nested(self): tree = AndOperation( OrOperation( SearchField("bar", Word("test")), AndOperation( Proximity(Phrase('"test"'), 2), SearchField("baz", Word("test")), ), ), Group( UnknownOperation( Fuzzy(Word("test")), Phrase('"test"'), ), ), ) auto_name(tree) # and and1 = tree self.assertEqual(get_name(and1), "0") # - or or1 = and1.children[0] self.assertEqual(get_name(or1), "0_0") # --- search field word sfield1 = or1.children[0] self.assertFalse(get_name(sfield1)) self.assertEqual(get_name(sfield1.expr), "0_0_0") # --- and and2 = or1.children[1] self.assertEqual(get_name(and2), "0_0_1") # ----- proximity phrase self.assertEqual(get_name(and2.children[0].term), "0_0_1_0") # ----- search field word sfield2 = and2.children[1] self.assertFalse(get_name(sfield2)) self.assertEqual(get_name(sfield2.expr), "0_0_1_1") # - group group1 = and1.children[1] self.assertEqual(get_name(group1), None) # --- unknown op unknownop1 = group1.children[0] self.assertEqual(get_name(unknownop1), "0_1") # ----- fuzzy word self.assertEqual(get_name(unknownop1.children[0].term), "0_1_0") # ----- phrase self.assertEqual(get_name(unknownop1.children[1]), "0_1_1")
def test_auto_name_integration(self): tree = (AndOperation( SearchField("text", Phrase('"foo bar"')), Group(OrOperation( Word("bar"), SearchField("spam", Word("baz")), ), ), )) auto_name(tree) expected = { 'bool': { 'must': [{ 'term': { 'text': { '_name': 'a', 'value': 'foo bar' } } }, { 'bool': { 'should': [{ 'term': { 'text': { '_name': 'c', 'value': 'bar' } } }, { 'match': { 'spam': { '_name': 'd', 'query': 'baz', 'zero_terms_query': 'none' } } }] } }] } } result = self.transformer(tree) self.assertEqual(result, expected)
def test_complex(self): tree = Group( OrOperation( SearchField( "foo", FieldGroup( UnknownOperation(Word("bar"), Range(Word("baz"), Word("spam")))), ), Not(Proximity(Phrase('"ham ham"'), 2)), Plus(Fuzzy(Word("hammer"), 3)), )) self.assertEqual(str(tree), '(foo:(bar[bazTOspam])ORNOT"ham ham"~2OR+hammer~3)') self.assertEqual( str(auto_head_tail(tree)), '(foo:(bar [baz TO spam]) OR NOT "ham ham"~2 OR +hammer~3)', ) # idem potent self.assertEqual( str(auto_head_tail(auto_head_tail(tree))), '(foo:(bar [baz TO spam]) OR NOT "ham ham"~2 OR +hammer~3)', )
def test_element_from_path(self): tree = AndOperation( OrOperation( SearchField("bar", Word("test")), Group( AndOperation( Proximity(Phrase('"test"'), 2), SearchField("baz", Word("test")), Fuzzy(Word("test")), Phrase('"test"'), ), ), ), ) names = { "a": (), "b": (0, 1), "c": (0, 1, 0, 2), "d": (0, 1, 0, 2, 0), "e": (0, 1, 0, 3) } self.assertEqual(element_from_path(tree, ()), tree) self.assertEqual(element_from_name(tree, "a", names), tree) self.assertEqual(element_from_path(tree, (0, 1)), tree.children[0].children[1]) self.assertEqual(element_from_name(tree, "b", names), tree.children[0].children[1]) self.assertEqual(element_from_path(tree, (0, 1, 0, 2)), Fuzzy(Word("test"))) self.assertEqual(element_from_name(tree, "c", names), Fuzzy(Word("test"))) self.assertEqual(element_from_path(tree, (0, 1, 0, 2, 0)), Word("test")) self.assertEqual(element_from_name(tree, "d", names), Word("test")) self.assertEqual(element_from_path(tree, (0, 1, 0, 3)), Phrase('"test"')) self.assertEqual(element_from_name(tree, "e", names), Phrase('"test"')) with self.assertRaises(IndexError): element_from_path(tree, (1, ))
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", }, )
class TestPrettify(TestCase): big_tree = AndOperation( Group(OrOperation(Word("baaaaaaaaaar"), Word("baaaaaaaaaaaaaz"))), Word("fooooooooooo")) fat_tree = AndOperation( SearchField( "subject", FieldGroup( OrOperation( Word("fiiiiiiiiiiz"), AndOperation(Word("baaaaaaaaaar"), Word("baaaaaaaaaaaaaz"))))), AndOperation(Word("fooooooooooo"), Word("wiiiiiiiiiz"))) def test_one_liner(self): tree = AndOperation(Group(OrOperation(Word("bar"), Word("baz"))), Word("foo")) self.assertEqual(prettify(tree), "( bar OR baz ) AND foo") def test_with_unknown_op(self): prettify = Prettifier(indent=8, max_len=20) tree = UnknownOperation( Group( UnknownOperation(Word("baaaaaaaaaar"), Word("baaaaaaaaaaaaaz"))), Word("fooooooooooo")) self.assertEqual( "\n" + prettify(tree), """ ( baaaaaaaaaar baaaaaaaaaaaaaz ) fooooooooooo""") def test_with_unknown_op_nested(self): prettify = Prettifier(indent=8, max_len=20) tree = OrOperation( UnknownOperation(Word("baaaaaaaaaar"), Word("baaaaaaaaaaaaaz")), Word("fooooooooooo")) self.assertEqual( "\n" + prettify(tree), """ baaaaaaaaaar baaaaaaaaaaaaaz OR fooooooooooo""") def test_small(self): prettify = Prettifier(indent=8, max_len=20) self.assertEqual( "\n" + prettify(self.big_tree), """ ( baaaaaaaaaar OR baaaaaaaaaaaaaz ) AND fooooooooooo""") self.assertEqual( "\n" + prettify(self.fat_tree), """ subject: ( fiiiiiiiiiiz OR baaaaaaaaaar AND baaaaaaaaaaaaaz ) AND fooooooooooo AND wiiiiiiiiiz""") def test_small_inline_ops(self): prettify = Prettifier(indent=8, max_len=20, inline_ops=True) self.assertEqual( "\n" + prettify(self.big_tree), """ ( baaaaaaaaaar OR baaaaaaaaaaaaaz ) AND fooooooooooo""") self.assertEqual( "\n" + prettify(self.fat_tree), """ subject: ( fiiiiiiiiiiz OR baaaaaaaaaar AND baaaaaaaaaaaaaz ) AND fooooooooooo AND wiiiiiiiiiz""") def test_normal(self): prettify = Prettifier(indent=4, max_len=50) self.assertEqual( "\n" + prettify(self.big_tree), """ ( baaaaaaaaaar OR baaaaaaaaaaaaaz ) AND fooooooooooo""") self.assertEqual( "\n" + prettify(self.fat_tree), """ subject: ( fiiiiiiiiiiz OR baaaaaaaaaar AND baaaaaaaaaaaaaz ) AND fooooooooooo AND wiiiiiiiiiz""") def test_normal_inline_ops(self): prettify = Prettifier(indent=4, max_len=50, inline_ops=True) self.assertEqual( "\n" + prettify(self.big_tree), """ ( baaaaaaaaaar OR baaaaaaaaaaaaaz ) AND fooooooooooo""") self.assertEqual( "\n" + prettify(self.fat_tree), """ subject: ( fiiiiiiiiiiz OR baaaaaaaaaar AND baaaaaaaaaaaaaz ) AND fooooooooooo AND wiiiiiiiiiz""")
def test_one_liner(self): tree = AndOperation(Group(OrOperation(Word("bar"), Word("baz"))), Word("foo")) self.assertEqual(prettify(tree), "( bar OR baz ) AND foo")