def test_term_iter_wildcard(self): self.assertListEqual( list(Term(r"a?b\*or*and\?").iter_wildcards()), [((1, 2), "?"), ((7, 8), "*")], ) self.assertListEqual( list(Term(r"\**\**").iter_wildcards()), [((2, 3), "*"), ((5, 6), "*")], )
def test_term_split_wildcard(self): self.assertListEqual( Term(r"a??b\*or*and\?").split_wildcards(), ["a", "?", "", "?", r"b\*or", "*", r"and\?"], ) self.assertListEqual( Term(r"\**\**").split_wildcards(), [r"\*", "*", r"\*", "*", ""], )
def get_term_value(node: Term) -> str: """ Retrieves unescaped value from Term with transformed wildcards to the SQL form :param node: luqum.Term object :return: Unescaped value """ if node.has_wildcard(): wildcard_map = { "*": "%", "?": "_" } # Escape already contained SQL wildcards node_value = re.sub(r"([%_])", r"\\\1", node.value) # Transform unescaped Lucene wildcards to SQL form node_value = Term.WILDCARDS_PATTERN.sub( lambda m: wildcard_map[m.group(0)], node_value ) # Unescape Lucene escaped special characters node_value = Term.WORD_ESCAPED_CHARS.sub( r'\1', node_value ) return node_value else: return node.unescaped_value
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_term_is_only_a_wildcard(self): self.assertTrue(Term('*').is_wildcard()) self.assertFalse(Term('*o').is_wildcard()) self.assertFalse(Term('b*').is_wildcard()) self.assertFalse(Term('b*o').is_wildcard()) self.assertFalse(Term('?').is_wildcard())
def test_term_wildcard_false(self): self.assertFalse(Term("bar").has_wildcard()) self.assertFalse(Term(r"bar\*").has_wildcard()) self.assertFalse(Term(r"b\?r\*").has_wildcard())
def test_term_wildcard_true(self): self.assertTrue(Term("ba*").has_wildcard()) self.assertTrue(Term("b*r").has_wildcard()) self.assertTrue(Term("*ar").has_wildcard()) self.assertTrue(Term("ba?").has_wildcard()) self.assertTrue(Term("b?r").has_wildcard()) self.assertTrue(Term("?ar").has_wildcard()) self.assertTrue(Term("?a*").has_wildcard()) self.assertTrue(Term(r"\?a*").has_wildcard()) self.assertTrue(Term(r"?a\*").has_wildcard()) self.assertTrue(Term("*").has_wildcard()) self.assertTrue(Term("?").has_wildcard())