def test_name_save(self): category = models.Category() category.name = ''.join( random.choices(string.ascii_letters + string.digits, k=20)) category.full_clean() category.save() category_from_db = models.Category.objects.get(pk=category.pk) self.assertEqual(category.name, category_from_db.name)
def _create_sheet_entry(sheet) -> models.SheetEntry: category = models.Category(name=_get_random_name()) category.save() entry = models.SheetEntry() entry.sheet = sheet entry.value = Decimal(random.uniform(-999.99, 999.99)).quantize(Decimal(".01")) entry.category = category entry.save() return entry
def test_category_cascade(self): category = models.Category(name="Test") category.save() entry = models.SheetEntry(sheet=self.sheet, category=category, value=0) entry.save() category.delete() actual_count = models.SheetEntry.objects.filter(pk=entry.pk).count() self.assertEqual(0, actual_count)
def test_category_prevent_deletion(self): category = models.Category(name="Expected category") category.save() transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = category transaction.account = self.account transaction.full_clean() transaction.save() with self.assertRaises(ProtectedError): category.delete()
def test_category(self): expected_category = models.Category(name="Expected category") expected_category.save() transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = expected_category transaction.account = self.account transaction.full_clean() transaction.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) self.assertEqual(expected_category, transaction_in_db.category)
def test_locked_no_change_to_category(self): entry = models.SheetEntry(sheet=self.sheet, category=self.category, value=Decimal(0)) entry.locked = True entry.full_clean() entry.save() entry_in_db = models.SheetEntry.objects.get(pk=entry.pk) new_category = models.Category(name=_get_random_name()) new_category.save() entry_in_db.category = new_category with self.assertRaises(ValidationError): entry_in_db.full_clean()
def test_locked_no_change_to_category(self): transaction = models.Transaction() transaction.locked = True transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = self.account transaction.full_clean() transaction.save() category = models.Category(name=_get_random_name()) category.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) transaction_in_db.category = category with self.assertRaises(ValidationError): transaction_in_db.full_clean()
def _create_transaction(month, year, account=None, locked=False) -> models.Transaction: category = models.Category(name=_get_random_name()) category.save() if account is None: account = models.Account(name=_get_random_name(), balance=Decimal(0)) account.save() transaction = models.Transaction() transaction.category = category transaction.account = account transaction.value = Decimal(random.uniform(-999.99, 999.99)).quantize( Decimal(".01")) transaction.partner = "Test partner" transaction.locked = locked transaction.date = _random_day_in_month(month, year) transaction.save() return transaction
def setUp(self): self.category = models.Category(name="Test") self.category.save() self.account = models.Account(name="Test", balance=Decimal(0)) self.account.save()
def test_str(self): expected_name = _get_random_name() category = models.Category(name=expected_name) self.assertEqual(expected_name, category.__str__())
def test_name_max_length_not_ok(self): category = models.Category() category.name = ''.join( random.choices(string.ascii_letters + string.digits, k=201)) with self.assertRaises(ValidationError): category.full_clean()
def setUp(self): self.sheet = models.Sheet(month=1, year=1) self.sheet.save() self.category = models.Category(name=_get_random_name()) self.category.save()
def _create_category() -> models.Category: category = models.Category(name=_get_random_name()) category.save() return category