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_equality_range_different_terms(self): r1 = Range(Word("20"), Word("40"), include_low=True, include_high=True) self.assertNotEqual( r1, Range(Word("30"), Word("40"), include_low=True, include_high=True)) self.assertNotEqual( r1, Range(Word("20"), Word("30"), include_low=True, include_high=True))
def visit_range(self, node: Range, parents: List[Item], context: SQLQueryBuilderContext) -> Range: """ Visitor for Range - inclusive [<Term> TO <Term>] - exclusive [<Term> TO <Term>] """ node.low = self.visit(node.low, parents + [node], context) node.high = self.visit(node.high, parents + [node], context) return node
def test_range(self): tree = (AndOperation( SearchField("foo", Range(Word("10", tail=" "), Word("100", head=" "), True, True), tail=" "), SearchField("bar", Range(Word("a*", tail=" "), Word("*", head=" "), True, False), head=" "))) parsed = parser.parse('foo:[10 TO 100] AND bar:[a* TO *}') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def visit_range(self, node: Range, parents: List[Item], context: SQLQueryBuilderContext) -> Range: """ Visitor for Range - inclusive [<Term> TO <Term>] - exclusive {<Term> TO <Term>} """ if not context.field_mapper.accepts_range: raise UnsupportedGrammarException( "Range queries are not supported for this type of field") node.low = self.visit(node.low, parents + [node], context) node.high = self.visit(node.high, parents + [node], context) return node
def visit_term(self, node: T, parents: List[Item], context: SQLQueryBuilderContext) -> Union[T, Range]: """ Visitor for Term (Word and Phrase). - checks if field is already set - performs wildcard mapping and unescaping - wildcards are not allowed inside ranges Returns mapped node """ if context.field_mapper is None: raise FieldNotQueryableException( "You have to specify field, check help for more information") is_range_term = isinstance(parents[-1], Range) if node.has_wildcard() and is_range_term and str(node) != "*": raise UnsupportedGrammarException( "Wildcards other than * are not supported in range queries") if context.field_mapper.accepts_range and not is_range_term: if node.value.startswith(">="): node.value = node.value[2:] return Range(low=node, high=Term("*"), include_low=True, include_high=False) elif node.value.startswith(">"): node.value = node.value[1:] return Range(low=node, high=Term("*"), include_low=False, include_high=False) elif node.value.startswith("<="): node.value = node.value[2:] return Range(low=Term("*"), high=node, include_low=False, include_high=True) elif node.value.startswith("<"): node.value = node.value[1:] return Range(low=Term("*"), high=node, include_low=False, include_high=False) return node
def test_flavours(self): tree = SearchField( "somedate", Range(Word("now/d-1d+7H", tail=" "), Word("now/d+7H", head=" "), True, True)) parsed = parser.parse('somedate:[now/d-1d+7H TO now/d+7H]') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_range(self): orig = Range(Word("foo"), Word("bar"), include_low=False, pos=3, head="\n", tail="\t") copy = orig.clone_item() self.assert_equal_tail_head_pos(orig, copy) self.assertEqual(orig.include_low, copy.include_low) self.assertEqual(orig.include_high, copy.include_high) self.assertEqual(copy.low, NONE_ITEM) self.assertEqual(copy.high, NONE_ITEM) self.assertNotEqual(orig, copy) copy.children = [Word("foo"), Word("bar")] self.assertEqual(orig, copy)
def test_date_in_range(self): # juste one funky expression tree = SearchField( "foo", Range(Word(r"2015-12-19||+2\d", tail=" "), Word(r"now+3d+12h\h", head=" "))) parsed = parser.parse(r'foo:[2015-12-19||+2\d TO now+3d+12h\h]') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed)
def test_auto_head_tail_no_change_to_existing(self): tree = AndOperation( Range(Word("foo", tail="\t"), Word("bar", head="\n"), tail="\r"), Not(Word("baz", head="\t\t"), head="\n\n", tail="\r\r"), Word("spam", head="\t\n"), ) self.assertEqual(str(tree), "[foo\tTO\nbar]\rAND\n\nNOT\t\tbaz\r\rAND\t\nspam") self.assertEqual(str(auto_head_tail(tree)), "[foo\tTO\nbar]\rAND\n\nNOT\t\tbaz\r\rAND\t\nspam")
def test_auto_name_one_term(self): tree = Word("test") auto_name(tree) self.assertEqual(get_name(tree), "0") tree = Phrase('"test"') auto_name(tree) self.assertEqual(get_name(tree), "0") tree = Range("test", "*") auto_name(tree) self.assertEqual(get_name(tree), "0")
def test_named_queries_range(self): tree = SearchField("text", Range(Word("x"), Word("z"))) set_name(tree, "a") result = self.transformer(tree) self.assertEqual( result, {'range': { 'text': { '_name': 'a', 'gte': 'x', 'lte': 'z' } }})
def test_equality_range_different_bounds(self): """ Regression test for a bug on range equalities. """ r1 = Range(Word("20"), Word("40"), include_low=True, include_high=True) r2 = Range(Word("20"), Word("40"), include_low=False, include_high=True) r3 = Range(Word("20"), Word("40"), include_low=True, include_high=False) r4 = Range(Word("20"), Word("40"), include_low=False, include_high=False) self.assertNotEqual(r1, r2) self.assertNotEqual(r1, r3) self.assertNotEqual(r1, r4) self.assertNotEqual(r2, r3) self.assertNotEqual(r2, r4) self.assertNotEqual(r3, r4)
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_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_range(self): tree = parser.parse("\r{\tfoo\nTO bar\r\n]\t\t") self.assertEqual(tree, Range(Word("foo"), Word("bar"), include_low=False)) self.assertEqual(tree.head, "\r") self.assertEqual(tree.tail, "\t\t") self.assertEqual(tree.pos, 1) self.assertEqual(tree.size, 16) foo, bar = tree.children self.assertEqual(foo.head, "\t") self.assertEqual(foo.tail, "\n") self.assertEqual(foo.pos, 3) self.assertEqual(foo.size, 3) self.assertEqual(bar.head, " ") self.assertEqual(bar.tail, "\r\n") self.assertEqual(bar.pos, 11) self.assertEqual(bar.size, 3) self.assertEqual(str(tree), "{\tfoo\nTO bar\r\n]") self.assertEqual(tree.__str__(head_tail=True), "\r{\tfoo\nTO bar\r\n]\t\t")
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": ()})
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_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_equality_range(self): r1 = Range(Word("20"), Word("40"), include_low=True, include_high=True) r2 = Range(Word("20"), Word("40"), include_low=True, include_high=True) self.assertEqual(r1, r2)
def test_range(self): tree = Range(Word("foo"), Word("bar")) self.assertEqual(str(tree), "[fooTObar]") self.assertEqual(str(auto_head_tail(tree)), "[foo TO bar]")