From 685bc8ad198e3892a3269db2e80a3f48a645a8fd Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Mon, 20 Mar 2023 00:52:52 +0100 Subject: [PATCH] Add unit tests for XML reading --- test/test.py | 150 ++++++++++++++++++ test/test1.xhb | 16 ++ test/test_error_account_key.xhb | 6 + test/test_error_account_key_invalid.xhb | 6 + test/test_error_account_name.xhb | 6 + test/test_error_category_key.xhb | 6 + test/test_error_category_key_invalid.xhb | 6 + test/test_error_category_name.xhb | 6 + test/test_error_category_parent_invalid.xhb | 6 + test/test_error_category_parent_unknown.xhb | 6 + test/test_error_operation_account.xhb | 9 ++ test/test_error_operation_account_invalid.xhb | 9 ++ test/test_error_operation_amount.xhb | 9 ++ test/test_error_operation_amount_invalid.xhb | 9 ++ .../test_error_operation_category_invalid.xhb | 9 ++ test/test_error_operation_date.xhb | 9 ++ test/test_error_operation_date_invalid.xhb | 9 ++ ...st_error_operation_dst_account_invalid.xhb | 9 ++ test/test_error_operation_payee_invalid.xhb | 9 ++ test/test_error_payee_key.xhb | 6 + test/test_error_payee_key_invalid.xhb | 6 + test/test_error_payee_name.xhb | 6 + 22 files changed, 313 insertions(+) create mode 100644 test/test.py create mode 100644 test/test1.xhb create mode 100644 test/test_error_account_key.xhb create mode 100644 test/test_error_account_key_invalid.xhb create mode 100644 test/test_error_account_name.xhb create mode 100644 test/test_error_category_key.xhb create mode 100644 test/test_error_category_key_invalid.xhb create mode 100644 test/test_error_category_name.xhb create mode 100644 test/test_error_category_parent_invalid.xhb create mode 100644 test/test_error_category_parent_unknown.xhb create mode 100644 test/test_error_operation_account.xhb create mode 100644 test/test_error_operation_account_invalid.xhb create mode 100644 test/test_error_operation_amount.xhb create mode 100644 test/test_error_operation_amount_invalid.xhb create mode 100644 test/test_error_operation_category_invalid.xhb create mode 100644 test/test_error_operation_date.xhb create mode 100644 test/test_error_operation_date_invalid.xhb create mode 100644 test/test_error_operation_dst_account_invalid.xhb create mode 100644 test/test_error_operation_payee_invalid.xhb create mode 100644 test/test_error_payee_key.xhb create mode 100644 test/test_error_payee_key_invalid.xhb create mode 100644 test/test_error_payee_name.xhb diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..23c5605 --- /dev/null +++ b/test/test.py @@ -0,0 +1,150 @@ +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() + diff --git a/test/test1.xhb b/test/test1.xhb new file mode 100644 index 0000000..989c064 --- /dev/null +++ b/test/test1.xhb @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/test/test_error_account_key.xhb b/test/test_error_account_key.xhb new file mode 100644 index 0000000..549b386 --- /dev/null +++ b/test/test_error_account_key.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_account_key_invalid.xhb b/test/test_error_account_key_invalid.xhb new file mode 100644 index 0000000..bf811a5 --- /dev/null +++ b/test/test_error_account_key_invalid.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_account_name.xhb b/test/test_error_account_name.xhb new file mode 100644 index 0000000..a3f96cb --- /dev/null +++ b/test/test_error_account_name.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_category_key.xhb b/test/test_error_category_key.xhb new file mode 100644 index 0000000..eec334e --- /dev/null +++ b/test/test_error_category_key.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_category_key_invalid.xhb b/test/test_error_category_key_invalid.xhb new file mode 100644 index 0000000..38638b9 --- /dev/null +++ b/test/test_error_category_key_invalid.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_category_name.xhb b/test/test_error_category_name.xhb new file mode 100644 index 0000000..7c247c1 --- /dev/null +++ b/test/test_error_category_name.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_category_parent_invalid.xhb b/test/test_error_category_parent_invalid.xhb new file mode 100644 index 0000000..c89bb94 --- /dev/null +++ b/test/test_error_category_parent_invalid.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_category_parent_unknown.xhb b/test/test_error_category_parent_unknown.xhb new file mode 100644 index 0000000..aa821a5 --- /dev/null +++ b/test/test_error_category_parent_unknown.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_operation_account.xhb b/test/test_error_operation_account.xhb new file mode 100644 index 0000000..94c5d4d --- /dev/null +++ b/test/test_error_operation_account.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_account_invalid.xhb b/test/test_error_operation_account_invalid.xhb new file mode 100644 index 0000000..9755ebb --- /dev/null +++ b/test/test_error_operation_account_invalid.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_amount.xhb b/test/test_error_operation_amount.xhb new file mode 100644 index 0000000..fddbfbd --- /dev/null +++ b/test/test_error_operation_amount.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_amount_invalid.xhb b/test/test_error_operation_amount_invalid.xhb new file mode 100644 index 0000000..f59f079 --- /dev/null +++ b/test/test_error_operation_amount_invalid.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_category_invalid.xhb b/test/test_error_operation_category_invalid.xhb new file mode 100644 index 0000000..4dde471 --- /dev/null +++ b/test/test_error_operation_category_invalid.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_date.xhb b/test/test_error_operation_date.xhb new file mode 100644 index 0000000..2dd1caf --- /dev/null +++ b/test/test_error_operation_date.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_date_invalid.xhb b/test/test_error_operation_date_invalid.xhb new file mode 100644 index 0000000..cdf93fe --- /dev/null +++ b/test/test_error_operation_date_invalid.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_dst_account_invalid.xhb b/test/test_error_operation_dst_account_invalid.xhb new file mode 100644 index 0000000..68bcf51 --- /dev/null +++ b/test/test_error_operation_dst_account_invalid.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_operation_payee_invalid.xhb b/test/test_error_operation_payee_invalid.xhb new file mode 100644 index 0000000..3682199 --- /dev/null +++ b/test/test_error_operation_payee_invalid.xhb @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/test_error_payee_key.xhb b/test/test_error_payee_key.xhb new file mode 100644 index 0000000..5928b83 --- /dev/null +++ b/test/test_error_payee_key.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_payee_key_invalid.xhb b/test/test_error_payee_key_invalid.xhb new file mode 100644 index 0000000..c4dbdd8 --- /dev/null +++ b/test/test_error_payee_key_invalid.xhb @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/test_error_payee_name.xhb b/test/test_error_payee_name.xhb new file mode 100644 index 0000000..7498a57 --- /dev/null +++ b/test/test_error_payee_name.xhb @@ -0,0 +1,6 @@ + + + + + +