Ejemplo n.º 1
0
def is_query_valid(query):
    ast = parser.parse(query)
    visitor = KeyExistenceValidatorVisitor(ast)
    try:
        visitor.evaluate()
    except DQValidationError:
        return False
    return True
    def test_visit_binary_ops(self):
        parser = DataQueryParser()
        ast = parser.parse('12 == 45')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('hello == 3')
        visitor = KeyExistenceValidatorVisitor(ast)
        self.assertTrue(visitor.evaluate())

        ast = parser.parse('3 == hello')
        visitor = KeyExistenceValidatorVisitor(ast)
        self.assertTrue(visitor.evaluate())
Ejemplo n.º 3
0
def match(data, query):
    """Checks if `data` object satisfies `query`"""
    ast = parser.parse(query)
    # validation
    KeyExistenceValidatorVisitor(ast).evaluate()

    dq = DataQueryVisitor(ast)
    return dq.evaluate(data)
Ejemplo n.º 4
0
def compile(query,
            use_nested_keys=True,
            key_separator='.',
            case_sensitive=True,
            raise_keyerror=False,
            validate=True):
    """Builder parses query and returns configured reusable DataQueryVisitor object."""
    ast = parser.parse(query)
    if validate:
        KeyExistenceValidatorVisitor(ast).evaluate()
    return DataQueryVisitor(ast,
                            use_nested_keys=use_nested_keys,
                            key_separator=key_separator,
                            case_sensitive=case_sensitive,
                            raise_keyerror=raise_keyerror)
    def test_visit_not(self):
        parser = DataQueryParser()
        ast = parser.parse('NOT 3')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('NOT False')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('NOT hello')
        visitor = KeyExistenceValidatorVisitor(ast)
        self.assertTrue(visitor.evaluate())
    def test_visit_or(self):
        parser = DataQueryParser()
        ast = parser.parse('12 OR 45')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('hello OR 45')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('hello == 3 OR 45')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('hello == 3 OR world > 3')
        visitor = KeyExistenceValidatorVisitor(ast)
        self.assertTrue(visitor.evaluate())
    def test_visit_values(self):
        parser = DataQueryParser()
        ast = parser.parse('12')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('"hello"')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('NONE')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('True')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse('NOW')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()

        ast = parser.parse(r'/\d+/')
        visitor = KeyExistenceValidatorVisitor(ast)
        with self.assertRaises(DQValidationError):
            visitor.evaluate()