def test_visit_in(self): parser = DataQueryParser() ast1 = parser.parse('"hello" IN "hello world"') ast2 = parser.parse('"hello1" IN "hello world"') dqv = DataQueryVisitor(ast1) self.assertEqual(ast1.accept(dqv), True) self.assertEqual(ast2.accept(dqv), False)
def test_visit_lt(self): parser = DataQueryParser() ast1 = parser.parse('12 < 23') ast2 = parser.parse('12 < 3') dqv = DataQueryVisitor(ast1) self.assertEqual(ast1.accept(dqv), True) self.assertEqual(ast2.accept(dqv), False)
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()
def test_visit_match(self): parser = DataQueryParser() ast1 = parser.parse(r'"hello world" MATCH /.*world.*/') ast2 = parser.parse(r'"hello world" MATCH /hello1/') dqv = DataQueryVisitor(ast1) self.assertEqual(ast1.accept(dqv), True) self.assertEqual(ast2.accept(dqv), False)
def test_visit_bool(self): parser = DataQueryParser() ast1 = parser.parse('TRUE') ast2 = parser.parse('FALSE') dqv = DataQueryVisitor(ast1) self.assertEqual(ast1.accept(dqv), True) self.assertEqual(ast2.accept(dqv), False)
def test_parse_key(self): parser = DataQueryParser() result = parser.parse('key1') self.assertIsInstance(result, KeyExpression) self.assertEqual(result.value, 'key1') with self.assertRaises(DQSyntaxError): result = parser.parse('key1.key2.key3')
def test_visit_contains(self): parser = DataQueryParser() ast1 = parser.parse('"hello world" CONTAINS "world"') ast2 = parser.parse('"hello world" CONTAINS "hello1"') dqv = DataQueryVisitor(ast1) self.assertEqual(ast1.accept(dqv), True) self.assertEqual(ast2.accept(dqv), False)
def test_parse_backtick_key(self): parser = DataQueryParser() result = parser.parse('`key1`') self.assertIsInstance(result, KeyExpression) self.assertEqual(result.value, 'key1') result = parser.parse('`key1.key2.key3`') self.assertIsInstance(result, KeyExpression) self.assertEqual(result.value, 'key1.key2.key3')
def test_visit_like(self): parser = DataQueryParser() ast1 = parser.parse('"hello world" LIKE "*world"') ast2 = parser.parse('"hello world" LIKE "hello?world"') ast3 = parser.parse('"hello world" LIKE "*admin*"') dqv = DataQueryVisitor(ast1) self.assertEqual(ast1.accept(dqv), True) self.assertEqual(ast2.accept(dqv), True) self.assertEqual(ast3.accept(dqv), False)
def test_visit_key(self): parser = DataQueryParser() ast1 = parser.parse('hello') ast2 = parser.parse('world') ast3 = parser.parse('`qwerty.x`') dqv = DataQueryVisitor(ast1) dqv.data = {'hello': 'world', 'qwerty': {'x': 42}} self.assertEqual(ast1.accept(dqv).values, ["world"]) self.assertEqual(ast2.accept(dqv).values, []) self.assertEqual(ast3.accept(dqv).values, [42])
def test_visit_gte(self): parser = DataQueryParser() ast1 = parser.parse('23 >= 12') ast2 = parser.parse('3 >= 12') ast3 = parser.parse('12 >= 12') dqv = DataQueryVisitor(ast1) self.assertEqual(ast1.accept(dqv), True) self.assertEqual(ast2.accept(dqv), False) self.assertEqual(ast3.accept(dqv), 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())
def test_parse_array(self): parser = DataQueryParser() result = parser.parse('[34, 11, "hello", "world"]') result_types = [ NumberExpression, NumberExpression, StringExpression, StringExpression ] for val, t in zip(result.value, result_types): self.assertIsInstance(val, t) self.assertEqual(result.value[0].value, '34') self.assertEqual(result.value[1].value, '11') self.assertEqual(result.value[2].value, 'hello') self.assertEqual(result.value[3].value, 'world')
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_array(self): parser = DataQueryParser() ast = parser.parse('[12, "hello"]') dqv = DataQueryVisitor(ast) self.assertEqual(ast.accept(dqv), [12, 'hello'])
def test_visit_string(self): parser = DataQueryParser() ast = parser.parse('"hello"') dqv = DataQueryVisitor(ast) self.assertEqual(ast.accept(dqv), 'hello')
def test_parse_number(self): parser = DataQueryParser() result = parser.parse('35') self.assertIsInstance(result, NumberExpression) self.assertEqual(result.value, '35')
def test_visit_now(self): parser = DataQueryParser() ast = parser.parse('NOW') dqv = DataQueryVisitor(ast) self.assertIsInstance(ast.accept(dqv), datetime)
def test_visit_empty(self): parser = DataQueryParser() ast = parser.parse('') dqv = DataQueryVisitor(ast) self.assertFalse(dqv.evaluate({}))
def test_visit_none(self): parser = DataQueryParser() ast = parser.parse('NONE') dqv = DataQueryVisitor(ast) self.assertEqual(ast.accept(dqv), None)
def test_parse_bool(self): parser = DataQueryParser() result = parser.parse('FALSE') self.assertIsInstance(result, BooleanExpression) self.assertEqual(result.value, 'FALSE')
def test_parse_now(self): parser = DataQueryParser() result = parser.parse('NOW') self.assertIsInstance(result, NowExpression) self.assertEqual(result.value, 'NOW')
def test_parse_regexp(self): parser = DataQueryParser() result = parser.parse(r'/\d+/') self.assertIsInstance(result, RegexpExpression) self.assertEqual(result.value, r'\d+')
def test_parse_equal(self): parser = DataQueryParser() result = parser.parse('35 == 13') self.assertIsInstance(result, EqualExpression) self.assertEqual(result.left.value, '35') self.assertEqual(result.right.value, '13')
def test_parse_empty(self): parser = DataQueryParser() result = parser.parse('') self.assertIsNone(result)
def test_visit_regexp(self): parser = DataQueryParser() ast = parser.parse(r'/[abcd]+\d\s*finish/') dqv = DataQueryVisitor(ast) self.assertEqual(ast.accept(dqv).pattern, r'[abcd]+\d\s*finish')
from dictquery.datavalue import query_value from dictquery.exceptions import DQValidationError from dictquery.visitors import (DataQueryVisitor, MongoQueryVisitor, KeyExistenceValidatorVisitor) from dictquery.parsers import DataQueryParser __version__ = '0.4.0' parser = DataQueryParser() def is_query_valid(query): ast = parser.parse(query) visitor = KeyExistenceValidatorVisitor(ast) try: visitor.evaluate() except DQValidationError: return False return True def query_to_mongo(query, case_sensitive=True): """Converts DictQuery query to mongo query""" ast = parser.parse(query) mq = MongoQueryVisitor(ast, case_sensitive) return mq.evaluate() def compile(query, use_nested_keys=True, key_separator='.', case_sensitive=True,
def test_visit_number(self): parser = DataQueryParser() ast = parser.parse('12') dqv = DataQueryVisitor(ast) self.assertEqual(ast.accept(dqv), 12)
def test_parse_not_expr(self): parser = DataQueryParser() result = parser.parse('NOT "hello" IN "hello world"') self.assertIsInstance(result, NotExpression) self.assertEqual(result.value.left.value, 'hello') self.assertEqual(result.value.right.value, 'hello world')