class SortCategoryEntriesTestCase(unittest.TestCase): def setUp(self): self.listing = Listing() for c, v in zip("ab", [20, 10]): self.listing.add_entry(BaseEntry("foo", v, "01-01"), c) def test_sort_by_name(self): Listing.CATEGORY_ENTRY_SORT_KEY = "name" self.assertEqual( str(self.listing), '\n'.join([ "{1:^{0}}".format(CategoryEntry.TOTAL_LENGTH, "Listing"), "Name Value Date ID ", "A 20.00" + 10 * " ", " Foo 20.00 01-01 0", "B 10.00" + 10 * " ", " Foo 10.00 01-01 0", ])) def test_sort_by_value(self): Listing.CATEGORY_ENTRY_SORT_KEY = "value" self.assertEqual( str(self.listing), '\n'.join([ "{1:^{0}}".format(CategoryEntry.TOTAL_LENGTH, "Listing"), "Name Value Date ID ", "B 10.00" + 10 * " ", " Foo 10.00 01-01 0", "A 20.00" + 10 * " ", " Foo 20.00 01-01 0", ]))
class AddBaseEntryTestCase(unittest.TestCase): def setUp(self): self.listing = Listing() self.item_name = "Aldi" self.item_value = 66.6 self.item_date = "11-08" self.item_category = "Groceries" self.listing.add_entry( BaseEntry(self.item_name, self.item_value, self.item_date), self.item_category) def test_str(self): self.assertEqual( str(self.listing), '\n'.join([ "{1:^{0}}".format(CategoryEntry.TOTAL_LENGTH, "Listing"), "Name Value Date ID ", "Groceries 66.60" + 10 * " ", " Aldi 66.60 11-08 0" ])) def test_str_no_eid(self): BaseEntry.SHOW_EID = False self.assertEqual( str(self.listing), '\n'.join([ "{1:^{0}}".format(CategoryEntry.TOTAL_LENGTH, "Listing"), "Name Value Date ", # TODO: fix this; category entry line has to be shorter "Groceries 66.60 ", " Aldi 66.60 11-08" ])) BaseEntry.SHOW_EID = True def test_add_invalid_entry(self): self.assertRaises(TypeError, self.listing.add_entry, None)
class AddCategoryEntryTestCase(unittest.TestCase): def setUp(self): self.listing = Listing() self.category_name = "Groceries" self.listing.add_entry(CategoryEntry(name=self.category_name)) def test_category_item_in_list(self): self.assertIn(self.category_name.lower(), self.listing.category_entry_names)
class AddBaseEntryWithoutCategoryTestCase(unittest.TestCase): def setUp(self): self.listing = Listing() self.item_name = "Aldi" self.item_value = 66.6 self.item_date = "11-08" self.listing.add_entry( BaseEntry(self.item_name, self.item_value, self.item_date)) def test_default_category_in_list(self): names = list(self.listing.category_entry_names) self.assertIn(CategoryEntry.DEFAULT_NAME, names)
class AddTwoBaseEntriesTestCase(unittest.TestCase): def setUp(self): self.listing = Listing() self.item_a_value = 66.6 self.item_b_value = 10.01 self.item_category = "Groceries" self.date = "11-11" self.listing.add_entry(BaseEntry("Aldi", self.item_a_value, self.date), self.item_category) self.listing.add_entry(BaseEntry("Rewe", self.item_b_value, self.date), self.item_category) def test_total_value(self): self.assertAlmostEqual(self.item_a_value + self.item_b_value, self.listing.total_value(), places=5)
class AddNegativeBaseEntryTestCase(unittest.TestCase): def setUp(self): self.listing = Listing() self.item_name = "Aldi" self.item_value = -66.6 self.item_date = "11-08" self.item_category = "Groceries" self.listing.add_entry( BaseEntry(self.item_name, self.item_value, self.item_date), self.item_category) def test_str(self): self.assertEqual( str(self.listing), '\n'.join([ "{1:^{0}}".format(CategoryEntry.TOTAL_LENGTH, "Listing"), "Name Value Date ID ", "Groceries 66.60" + 10 * " ", " Aldi 66.60 11-08 0" ]))