def test_compile_EvalSum(self): with self.assertRaises(qc.CompilationError): qe.Sum([qc.EvalConstant('testing')]) c_sum = qe.Sum([qc.EvalConstant(17)]) self.assertEqual(int, c_sum.dtype) c_sum = qe.Sum([qc.EvalConstant(D('17.'))]) self.assertEqual(Decimal, c_sum.dtype)
def test_operaotors(self): expr = self.compile("SELECT 1 + 1 AS expr") self.assertEqual( expr, qc.EvalQuery([ qc.EvalTarget( qc.Operator(qp.Add, [qc.EvalConstant(1), qc.EvalConstant(1)]), 'expr', False) ], None, None, None, None, None, None, None)) expr = self.compile("SELECT 1 + meta('int') AS expr") self.assertEqual( expr, qc.EvalQuery([ qc.EvalTarget( qc.Operator(qp.Add, [ qc.EvalConstant(1), qe.Function('decimal', [ qe.Function('meta', [ qc.EvalConstant('int'), ]), ]), ]), 'expr', False) ], None, None, None, None, None, None, None))
def test_compile_EvalCost(self): with self.assertRaises(qc.CompilationError): qe.CostPosition([qc.EvalConstant(17)]) with self.assertRaises(qc.CompilationError): qe.CostPosition([qc.EvalConstant(inventory.Inventory())]) c_cost = qe.CostPosition([qc.EvalConstant( position.Position.from_string('100 USD'))]) self.assertEqual(amount.Amount, c_cost.dtype)
def test_compile_EvalConstant(self): c_int = qc.EvalConstant(17) self.assertEqual(int, c_int.dtype) c_decimal = qc.EvalConstant(D('7364.35')) self.assertEqual(Decimal, c_decimal.dtype) c_str = qc.EvalConstant("Assets:Checking") self.assertEqual(str, c_str.dtype)
def test_uses_balance_column(self): c_simple = qe.BalanceColumn() self.assertTrue(qx.uses_balance_column(c_simple)) c_simple_not = qe.AccountColumn() self.assertFalse(qx.uses_balance_column(c_simple_not)) c_subexpr = qc.EvalEqual(qe.BalanceColumn(), qc.EvalConstant(2012)) self.assertTrue(qx.uses_balance_column(c_subexpr)) c_subexpr_not = qc.EvalEqual(qe.AccountColumn(), qc.EvalConstant('Assets')) self.assertFalse(qx.uses_balance_column(c_subexpr_not))
def test_print_from(self): self.assertCompile( qc.EvalPrint( qc.EvalFrom( qc.Operator(qp.Equal, [ qe.YearEntryColumn(), qc.EvalConstant(2014), ]), None, None, None)), """PRINT FROM year = 2014;""")
def test_expr_binaryop(self): self.assertEqual( qc.EvalEqual(qe.DateColumn(), qc.EvalConstant(datetime.date(2014, 1, 1))), qc.compile_expression( qp.Equal(qp.Column('date'), qp.Constant(datetime.date(2014, 1, 1))), qe.TargetsEnvironment()))
def test_coalesce(self): expr = self.compile( "SELECT coalesce(narration, str(date), '~') AS expr") self.assertEqual( expr, qc.EvalQuery([ qc.EvalTarget( qc.EvalCoalesce([ qe.NarrationColumn(), qe.Function('str', [qe.DateColumn()]), qc.EvalConstant('~'), ]), 'expr', False) ], None, None, None, None, None, None, None)) with self.assertRaises(qc.CompilationError): self.compile("SELECT coalesce(narration, date, 1)")
def test_print_with_filter(self): statement = qc.EvalPrint( qc.EvalFrom( qc.EvalEqual(qe.YearEntryColumn(), qc.EvalConstant(2012)), None, None, None)) oss = io.StringIO() qx.execute_print(statement, self.entries, self.options_map, oss) self.assertEqualEntries( """ 2012-02-02 * "Dinner with Dos" Assets:Bank:Checking 102.00 USD Expenses:Restaurant -102.00 USD """, oss.getvalue())
def test_is_aggregate_derived(self): columns, aggregates = qc.get_columns_and_aggregates( qc.Operator(qp.And, [ qc.Operator(qp.Equal, [ qe.LineNoColumn(), qc.EvalConstant(42), ]), qc.Operator(qp.Or, [ qc.Operator(qp.Not, [ qc.Operator(qp.Equal, [ qe.DateColumn(), qc.EvalConstant(datetime.date(2014, 1, 1)), ]), ]), qc.EvalConstant(False), ]), ])) self.assertEqual((2, 0), (len(columns), len(aggregates))) columns, aggregates = qc.get_columns_and_aggregates( qc.Operator( qp.And, [ qc.Operator(qp.Equal, [ qe.LineNoColumn(), qc.EvalConstant(42), ]), qc.Operator( qp.Or, [ qc.Operator(qp.Not, [ qc.Operator(qp.Not, [ qc.Operator(qp.Equal, [ qe.DateColumn(), qc.EvalConstant( datetime.date(2014, 1, 1)), ]), ]), ]), # Aggregation node deep in the tree. qe.SumInt([qc.EvalConstant(1)]), ]), ])) self.assertEqual((2, 1), (len(columns), len(aggregates)))
def test_is_aggregate_derived(self): columns, aggregates = qc.get_columns_and_aggregates( qc.EvalAnd( qc.EvalEqual(qe.PositionColumn(), qc.EvalConstant(42)), qc.EvalOr( qc.EvalNot( qc.EvalEqual( qe.DateColumn(), qc.EvalConstant(datetime.date(2014, 1, 1)))), qc.EvalConstant(False)))) self.assertEqual((2, 0), (len(columns), len(aggregates))) columns, aggregates = qc.get_columns_and_aggregates( qc.EvalAnd( qc.EvalEqual(qe.PositionColumn(), qc.EvalConstant(42)), qc.EvalOr( qc.EvalNot( qc.EvalEqual( qe.DateColumn(), qc.EvalConstant(datetime.date(2014, 1, 1)))), # Aggregation node deep in the tree. qe.Sum([qc.EvalConstant(1)])))) self.assertEqual((2, 1), (len(columns), len(aggregates)))
def test_compile_EvalSub(self): c_plus = qc.EvalSub(qc.EvalConstant(17), qc.EvalConstant(18)) self.assertEqual(Decimal, c_plus.dtype)
def test_compile_EvalOr(self): c_or = qc.EvalOr(qc.EvalConstant(17), qc.EvalConstant(18)) self.assertEqual(bool, c_or.dtype)
def test_compile_EvalAnd(self): c_and = qc.EvalAnd(qc.EvalConstant(17), qc.EvalConstant(18)) self.assertEqual(bool, c_and.dtype)
def test_compile_EvalMatch(self): with self.assertRaises(qc.CompilationError): qc.EvalMatch(qc.EvalConstant('testing'), qc.EvalConstant(18)) c_equal = qc.EvalMatch(qc.EvalConstant('testing'), qc.EvalConstant('test.*')) self.assertEqual(bool, c_equal.dtype)
def test_compile_EvalLessEq(self): c_le = qc.EvalLessEq(qc.EvalConstant(17), qc.EvalConstant(18)) self.assertEqual(bool, c_le.dtype)
def test_compile_Div(self): c_plus = qc.Operator( qp.Div, [qc.EvalConstant(17), qc.EvalConstant(18)]) self.assertEqual(Decimal, c_plus.dtype)
def test_compile_Equal(self): c_equal = qc.Operator( qp.Equal, [qc.EvalConstant(17), qc.EvalConstant(18)]) self.assertEqual(bool, c_equal.dtype)
def test_compile_EvalNot(self): c_not = qc.EvalNot(qc.EvalConstant(17)) self.assertEqual(bool, c_not.dtype)
def test_compile_LessEq(self): c_le = qc.Operator( qp.LessEq, [qc.EvalConstant(17), qc.EvalConstant(18)]) self.assertEqual(bool, c_le.dtype)
def test_compile_GreaterEq(self): c_ge = qc.Operator( qp.GreaterEq, [qc.EvalConstant(17), qc.EvalConstant(18)]) self.assertEqual(bool, c_ge.dtype)
def test_compile_Match(self): c_match = qc.Operator(qp.Match, [qc.EvalConstant('abc'), qc.EvalConstant('b')]) self.assertEqual(bool, c_match.dtype)
def test_compile_And(self): c_and = qc.Operator(qp.And, [qc.EvalConstant(17), qc.EvalConstant(18)]) self.assertEqual(bool, c_and.dtype)
def test_compile_Sub(self): c_plus = qc.Operator( qp.Sub, [qc.EvalConstant(17), qc.EvalConstant(18)]) self.assertEqual(int, c_plus.dtype)
def test_compile_Not(self): c_not = qc.Operator(qp.Not, [qc.EvalConstant(17)]) self.assertEqual(bool, c_not.dtype)
def test_compile_EvalEqual(self): c_equal = qc.EvalEqual(qc.EvalConstant(17), qc.EvalConstant(18)) self.assertEqual(bool, c_equal.dtype)
def test_expr_constant(self): self.assertEqual( qc.EvalConstant(D(17)), qc.compile_expression(qp.Constant(D(17)), qe.TargetsEnvironment()))
def test_compile_EvalGreaterEq(self): c_ge = qc.EvalGreaterEq(qc.EvalConstant(17), qc.EvalConstant(18)) self.assertEqual(bool, c_ge.dtype)
def test_print_from(self): self.assertCompile( qc.EvalPrint( qc.EvalFrom( qc.EvalEqual(qe.YearEntryColumn(), qc.EvalConstant(2014)), None, None, None)), "PRINT FROM year = 2014;")
def test_compile_Or(self): c_or = qc.Operator(qp.Or, [qc.EvalConstant(17), qc.EvalConstant(18)]) self.assertEqual(bool, c_or.dtype)