def test_expr_match(self):
     self.assertParse(
         qSelect([
             qp.Target(
                 qp.Match(qp.Column('a'),
                          qp.Constant('Assets:.*:Checking')), None)
         ]), "SELECT a ~ 'Assets:.*:Checking';")
Exemple #2
0
 def test_journal_with_account_func_and_from(self):
     journal = self.parse("JOURNAL 'liabilities' AT cost FROM year = 2014;")
     select = qc.transform_journal(journal)
     self.assertEqual(
         select,
         qp.Select([
             qp.Target(qp.Column('date'), None),
             qp.Target(qp.Column('flag'), None),
             qp.Target(
                 qp.Function('maxwidth',
                             [qp.Column('payee'),
                              qp.Constant(48)]), None),
             qp.Target(
                 qp.Function('maxwidth',
                             [qp.Column('narration'),
                              qp.Constant(80)]), None),
             qp.Target(qp.Column('account'), None),
             qp.Target(qp.Function('cost', [qp.Column('position')]), None),
             qp.Target(qp.Function('cost', [qp.Column('balance')]), None),
         ],
                   qp.From(qp.Equal(qp.Column('year'), qp.Constant(2014)),
                           None, None, None),
                   qp.Match(qp.Column('account'),
                            qp.Constant('liabilities')), None, None, None,
                   None, None))
 def test_explain_select(self):
     self.assertParse(
         qp.Explain(
             qSelect([
                 qp.Target(qp.Column('date'), None),
                 qp.Target(qp.Column('account'), None)
             ],
                     where_clause=qp.Match(qp.Column('account'),
                                           qp.Constant('etrade')))),
         "EXPLAIN SELECT date, account WHERE account ~ 'etrade';")
Exemple #4
0
 def test_journal_with_account(self):
     journal = self.parse("JOURNAL 'liabilities';")
     select = qc.transform_journal(journal)
     self.assertEqual(
         qp.Select([
             qp.Target(qp.Column('date'), None),
             qp.Target(qp.Column('flag'), None),
             qp.Target(
                 qp.Function('maxwidth',
                             [qp.Column('payee'),
                              qp.Constant(48)]), None),
             qp.Target(
                 qp.Function('maxwidth',
                             [qp.Column('narration'),
                              qp.Constant(80)]), None),
             qp.Target(qp.Column('account'), None),
             qp.Target(qp.Column('position'), None),
             qp.Target(qp.Column('balance'), None),
         ], None, qp.Match(qp.Column('account'),
                           qp.Constant('liabilities')), None, None, None,
                   None, None, None), select)
Exemple #5
0
    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')]))