def _query_paper_id(term: str, operator: str = "and") -> Q: operator = operator.lower() logger.debug(f"query paper ID with: {term}") q = Q_("match", "paper_id", escape(term), operator=operator) | Q_( "match", "paper_id_v", escape(term), operator=operator) if is_old_papernum(term): q |= Q("wildcard", paper_id=f"*/{term}") return q
def test_wildcard_at_opening_of_string(self): """A wildcard character is the first character in the querystring.""" with self.assertRaises(index.QueryError): wildcard_escape("*nope") with self.assertRaises(index.QueryError): Q_('match', 'title', '*nope')
def test_match_any_wildcard_in_literal(self): """A * wildcard is present in a string literal.""" qs = '"Foo t*"' qs_escaped, wildcard = wildcard_escape(qs) self.assertEqual(qs_escaped, '"Foo t\*"', "Wildcard should be escaped") self.assertFalse(wildcard, "Wildcard should not be detected") self.assertIsInstance(Q_('match', 'title', qs), type(index.Q('match', title='"Foo t\*"')), "Wildcard Q object should not be generated")
def test_match_any_wildcard_is_present(self): """A * wildcard is present in the query.""" qs = "Foo t*" qs_escaped, wildcard = wildcard_escape(qs) self.assertTrue(wildcard, "Wildcard should be detected") self.assertEqual(qs, qs_escaped, "The querystring should be unchanged") self.assertIsInstance(Q_('match', 'title', qs), type(index.Q('wildcard', title=qs)), "Wildcard Q object should be generated")
def test_mixed_wildcards_in_literal(self): """Both * and ? characters are present in a string literal.""" qs = '"Fo? t*"' qs_escaped, wildcard = wildcard_escape(qs) self.assertEqual(qs_escaped, '"Fo\? t\*"', "Both wildcards should be escaped") self.assertFalse(wildcard, "Wildcard should not be detected") self.assertIsInstance(Q_('match', 'title', qs), type(index.Q('match', title='"Fo\? t\*"')), "Wildcard Q object should not be generated")
def test_wildcards_both_inside_and_outside_literal(self): """Wildcard characters are present both inside and outside literal.""" qs = '"Fo? t*" said the *' qs_escaped, wildcard = wildcard_escape(qs) self.assertEqual(qs_escaped, '"Fo\? t\*" said the *', "Wildcards in literal should be escaped") self.assertTrue(wildcard, "Wildcard should be detected") self.assertIsInstance( Q_('match', 'title', qs), type(index.Q('wildcard', title='"Fo\? t\*" said the *')), "Wildcard Q object should be generated")
def test_multiple_match_any_wildcard_in_literal(self): """Multiple * wildcards are present in a string literal.""" qs = '"Fo*o t*"' qs_escaped, wildcard = wildcard_escape(qs) self.assertEqual(qs_escaped, r'"Fo\*o t\*"', "Both wildcards should be escaped") self.assertFalse(wildcard, "Wildcard should not be detected") self.assertIsInstance( Q_("match", "title", qs), type(index.Q("match", title=r'"Fo\*o t\*"')), "Wildcard Q object should not be generated", )
def test_wildcards_inside_outside_multiple_literals(self): """Wildcard chars are everywhere, and there are multiple literals.""" qs = '"Fo?" s* "yes*" o?' qs_escaped, wildcard = wildcard_escape(qs) self.assertEqual(qs_escaped, '"Fo\?" s* "yes\*" o?', "Wildcards in literal should be escaped") self.assertTrue(wildcard, "Wildcard should be detected") self.assertIsInstance( Q_('match', 'title', qs), type(index.Q('wildcard', title='"Fo\?" s* "yes\*" o?')), "Wildcard Q object should be generated")