def test_operators_precedence(self): self.assertParseTarget( "SELECT a AND b OR c AND d;", qp.Or(qp.And(qp.Column('a'), qp.Column('b')), qp.And(qp.Column('c'), qp.Column('d')))) self.assertParseTarget( "SELECT a = 2 AND b != 3;", qp.And(qp.Equal(qp.Column('a'), qp.Constant(2)), qp.Not(qp.Equal(qp.Column('b'), qp.Constant(3))))) self.assertParseTarget("SELECT not a AND b;", qp.And(qp.Not(qp.Column('a')), qp.Column('b'))) self.assertParseTarget( "SELECT a + b AND c - d;", qp.And(qp.Add(qp.Column('a'), qp.Column('b')), qp.Sub(qp.Column('c'), qp.Column('d')))) self.assertParseTarget( "SELECT a * b + c / d - 3;", qp.Sub( qp.Add(qp.Mul(qp.Column(name='a'), qp.Column(name='b')), qp.Div(qp.Column(name='c'), qp.Column(name='d'))), qp.Constant(value=3))) self.assertParseTarget( "SELECT 'orange' IN tags AND 'bananas' IN tags;", qp.And(qp.Contains(qp.Constant('orange'), qp.Column('tags')), qp.Contains(qp.Constant('bananas'), qp.Column('tags'))))
def test_complex_expressions(self): self.assertParseTarget( "SELECT a != (b != (42 AND 17));", qp.Not( qp.Equal( qp.Column('a'), qp.Not( qp.Equal(qp.Column('b'), qp.And(qp.Constant(42), qp.Constant(17)))))))
def test_expr_paren_multi2(self): self.assertParse( qSelect([ qp.Target( qp.Not( qp.Equal( qp.Column('a'), qp.Not( qp.Equal( qp.Column('b'), qp.And(qp.Constant(42), qp.Constant(17)))))), None) ]), "SELECT a != (b != (42 AND 17));")
def test_expr_function__and_eq(self): self.assertParse( qSelect(qp.Wildcard(), where_clause=qp.And( qp.Equal(qp.Column('a'), qp.Constant(2)), qp.Not(qp.Equal(qp.Column('b'), qp.Constant(3))))), "SELECT * WHERE a = 2 AND b != 3;")
def test_unary(self): name = qp.get_expression_name(qp.Not(qp.Column('account'))) self.assertEqual(name, 'not(account)')
def test_expressions(self): # comparison operators self.assertParseTarget("SELECT a = 42;", qp.Equal(qp.Column('a'), qp.Constant(42))) self.assertParseTarget( "SELECT a != 42;", qp.Not(qp.Equal(qp.Column('a'), qp.Constant(42)))) self.assertParseTarget("SELECT a > 42;", qp.Greater(qp.Column('a'), qp.Constant(42))) self.assertParseTarget("SELECT a >= 42;", qp.GreaterEq(qp.Column('a'), qp.Constant(42))) self.assertParseTarget("SELECT a < 42;", qp.Less(qp.Column('a'), qp.Constant(42))) self.assertParseTarget("SELECT a <= 42;", qp.LessEq(qp.Column('a'), qp.Constant(42))) self.assertParseTarget("SELECT a ~ 'abc';", qp.Match(qp.Column('a'), qp.Constant('abc'))) self.assertParseTarget( "SELECT a != 42;", qp.Not(qp.Equal(qp.Column('a'), qp.Constant(42)))) self.assertParseTarget("SELECT not a;", qp.Not(qp.Column('a'))) self.assertParseTarget("SELECT a IS NULL;", qp.IsNull(qp.Column('a'))) self.assertParseTarget("SELECT a IS NOT NULL;", qp.IsNotNull(qp.Column('a'))) # bool expressions self.assertParseTarget("SELECT a AND b;", qp.And(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT a OR b;", qp.Or(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT NOT a;", qp.Not(qp.Column('a'))) # math expressions with identifiers self.assertParseTarget("SELECT a * b;", qp.Mul(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT a / b;", qp.Div(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT a + b;", qp.Add(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT a+b;", qp.Add(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT a - b;", qp.Sub(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT a-b;", qp.Sub(qp.Column('a'), qp.Column('b'))) self.assertParseTarget("SELECT +a;", qp.Column('a')) self.assertParseTarget("SELECT -a;", qp.Neg(qp.Column('a'))) # math expressions with numerals self.assertParseTarget("SELECT 2 * 3;", qp.Mul(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT 2 / 3;", qp.Div(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT 2+(3);", qp.Add(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT (2)-3;", qp.Sub(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT 2 + 3;", qp.Add(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT 2+3;", qp.Add(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT 2 - 3;", qp.Sub(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT 2-3;", qp.Sub(qp.Constant(2), qp.Constant(3))) self.assertParseTarget("SELECT +2;", qp.Constant(2)) self.assertParseTarget("SELECT -2;", qp.Constant(-2)) # silly, fails at compile time self.assertParseTarget("SELECT -'abc';", qp.Neg(qp.Constant('abc'))) # functions self.assertParseTarget("SELECT random();", qp.Function('random', [])) self.assertParseTarget("SELECT min(a);", qp.Function('min', [qp.Column('a')])) self.assertParseTarget( "SELECT min(a, b);", qp.Function('min', [qp.Column('a'), qp.Column('b')]))
def test_unary(self): self.assertEqual('not_account', qp.get_expression_name(qp.Not(qp.Column('account'))))
def test_expr_function__and_not(self): self.assertParse( qSelect(qp.Wildcard(), where_clause=qp.And(qp.Not(qp.Column('a')), qp.Column('b'))), "SELECT * WHERE not a AND b;")
def test_expr_not(self): self.assertParse(qSelect([qp.Target(qp.Not(qp.Column('a')), None)]), "SELECT NOT a;")
def test_expr_paren_multi(self): self.assertParse( qSelect([ qp.Target(qp.Not(qp.Equal(qp.Column('a'), qp.Constant(42))), None) ]), "SELECT not (a = 42);")
def test_expr_ne(self): self.assertParse( qSelect([ qp.Target(qp.Not(qp.Equal(qp.Column('a'), qp.Constant(42))), None) ]), "SELECT a != 42;")
def test_expr_unaryop(self): self.assertEqual( qc.EvalNot(qe.AccountColumn()), qc.compile_expression(qp.Not(qp.Column('account')), qe.TargetsEnvironment()))