Ejemplo n.º 1
0
 def test_lucene_resolution_simple(self):
     tree = (UnknownOperation(Word("a"), Word("b"),
                              UnknownOperation(Word("c"), Word("d"))))
     expected = (AndOperation(Word("a"), Word("b"),
                              AndOperation(Word("c"), Word("d"))))
     resolver = UnknownOperationResolver(resolve_to=None)
     self.assertEqual(resolver(tree), expected)
Ejemplo n.º 2
0
    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""")
Ejemplo n.º 3
0
 def test_lucene_resolution_last_op(self):
     tree = (OrOperation(
         Word("a"), Word("b"), UnknownOperation(Word("c"), Word("d")),
         AndOperation(Word("e"), UnknownOperation(Word("f"), Word("g"))),
         UnknownOperation(Word("i"), Word("j")),
         OrOperation(Word("k"), UnknownOperation(Word("l"), Word("m"))),
         UnknownOperation(Word("n"), Word("o"))))
     expected = (OrOperation(
         Word("a"), Word("b"), OrOperation(Word("c"), Word("d")),
         AndOperation(Word("e"), AndOperation(Word("f"), Word("g"))),
         AndOperation(Word("i"), Word("j")),
         OrOperation(Word("k"), OrOperation(Word("l"), Word("m"))),
         OrOperation(Word("n"), Word("o"))))
     resolver = UnknownOperationResolver(resolve_to=None)
     self.assertEqual(resolver(tree), expected)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 def test_named_queries_unknown(self):
     tree = UnknownOperation(SearchField("text", Word("foo")),
                             SearchField("spam", Word("bar")))
     set_name(tree.operands[0], "a")
     set_name(tree.operands[1], "b")
     result = self.transformer(tree)
     self.assertEqual(
         result, {
             'bool': {
                 'should': [{
                     'term': {
                         'text': {
                             '_name': 'a',
                             'value': 'foo'
                         }
                     }
                 }, {
                     'match': {
                         'spam': {
                             '_name': 'b',
                             'query': 'bar',
                             'zero_terms_query': 'none'
                         }
                     }
                 }]
             }
         })
Ejemplo n.º 6
0
 def test_and_resolution(self):
     tree = (UnknownOperation(Word("a"), Word("b"),
                              OrOperation(Word("c"), Word("d"))))
     expected = (AndOperation(Word("a"), Word("b"),
                              OrOperation(Word("c"), Word("d"))))
     resolver = UnknownOperationResolver(resolve_to=AndOperation)
     self.assertEqual(resolver(tree), expected)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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))
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
    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""")
Ejemplo n.º 11
0
 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")])
Ejemplo n.º 12
0
    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")
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
 def test_unknown_operator(self):
     tree = parser.parse("\tfoo\nbar\r")
     self.assertEqual(tree, UnknownOperation(Word("foo"), Word("bar")))
     self.assertEqual(tree.head, "")
     self.assertEqual(tree.tail, "")
     self.assertEqual(tree.pos, 0)
     self.assertEqual(tree.size, 9)
     foo, bar = tree.children
     self.assertEqual(foo.head, "\t")
     self.assertEqual(foo.tail, "\n")
     self.assertEqual(foo.pos, 1)
     self.assertEqual(foo.size, 3)
     self.assertEqual(bar.head, "")
     self.assertEqual(bar.tail, "\r")
     self.assertEqual(bar.pos, 5)
     self.assertEqual(bar.size, 3)
     self.assertEqual(str(tree), "\tfoo\nbar\r")
     self.assertEqual(tree.__str__(head_tail=True), "\tfoo\nbar\r")
Ejemplo n.º 15
0
    def test_unknown_operation(self):
        tree = UnknownOperation(Word("foo"), Phrase('"bar"'), Word("baz"))

        tree_or = OrOperation(Word("foo"), Phrase('"bar"'), Word("baz"))
        tree_and = AndOperation(Word("foo"), Phrase('"bar"'), Word("baz"))
        propagate_or = self.propagate_matching
        propagate_and = MatchingPropagator(default_operation=AndOperation)
        all_paths = {(0, ), (1, ), (2, )}

        for matching in [
                set(), {(2, )}, {(0, ), (2, )}, {(0, ), (1, ), (2, )}
        ]:
            self.assertEqual(
                propagate_or(tree, matching),
                self.propagate_matching(tree_or, matching,
                                        matching - all_paths),
            )
            self.assertEqual(
                propagate_and(tree, matching),
                self.propagate_matching(tree_and, matching,
                                        matching - all_paths),
            )
Ejemplo n.º 16
0
 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)',
     )
Ejemplo n.º 17
0
 def test_implicit_operations(self):
     tree = (UnknownOperation(Word("foo", tail=" "), Word("bar")))
     parsed = parser.parse("foo bar")
     self.assertEqual(str(parsed), str(tree))
     self.assertEqual(parsed, tree)
Ejemplo n.º 18
0
 def test_unknown_operation(self):
     tree = UnknownOperation(Word("foo"), Word("bar"), Word("baz"))
     self.assertEqual(str(tree), "foobarbaz")
     self.assertEqual(str(auto_head_tail(tree)), "foo bar baz")
Ejemplo n.º 19
0
 def test_unknown_operation(self):
     tree = UnknownOperation(Word("foo", tail=" "), Word("bar", tail=" "),
                             Word("baz"))
     self.assertEqual(str(tree), "foo bar baz")