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_approx(self): tree = (UnknownOperation( Proximity(Phrase('"foo bar"'), 3, tail=" "), Proximity(Phrase('"foo baz"'), None, tail=" "), Fuzzy(Word('baz'), Decimal("0.3"), tail=" "), Fuzzy(Word('fou'), None))) parsed = parser.parse('"foo bar"~3 "foo baz"~ baz~0.3 fou~') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_proximity(self): orig = Proximity(Word("bar"), degree=3, pos=3, head="\n", tail="\t") copy = orig.clone_item() self.assert_equal_tail_head_pos(orig, copy) self.assertEqual(orig.degree, copy.degree) self.assertEqual(copy.term, NONE_ITEM) self.assertNotEqual(orig, copy) copy.children = [Word("bar")] self.assertEqual(orig, copy)
def test_parents_tracking(self): tree = AndOperation(Word("foo"), Proximity(Phrase('"bar"'), 2)) visitor = self.TrackingParentsVisitor(track_parents=True) nodes = visitor.visit(tree) self.assertListEqual( [ (tree, None), (Word("foo"), (tree, )), (Proximity(Phrase('"bar"'), degree=2), (tree, )), (Phrase('"bar"'), (tree, Proximity(Phrase('"bar"'), 2))), ], nodes, )
def test_equality_proximty(self): """ Regression test for a bug on approx equalities. .. todo:: Testing other tokens might be a good idea... """ p1 = Proximity(term=Word('foo'), degree=5) p2 = Proximity(term=Word('bar'), degree=5) p3 = Proximity(term=Word('foo'), degree=5) p4 = Proximity(term=Word('foo'), degree=1) p5 = Proximity(term=Word('foo'), degree=None) self.assertNotEqual(p1, p2) self.assertEqual(p1, p3) self.assertNotEqual(p1, p4) self.assertEqual(p4, p5)
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_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_proximity(self): item = Proximity(Word("foo"), degree=None) self.assertEqual(str(item), "foo~") self.assertEqual(repr(item), "Proximity(Word('foo'), 1)") self.assertEqual(item.degree, 1) item = Proximity(Word("foo"), degree="1") self.assertEqual(str(item), "foo~1") self.assertEqual(repr(item), "Proximity(Word('foo'), 1)") item = Proximity(Word("foo"), degree="4") self.assertEqual(str(item), "foo~4") self.assertEqual(repr(item), "Proximity(Word('foo'), 4)") # head tail item = Proximity(Word("foo", head="\t", tail="\n"), head="\r", tail=" ") self.assertEqual(str(item), "\tfoo\n~") self.assertEqual(item.__str__(head_tail=True), "\r\tfoo\n~ ")
def test_proximity(self): tree = parser.parse('\r"foo"\t~2\n') self.assertEqual(tree, Proximity(Phrase('"foo"'), 2)) self.assertEqual(tree.head, "") self.assertEqual(tree.tail, "\n") self.assertEqual(tree.pos, 0) self.assertEqual(tree.size, 9) foo, = tree.children self.assertEqual(foo.head, "\r") self.assertEqual(foo.tail, "\t") self.assertEqual(foo.pos, 1) self.assertEqual(foo.size, 5) self.assertEqual(str(tree), '\r"foo"\t~2') self.assertEqual(tree.__str__(head_tail=True), '\r"foo"\t~2\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_named_queries_proximity(self): tree = SearchField("spam", Proximity(Phrase('"foo bar"'))) set_name(tree.children[0], "a") result = self.transformer(tree) self.assertEqual( result, { "match_phrase": { "spam": { "query": "foo bar", "_name": "a", 'slop': 1.0 } } }, )
def test_no_propagation(self): for tree in [ Range(Word("a"), Word("b")), Fuzzy(Word("foo")), Proximity('"bar baz"', 2) ]: with self.subTest("%r" % type(tree)): paths_ok, paths_ko = self.propagate_matching(tree, set(), {()}) # no down propagation self.assertEqual(paths_ok, set()) self.assertEqual(paths_ko, {()}) paths_ok, paths_ko = self.propagate_matching(tree, {()}, set()) self.assertEqual(paths_ok, {()}) self.assertEqual(paths_ko, set())
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_combinations(self): # self.assertEqual(parser.parse("subject:test desc:(house OR car)").pval, "") tree = (UnknownOperation( SearchField("subject", Word("test"), tail=" "), AndOperation( SearchField("desc", FieldGroup( OrOperation(Word("house", tail=" "), Word("car", head=" "))), tail=" "), Not(Proximity(Phrase('"approximatly this"'), 3, head=" "), head=" ")))) parsed = parser.parse( 'subject:test desc:(house OR car) AND NOT "approximatly this"~3') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
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_proximity_non_phrase(self): check = LuceneCheck() query = Proximity(Word("foo"), "2") self.assertFalse(check(query)) self.assertEqual(len(check.errors(query)), 1) self.assertIn("phrase", check.errors(query)[0])