Add unit tests for XML reading

This commit is contained in:
Jonny007-MKD 2023-03-20 00:52:52 +01:00
parent 016f93ce65
commit 685bc8ad19
22 changed files with 313 additions and 0 deletions

150
test/test.py Normal file
View file

@ -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()

16
test/test1.xhb Normal file
View file

@ -0,0 +1,16 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<account key="2" pos="2" type="2" curr="1" name="MyCashAccount" initial="0" minimum="0" maximum="0" rdate="738460"/>
<account key="3" pos="3" type="3" curr="1" name="MyAssetAccount" initial="0" minimum="0" maximum="0"/>
<account key="4" pos="4" type="4" curr="1" name="MyCreditCardAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<pay key="2" name="Payee2"/>
<cat key="1" name="Category1"/>
<cat key="2" parent="1" flags="1" name="Subcategory1"/>
<cat key="3" name="Category2"/>
<ope date="738460" amount="-0.01" account="2" paymode="3" payee="1" category="2" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
<ope date="738460" amount="-0.02" account="4" paymode="6" st="2" payee="2" category="3" wording="MyMemo2"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account pos="2" type="2" curr="1" name="MyCashAccount" initial="0" minimum="0" maximum="0" rdate="738460"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="bla" pos="2" type="2" curr="1" name="MyCashAccount" initial="0" minimum="0" maximum="0" rdate="738460"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="3" pos="2" type="2" curr="1" initial="0" minimum="0" maximum="0" rdate="738460"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<cat name="Category1"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<cat key="bla" name="Category1"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<cat key="1"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<cat key="1" name="Category1" parent="bla"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<cat key="1" name="Category1" parent="123"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="738460" amount="-0.01" paymode="3" payee="1" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="738460" amount="-0.01" account="bla" paymode="3" payee="1" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="738460" account="1" paymode="3" payee="1" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="738460" amount="bla" account="1" paymode="3" payee="1" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="738460" amount="-0.01" account="1" paymode="3" payee="1" category="bla" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope amount="-0.01" account="1" paymode="3" payee="1" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="bla" amount="-0.01" account="1" paymode="3" payee="1" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="738460" amount="-0.01" account="1" dst_account="bla" paymode="3" payee="1" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<account key="1" pos="1" type="1" curr="1" name="MyBankAccount" initial="0" minimum="0" maximum="0"/>
<pay key="1" name="Payee1"/>
<cat key="1" name="Category1"/>
<ope date="738460" amount="-0.01" account="1" paymode="3" payee="bla" category="1" wording="MyMemo" info="info" tags="Tag1 Tag2"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<pay name="bla"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<pay key="bla" name="bla"/>
</homebank>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<homebank v="1.3999999999999999" d="050502">
<properties title="MyTitle" curr="1" auto_smode="1" auto_weekday="1"/>
<cur key="1" flags="0" iso="EUR" name="Euro" symb="€" syprf="0" dchar="," gchar="." frac="2" rate="0" mdate="0"/>
<pay key="1"/>
</homebank>