Skip to content

Commit

Permalink
Add account tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcapriotti committed Sep 14, 2011
1 parent 3f27b7c commit fdd73be
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pledger/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self):
self.precision = 2

def parse_account(self, str):
return NamedAccount(str)
return self.accounts[str]

def parse_value(self, str):
return Value.parse(str)
Expand Down
15 changes: 15 additions & 0 deletions tests/test-account.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ def testAccountSub(self):
self.assertEqual(self.account, entry.account)
self.assertEqual(-amount, entry.amount)

def testRoot(self):
self.assertEqual(self.parser.accounts["Assets"], self.account.root())

def testShortenedName(self):
account = self.account["Joint:Savings:Yearly:Interest:Compound:Open"]
size = 30
name = account.shortened_name(size)

self.assertLessEqual(len(name), size)
self.assertEqual(7, len(filter(lambda x: x == ':', name)))

def testSubName(self):
account = self.account["Checking"]
self.assertEqual(u'Bank:Checking', self.account.parent.sub_name(account))
self.assertIsNone(self.account.sub_name(self.parser.accounts["Test"]))
21 changes: 19 additions & 2 deletions tests/test-rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import unittest
from decimal import Decimal
from pledger.account import Account
from pledger.filter import Filter
from pledger.parser import Parser
from pledger.rule import Rule, RuleCollection, Generator
Expand All @@ -39,14 +40,15 @@ def setUp(self):

self.rules = RuleCollection()

def testRuleOnLedger(self):
@Generator
def discount(entry):
amount = entry.amount * Decimal("0.1")
yield entry.account - amount
yield self.cash_account + amount
self.discount = discount

rule = Rule(Filter.has_account(self.books_account), discount)
def testRuleOnLedger(self):
rule = Rule(Filter.has_account(self.books_account), self.discount)
self.rules.add_rule(rule)

result = []
Expand All @@ -59,3 +61,18 @@ def discount(entry):
self.books_account - self.parser.parse_value("3.30 EUR")]

self.assertItemsEqual(expected, result)

def testAccountTagRule(self):
self.books_account.tags["discount"] = self.discount
self.rules.add_rule(Account.tag_rule("discount"))

result = []
result += self.rules.apply(self.tr, self.tr.entries[0])
result += self.rules.apply(self.tr, self.tr.entries[1])

expected = [self.bank_account - self.parser.parse_value("33.00 EUR"),
self.books_account + self.parser.parse_value("33.00 EUR"),
self.cash_account + self.parser.parse_value("3.30 EUR"),
self.books_account - self.parser.parse_value("3.30 EUR")]

self.assertItemsEqual(expected, result)

0 comments on commit fdd73be

Please sign in to comment.