#!/usr/bin/env python # # Unit tests for houseaccts.core # # TODO: Actually write some tests import unittest import datetime import difflib import houseaccts.core def assert_eq_lines(a, b): """ If a == b, do nothing; otherwise, raise an exception containing a context diff of a and b. """ if a == b: return diff = "\n".join(difflib.ndiff(a.split('\n'), b.split('\n'))) raise AssertionError("strings differ:\n%s" % diff) class TextStatementTestCase(unittest.TestCase): """Tests for the TextStatement class""" def testRealWorld(self): sample_stmt = { 'is_complete': True, 'user': 'nturner', 'user_full_name': 'Nathaniel W. Turner', 'start_date': datetime.datetime(2004, 9, 1), 'end_date': datetime.datetime(2004, 10, 1), 'end_date_incl': datetime.datetime(2004, 9, 30), 'period_txt': 'September 2004', 'start_bal': -297.87, 'tot_credit': 70.41, 'tot_debit': 23.47 + 30.00 + 22.29, 'end_bal': -292.51, 'transactions': [ { 'posted': datetime.datetime(2004, 9, 3), 'description': 'DSL for Sep', 'mysummary': {'credit': 70.41, 'debit': 23.47}, 'mysplits': [ {'user': 'nturner', 'debit_amt': -70.41}, {'user': 'nturner', 'debit_amt': 23.47}, ], 'running_bal': -344.80, }, { 'posted': datetime.datetime(2004, 9, 4), 'description': 'Beers & dinner', 'mysummary': {'credit': 0, 'debit': 30.00}, 'mysplits': [ {'user': 'nturner', 'debit_amt': 30.00}, ], 'running_bal': -314.80, }, { 'posted': datetime.datetime(2004, 9, 29), 'description': "Closing Tim's account [nwt]", 'mysummary': {'credit': 0, 'debit': 22.29}, 'mysplits': [ {'user': 'nturner', 'debit_amt': 22.29}, ], 'running_bal': -292.51, }, ] } sample_acct_type = "3 Howe St #3 Account" sample_output = u"""\ 3 Howe St #3 Account Statement for Nathaniel W. Turner Transactions for the month of September 2004 You have a credit of $292.51 as of 2004-09-30. Date Description Payment Expense Balance -------------------------------------------------------------------------- 2004-09-01 (Previous Balance) -297.87 2004-09-03 DSL for Sep 70.41 23.47 -344.80 2004-09-04 Beers & dinner 30.00 -314.80 2004-09-29 Closing Tim's account [nwt] 22.29 -292.51 -------------------------------------------------------------------------- The Expense column shows your part of a shared expense (or in the case of transfers, money paid to you). The Payment column shows payments you have made. The Balance column keeps a running balance; when the balance is positive, you owe money; when the balance is negative, you are owed money. """ self.ts = houseaccts.core.TextStatement(sample_stmt, acct_type=sample_acct_type) assert_eq_lines(unicode(self.ts), sample_output) def suite(): return unittest.makeSuite(TextStatementTestCase) if __name__ == '__main__': # When this module is executed from the command-line, run all its tests unittest.TextTestRunner().run(suite())