Ejemplo n.º 1
0
 def test_regex(self):
     tree = (AndOperation(
         Regex('/a regex (with some.*match+ing)?/', tail=" "),
         Regex('/Another one/', head=" ")))
     parsed = parser.parse(
         '/a regex (with some.*match+ing)?/ AND /Another one/')
     self.assertEqual(str(parsed), str(tree))
     self.assertEqual(parsed, tree)
Ejemplo n.º 2
0
 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")])
Ejemplo n.º 3
0
 def test_regex(self):
     tree = parser.parse('\t/foo/\r')
     self.assertEqual(tree, Regex('/foo/'))
     self.assertEqual(tree.head, "\t")
     self.assertEqual(tree.tail, "\r")
     self.assertEqual(tree.pos, 1)
     self.assertEqual(tree.size, 5)
     self.assertEqual(str(tree), '/foo/')
     self.assertEqual(tree.__str__(head_tail=True), '\t/foo/\r')
Ejemplo n.º 4
0
    def test_single_element(self):
        for tree in [Word("a"), Phrase('"a"'), Regex("/a/")]:
            with self.subTest("%r" % type(tree)):
                paths_ok, paths_ko = self.propagate_matching(tree, set())
                self.assertEqual(paths_ok, set(), {()})
                self.assertEqual(paths_ko, {()})

                paths_ok, paths_ko = self.propagate_matching(tree, {()})
                self.assertEqual(paths_ok, {()}, set())
                self.assertEqual(paths_ko, set())
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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/'),
     ])
Ejemplo n.º 7
0
    def test_auto_name_one_term(self):
        tree = Word("test")
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})

        tree = Phrase('"test"')
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})

        tree = Range(Word("test"), Word("*"))
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})

        tree = Regex("/test/")
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})
Ejemplo n.º 8
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.º 9
0
 def test_regex(self):
     orig = Regex("/foo/", pos=3, head="\n", tail="\t")
     copy = orig.clone_item()
     self.assert_equal_tail_head_pos(orig, copy)
     self.assertEqual(orig.value, copy.value)
     self.assertEqual(orig, copy)
Ejemplo n.º 10
0
    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",
            },
        )