Skip to content
Browse files

Add account tests.

  • Loading branch information...
1 parent 3f27b7c commit fdd73befecb4486987dfe64d7e46492700dfc789 @pcapriotti committed Sep 14, 2011
Showing with 35 additions and 3 deletions.
  1. +1 −1 pledger/parser.py
  2. +15 −0 tests/test-account.py
  3. +19 −2 tests/test-rules.py
View
2 pledger/parser.py
@@ -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)
View
15 tests/test-account.py
@@ -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"]))
View
21 tests/test-rules.py
@@ -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
@@ -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 = []
@@ -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.
Something went wrong with that request. Please try again.