def test_total_two_entries(self): """Test the total method with one entries.""" group = Group("Test Group", [ Entry("Entry 1", 100), Entry("Entry 2", "23.5"), ]) self.assertEqual(group.total(), Money("123.5", "EUR"))
def test_total_complex_group_currencies(self): """ Tests the total function with some more complex (sub)-groups and multiple currencies. """ rates = Rates() rates.add_currency("USD", 2) rates.add_currency("CHF", 0.5) group = Group("Test Group", [ Entry("Entry 1", 100), Group("Sub Group", [ Entry("Sub Entry", 100), Group( "Sub-Sub Group", [ Entry("Sub-Sub Entry 1", 100, currency="CHF"), Entry("Sub-Sub Entry 2", 200, currency="USD"), ], currency="CHF", ) ]), Entry("Sub-Sub Entry 2", 100, currency="CHF"), ]) self.assertEqual(group.total(), Money(700, "EUR"))
def set_currency(cls, currency: str): """ Alter the currency for all following budget elements (defaults to EUR). Currencies are expressed in a three lettered code as stated in the ISO 4217 standard. """ Group._set_currency(currency) Entry._set_currency(currency) Rates._set_currency(currency)
def test_complex_sum(self): """Some more currencies.""" rates = Rates() rates.add_currency("USD", 2) rates.add_currency("CHF", 0.5) group = Group("Set Design", [ Entry("Expense 1", 100), Entry("Expense 2", 200, currency="USD"), Entry("Expense 3", 100, currency="CHF"), ]) self.assertEqual(group.total(), Money(400, "EUR"))
def test_failure_on_no_rates(self): """ Tests if a exception is thrown when multiple currencies are used by no Rates object was instantiated. """ group = Group("Test Group", [ Entry("Entry 1", 100), Entry("Entry 2", "23.5"), ]) self.assertEqual(group.total(), Money("123.5", "USD")) self.assertNotEqual(group.total(), Money("123.5", "EUR")) self.assertNotEqual(group.total(), Money(100, "USD"))
def test_simple_sum(self): """ Tests the total of a group consisting of two entries with different currencies. """ rates = Rates() rates.add_currency("USD", 2) group = Group("Set Design", [ Entry("Expense 1", 100), Entry("Expense 2", 200, currency="USD"), ]) self.assertEqual(group.total(), Money(200, "EUR"))
def test_total_two_entries_usd(self): """Test the total method while having one entry in USD.""" rates = Rates() rates.add_currency("USD", 2) group = Group("Test Group", [ Entry("Entry 1", 100), Entry("Entry 2", 200, currency="USD"), ]) rsl = group.total() self.assertEqual(rsl, Money(200, "EUR")) self.assertNotEqual(group.total(), Money(300, "EUR")) self.assertNotEqual(group.total(), Money(200, "USD")) self.assertNotEqual(group.total(), Money(300, "USD"))
def test_total_complex_group(self): """ Tests the total function with some more complex (sub)-groups. """ group = Group("Test Group", [ Entry("Entry 1", 100), Group("Sub Group", [ Entry("Sub Entry", 100), Group("Sub-Sub Group", [ Entry("Sub-Sub Entry 1", 100), Entry("Sub-Sub Entry 2", 300), ]) ]), Entry("Entry 2", 50), ]) self.assertEqual(group.total(), Money(650, "EUR"))
def _from_entry(cls, entry: Entry) -> "Group": """ Generates a Group from a single Entry. Used for rendering Groups containing sub-Groups and single items at the same time. """ code = entry.code entry.code = "" return cls( entry.name, [entry], code=code, comment="", currency=entry.currency, )
def test_total_with_different_base_currency(self): """Tests the total with a different base currency than EUR.""" Budget.set_currency("USD") group = Group("Test Group", [Entry("Entry 1", 100)]) self.assertEqual(group.total(), Money(100, "USD"))