def _init_and_parse(self, input_str): conf = { 'class': 'plyse.term_parser.TermParser', 'default_fields': ['name', 'description'], 'aliases': { 'id': '__id__', 'type': '__type__' }, 'integer_as_string': False } term_parser = TermParserFactory.build_from_conf(conf) g = GrammarFactory.build_default(term_parser) g.add_value_type(IntegerComparison(term_parser.integer_comparison_parse)) self.assertTrue(g.parse(input_str, True)) return g.parse(input_str, True)[0]
def _init_and_parse(self, input_str): conf = { 'class': 'plyse.term_parser.TermParser', 'default_fields': ['name', 'description'], 'aliases': { 'id': '__id__', 'type': '__type__' }, 'integer_as_string': False } term_parser = TermParserFactory.build_from_conf(conf) g = GrammarFactory.build_default(term_parser) g.add_value_type( IntegerComparison(term_parser.integer_comparison_parse)) self.assertTrue(g.parse(input_str, True)) return g.parse(input_str, True)[0]
def init_and_parse(self, input_str): qp = QueryParser(GrammarFactory.build_default()) self.assert_(qp.parse(input_str)) return qp.parse(input_str).query_as_tree
def _build_grammar(self): return GrammarFactory.build_from_conf(conf)
def _build_grammar(self): return GrammarFactory.build_default()
class QueryParserTester(unittest.TestCase): qp = QueryParser(GrammarFactory.build_default()) def test_init_query(self): q = self.qp.parse("name:plyse") self.assertEqual('name:plyse', q.raw_query) self.assertTrue(isinstance(q.query_as_tree, Operand)) self.assertEqual(1, len(q._stack_map)) self.assertTrue(0 in q._stack_map) self.assertTrue(isinstance(q.query_from_stack(0)[0], Operand)) self.assertEqual('name:plyse', q.query_from_stack(0)[1]) self.assertEqual(1, len(q.terms())) self.assertEqual('name', q.terms()[0].field) self.assertEqual('plyse', q.terms()[0].val) def test_stack_query(self): q = self.qp.parse("name:plyse") q2 = q.stack(self.qp.parse("ask:gently")) # query is immutable, thus self.assertEqual('name:plyse', q.raw_query) self.assertEqual('(name:plyse) and (ask:gently)', q2.raw_query) self.assertTrue(And.type, q2.query_as_tree.type) self.assertEqual(2, len(q2._stack_map)) self.assertTrue(0 in q2._stack_map) self.assertTrue(1 in q2._stack_map) self.assertEqual(2, len(q2.terms())) self.assertEqual('name', q2.terms()[0].field) self.assertEqual('plyse', q2.terms()[0].val) self.assertEqual('ask', q2.terms()[1].field) self.assertEqual('gently', q2.terms()[1].val) self.assertTrue(isinstance(q2.query_from_stack(0)[0], Operand)) self.assertEqual('name:plyse', q.query_from_stack(0)[1]) self.assertTrue(isinstance(q2.query_from_stack(1)[0], Operand)) self.assertEqual('ask:gently', q2.query_from_stack(1)[1]) def test_stack_negated_query(self): q = self.qp.parse("name:plyse") q2 = q.stack(self.qp.parse("-ask:gently")) # query is immutable, thus self.assertEqual('name:plyse', q.raw_query) self.assertEqual('(name:plyse) and (-ask:gently)', q2.raw_query) self.assertTrue(And.type, q2.query_as_tree.type) self.assertEqual(2, len(q2._stack_map)) self.assertTrue(0 in q2._stack_map) self.assertTrue(1 in q2._stack_map) self.assertEqual(1, len(q2.terms(ignore_negated=True))) self.assertEqual('name', q2.terms()[0].field) self.assertEqual('plyse', q2.terms()[0].val) self.assertTrue(isinstance(q2.query_from_stack(0)[0], Operand)) self.assertEqual('name:plyse', q2.query_from_stack(0)[1]) self.assertTrue(isinstance(q2.query_from_stack(1)[0], Operator)) self.assertEqual('-ask:gently', q2.query_from_stack(1)[1])
def init_and_parse(self, input_str, fail_if_syntax_mismatch=False): qp = QueryParser(GrammarFactory.build_default()) self.assert_(qp.parse(input_str)) return qp.parse(input_str, fail_if_syntax_mismatch).query_as_tree