homebank/test/test.py

151 lines
6.1 KiB
Python

import unittest
from datetime import date
from lxml import etree
import homebank
class TestReadXml_InvalidInput(unittest.TestCase):
def test_invalid_path(self):
with self.assertRaises(TypeError):
homebank.Homebank(None)
def test_nonexistent_path(self):
with self.assertRaises(OSError):
homebank.Homebank("nonexistent.xhb")
def test_not_an_xml(self):
with self.assertRaises(etree.XMLSyntaxError):
homebank.Homebank("test/test.py")
class TestReadXml_Test1(unittest.TestCase):
def test_payees(self):
hb = homebank.Homebank("test/test1.xhb")
self.assertEqual(hb.payees, {
1: homebank.Payee("Payee1"),
2: homebank.Payee("Payee2"),
})
def test_accounts(self):
hb = homebank.Homebank("test/test1.xhb")
self.assertEqual(hb.accounts, {
1: homebank.Account("MyBankAccount"),
2: homebank.Account("MyCashAccount"),
3: homebank.Account("MyAssetAccount"),
4: homebank.Account("MyCreditCardAccount"),
})
def test_categories(self):
hb = homebank.Homebank("test/test1.xhb")
cat1 = homebank.Category("Category1")
cat2 = homebank.Category("Subcategory1", cat1)
cat3 = homebank.Category("Category2")
self.assertEqual(hb.categories, {
1: cat1,
2: cat2,
3: cat3,
})
def test_operations(self):
hb = homebank.Homebank("test/test1.xhb")
self.assertCountEqual(hb.operations, [
homebank.Operation(date(2022,11,1), -0.01, hb.accounts[2], None, hb.payees[1], hb.categories[2], "MyMemo"),
homebank.Operation(date(2022,11,1), -0.02, hb.accounts[4], None, hb.payees[2], hb.categories[3], "MyMemo2"),
])
class TestReadXml_Payees(unittest.TestCase):
def test_missing_key(self):
with self.assertRaisesRegex(KeyError, "Node 'pay'.*is missing attribute 'key'"):
homebank.Homebank("test/test_error_payee_key.xhb")
def test_missing_name(self):
with self.assertRaisesRegex(KeyError, "Node 'pay'.*is missing attribute 'name'"):
homebank.Homebank("test/test_error_payee_name.xhb")
def test_invalid_key(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'key' of node 'pay'.*to integer"):
homebank.Homebank("test/test_error_payee_key_invalid.xhb")
class TestReadXml_Accounts(unittest.TestCase):
def test_missing_key(self):
with self.assertRaisesRegex(KeyError, "Node 'account'.*is missing attribute 'key'"):
homebank.Homebank("test/test_error_account_key.xhb")
def test_missing_name(self):
with self.assertRaisesRegex(KeyError, "Node 'account'.*is missing attribute 'name'"):
homebank.Homebank("test/test_error_account_name.xhb")
def test_invalid_key(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'key' of node 'account'.*to integer"):
homebank.Homebank("test/test_error_account_key_invalid.xhb")
class TestReadXml_Categories(unittest.TestCase):
def test_missing_key(self):
with self.assertRaisesRegex(KeyError, "Node 'cat'.*is missing attribute 'key'"):
homebank.Homebank("test/test_error_category_key.xhb")
def test_missing_name(self):
with self.assertRaisesRegex(KeyError, "Node 'cat'.*is missing attribute 'name'"):
homebank.Homebank("test/test_error_category_name.xhb")
def test_invalid_key(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'key' of node 'cat'.*to integer"):
homebank.Homebank("test/test_error_category_key_invalid.xhb")
def test_invalid_parent(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'parent' of node 'cat'.*to integer"):
homebank.Homebank("test/test_error_category_parent_invalid.xhb")
def test_unknown_parent(self):
with self.assertRaisesRegex(KeyError, "Parent category 123 for category 'Category1' not found"):
homebank.Homebank("test/test_error_category_parent_unknown.xhb")
class TestReadXml_Operations(unittest.TestCase):
def test_missing_date(self):
with self.assertRaisesRegex(KeyError, "Node 'ope'.*is missing attribute 'date'"):
homebank.Homebank("test/test_error_operation_date.xhb")
def test_invalid_date(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'date' of node 'ope'.*to integer"):
homebank.Homebank("test/test_error_operation_date_invalid.xhb")
def test_missing_amount(self):
with self.assertRaisesRegex(KeyError, "Node 'ope'.*is missing attribute 'amount'"):
homebank.Homebank("test/test_error_operation_amount.xhb")
def test_invalid_amount(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'amount' of node 'ope'.*to float"):
homebank.Homebank("test/test_error_operation_amount_invalid.xhb")
def test_missing_account(self):
with self.assertRaisesRegex(KeyError, "Node 'ope'.*is missing attribute 'account'"):
homebank.Homebank("test/test_error_operation_account.xhb")
def test_invalid_account(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'account' of node 'ope'.*to integer"):
homebank.Homebank("test/test_error_operation_account_invalid.xhb")
def test_invalid_dst_account(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'dst_account' of node 'ope'.*to integer"):
homebank.Homebank("test/test_error_operation_dst_account_invalid.xhb")
def test_invalid_payee(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'payee' of node 'ope'.*to integer"):
homebank.Homebank("test/test_error_operation_payee_invalid.xhb")
def test_invalid_category(self):
with self.assertRaisesRegex(ValueError, "Could not convert attribute 'category' of node 'ope'.*to integer"):
homebank.Homebank("test/test_error_operation_category_invalid.xhb")
if __name__ == '__main__':
unittest.main()