def test_ctor(self): ra0 = RealAccount('Assets:US:Bank:Checking') self.assertEqual(0, len(ra0)) ra0 = RealAccount('Equity') ra0 = RealAccount('') with self.assertRaises(Exception): ra0 = RealAccount(None)
def test_compare_realizations(self): # Check that value comparison uses our balance comparison properly. map1 = {'Assets:US:Bank:Checking': inventory.Inventory()} map2 = {'Assets:US:Bank:Checking': inventory.Inventory()} map2['Assets:US:Bank:Checking'].add_amount(A('0.01 USD')) self.assertNotEqual(map1, map2) # Now check this with accounts. root1 = RealAccount('') ra1 = realization.get_or_create(root1, 'Assets:US:Bank:Checking') ra1.balance.add_amount(A('0.01 USD')) root2 = RealAccount('') ra2 = realization.get_or_create(root2, 'Assets:US:Bank:Checking') ra2.balance.add_amount(A('0.01 USD')) self.assertEqual(ra1, ra2) root3 = copy.deepcopy(root2) ra3 = realization.get(root3, 'Assets:US:Bank:Checking') ra3.account = 'Liabilities:US:CreditCard' self.assertNotEqual(root1, root3) root3 = copy.deepcopy(root2) ra3 = realization.get(root3, 'Assets:US:Bank:Checking') ra3.balance.add_amount(A('0.01 CAD')) self.assertNotEqual(root1, root3) root3 = copy.deepcopy(root2) ra3 = realization.get(root3, 'Assets:US:Bank:Checking') ra3.txn_postings.append('posting') self.assertNotEqual(root1, root3) root3 = copy.deepcopy(root2) ra3 = realization.get(root3, 'Assets:US:Bank:Checking') ra3['Sub'] = RealAccount('Assets:US:Bank:Checking:Sub') self.assertNotEqual(root1, root3)
def create_simple_account(): ra0 = RealAccount('') ra0['Assets'] = RealAccount('Assets') ra0['Assets']['US'] = RealAccount('Assets:US') ra0['Assets']['US']['Bank'] = RealAccount('Assets:US:Bank') ra0['Assets']['US']['Bank']['Checking'] = RealAccount('Assets:US:Bank:Checking') return ra0
def test_setitem_constraints(self): ra0 = RealAccount('') ra0['Assets'] = RealAccount('Assets') with self.assertRaises(KeyError): ra0['Assets'][42] = RealAccount('Assets:US') with self.assertRaises(ValueError): ra0['Assets']['US'] = 42 with self.assertRaises(ValueError): ra0['Assets']['US'] = RealAccount('Assets:US:Checking')
def test_get_or_create(self): ra0 = RealAccount('') ra0_checking = realization.get_or_create(ra0, 'Assets:US:Bank:Checking') realization.get_or_create(ra0, 'Assets:US:Bank:Savings') self.assertEqual('Assets:US:Bank:Checking', ra0_checking.account) self.assertEqual({'Assets'}, ra0.keys()) self.assertEqual({'Checking', 'Savings'}, ra0['Assets']['US']['Bank'].keys()) ra0_assets = ra0['Assets'] ra0_assets2 = realization.get_or_create(ra0, 'Assets') self.assertTrue(ra0_assets2 is ra0_assets)
def test_contains(self): ra0 = RealAccount('') realization.get_or_create(ra0, 'Assets:US:Bank:Checking') realization.get_or_create(ra0, 'Assets:US:Bank:Savings') self.assertTrue(realization.contains(ra0, 'Assets:US:Bank:Checking')) self.assertTrue(realization.contains(ra0, 'Assets:US:Bank:Savings')) self.assertFalse(realization.contains(ra0, 'Assets:US:Cash'))
def test_clone(self): ra0 = RealAccount('') ra0['Assets'] = RealAccount('Assets') ra0.balance = 42 ra0.txn_postings.append('posting1') ra0.txn_postings.append('posting2') ra0_clone = copy.copy(ra0) self.assertEqual(42, ra0_clone.balance) self.assertEqual(['posting1', 'posting2'], ra0_clone.txn_postings) self.assertEqual({'Assets'}, ra0_clone.keys()) ra0_clone = ra0.copy() self.assertEqual(42, ra0_clone.balance) self.assertEqual(['posting1', 'posting2'], ra0_clone.txn_postings) self.assertEqual({'Assets'}, ra0_clone.keys())
def test_iter_children(self): ra0 = RealAccount('') for account_name in ['Assets:US:Bank:Checking', 'Assets:US:Bank:Savings', 'Assets:US:Cash', 'Assets:CA:Cash']: realization.get_or_create(ra0, account_name) # Test enumerating all accounts. self.assertEqual(['', 'Assets', 'Assets:CA', 'Assets:CA:Cash', 'Assets:US', 'Assets:US:Bank', 'Assets:US:Bank:Checking', 'Assets:US:Bank:Savings', 'Assets:US:Cash'], [ra.account for ra in realization.iter_children(ra0)]) # Test enumerating leaves only. self.assertEqual(['Assets:CA:Cash', 'Assets:US:Bank:Checking', 'Assets:US:Bank:Savings', 'Assets:US:Cash'], [ra.account for ra in realization.iter_children(ra0, True)])
def test_str(self): ra0 = RealAccount('Assets:US:Bank:Checking') self.assertEqual('{}', str(ra0)) #self.assertEqual('RealAccount()', str(ra0)) ra0 = create_simple_account() ra0_str = str(ra0) self.assertRegex(ra0_str, 'Assets') self.assertRegex(ra0_str, 'Bank') self.assertRegex(ra0_str, 'Checking')
def test_str(self): ra0 = RealAccount('Assets:US:Bank:Checking') self.assertEqual('{}', str(ra0)) #self.assertEqual('RealAccount()', str(ra0)) ra0 = create_simple_account() ra0_str = str(ra0) self.assertTrue(re.search('Assets', ra0_str)) self.assertTrue(re.search('Bank', ra0_str)) self.assertTrue(re.search('Checking', ra0_str))
def test_equality(self): ra1 = RealAccount('Assets:US:Bank:Checking') ra1.balance.add_amount(A('100 USD')) ra1.txn_postings.extend(['a', 'b']) ra2 = RealAccount('Assets:US:Bank:Checking') ra2.balance.add_amount(A('100 USD')) ra2.txn_postings.extend(['a', 'b']) self.assertEqual(ra1, ra2) saved_balance = ra2.balance ra2.balance.add_amount(A('0.01 USD')) self.assertNotEqual(ra1, ra2) ra2.balance = saved_balance ra2.txn_postings.append('c') self.assertNotEqual(ra1, ra2) ra2.txn_postings.pop(-1) saved_account = ra2.account ra2.account += ':First' self.assertNotEqual(ra1, ra2) ra2.account = saved_account
def create_real(account_value_pairs): real_root = RealAccount('') for account_name, value in account_value_pairs: real_account = realization.get_or_create(real_root, account_name) real_account.balance += inventory.from_string(value) return real_root