def test_implicit_or(self): postfix = 'x y' result = expression.build_expression(postfix) query = Or((RegexQuery.from_string('y'), RegexQuery.from_string('x'))) assert result == query
def test_precedence(self): postfix = ['y', 'not', 'x', '@', 'and'] result = expression.build_expr_from_postfix(postfix) query = And((PayeeQuery(RegexQuery.from_string('x')), Not(RegexQuery.from_string('y')))) assert result == query
def test_binary(self): postfix = ['x', 'y', 'or'] result = expression.build_expr_from_postfix(postfix) query = Or((RegexQuery.from_string('y'), RegexQuery.from_string('x'))) assert result == query postfix = ['x', 'y', 'and'] result = expression.build_expr_from_postfix(postfix) query = And((RegexQuery.from_string('y'), RegexQuery.from_string('x'))) assert result == query
def test_binary(self): postfix = 'x or y' result = expression.build_expression(postfix) query = Or((RegexQuery.from_string('y'), RegexQuery.from_string('x'))) assert result == query postfix = 'x and y' result = expression.build_expression(postfix) query = And((RegexQuery.from_string('y'), RegexQuery.from_string('x'))) assert result == query
def build_expr_from_postfix(postfix_expr): operand_stack = deque() for token in postfix_expr: if token == 'and': op1, op2 = operand_stack.pop(), operand_stack.pop() operand_stack.append(And((op1, op2))) elif token == 'or': op1, op2 = operand_stack.pop(), operand_stack.pop() operand_stack.append(Or((op1, op2))) elif token == 'not': operand = operand_stack.pop() operand_stack.append(Not(operand)) elif token in ('payee', '@'): operand = operand_stack.pop() if not isinstance(operand, RegexQuery): raise ValueError operand_stack.append(PayeeQuery(operand)) else: operand = RegexQuery.from_string(token) operand_stack.append(operand) if len(operand_stack) != 1: raise ValueError return operand_stack[0]
def test_query_filtering(): agg = AccountAggregate(query=Not(RegexQuery(re.compile('Asset')))) trans = Transaction(date=arrow.get(datetime(2013, 2, 20)), description='Purchased reddit gold for the year') trans.add_posting( Posting(account=('Asset', 'Bitcoin Wallet'), amounts={BTC: -10})) trans.add_posting( Posting(account=('Expense', 'Web Services', 'Reddit'), amounts=None)) agg.add_transaction(trans) trans = Transaction(date=arrow.get(datetime(2013, 2, 20)), description='2012/7/1 Partial payment from Client X') trans.add_posting(Posting(account=('Bank', 'Paypal'), amounts={USD: 350})) trans.add_posting( Posting(account=('Expense', 'Web Services', 'Reddit'), amounts=None)) agg.add_transaction(trans) assert agg.aggregates == {USD: 0, BTC: Fraction(10)}
def test_single(self): postfix = ['x'] result = expression.build_expr_from_postfix(postfix) query = RegexQuery.from_string('x') assert result == query
def test_unary(self): postfix = ['x', 'not'] result = expression.build_expr_from_postfix(postfix) query = Not(RegexQuery.from_string('x')) assert result == query
def test_payee_at(self): postfix = ['x', '@'] result = expression.build_expr_from_postfix(postfix) query = PayeeQuery(RegexQuery.from_string('x')) assert result == query