def test_account_must_be_net(self): transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = None with self.assertRaises(ValidationError): transaction.full_clean()
def test_str(self, date, expected_date): partner = _get_random_name() transaction = models.Transaction(partner=partner, date=date, value=Decimal(0), category=self.category, account=self.account) expected_name = ( f"[{expected_date}] {str(transaction.account)} -> " f"{str(transaction.partner)} ({str(transaction.category)})") self.assertEqual(expected_name, transaction.__str__())
def test_value_decimal_places(self): expected_value = Decimal('123456789.123') transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = expected_value transaction.category = self.category transaction.account = self.account with self.assertRaises(ValidationError): transaction.full_clean()
def test_locked_default_false(self): transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = self.account transaction.full_clean() transaction.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) self.assertFalse(transaction_in_db.locked)
def test_partner_max_length(self): expected_name = ''.join( random.choices(string.ascii_letters + string.digits, k=201)) transaction = models.Transaction() transaction.partner = expected_name transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = self.account with self.assertRaises(ValidationError): transaction.full_clean()
def test_account_prevent_deletion(self): account = models.Account(name="Expected account", balance=Decimal(0)) account.save() transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = account transaction.full_clean() transaction.save() with self.assertRaises(ProtectedError): account.delete()
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_partner_save(self): expected_name = ''.join( random.choices(string.ascii_letters + string.digits, k=200)) transaction = models.Transaction() transaction.partner = expected_name transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = self.account transaction.full_clean() transaction.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) self.assertEqual(expected_name, transaction_in_db.partner)
def test_locked_no_change_to_value(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() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) transaction_in_db.value = 1 with self.assertRaises(ValidationError): transaction_in_db.full_clean()
def test_locked(self): expected_lock = bool(random.getrandbits(1)) transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = self.account transaction.locked = expected_lock transaction.full_clean() transaction.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) transaction_in_db.full_clean() self.assertEqual(expected_lock, transaction_in_db.locked)
def test_account(self): expected_account = models.Account(name="Expected account", balance=Decimal(0)) expected_account.save() transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = 0 transaction.category = self.category transaction.account = expected_account transaction.full_clean() transaction.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) self.assertEqual(expected_account, transaction_in_db.account)
def test_value_save(self): expected_value = (Decimal(random.uniform(-999999999.99, 999999999.99)).quantize( Decimal('.01'))) transaction = models.Transaction() transaction.partner = "a" transaction.date = datetime.date.today() transaction.value = expected_value transaction.category = self.category transaction.account = self.account transaction.full_clean() transaction.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) self.assertEqual(expected_value, transaction_in_db.value)
def test_date_save(self): expected_date = datetime.date(random.randrange(1980, 2100), random.randrange(1, 12), random.randrange(1, 28)) transaction = models.Transaction() transaction.partner = "a" transaction.date = expected_date transaction.value = 0 transaction.category = self.category transaction.account = self.account transaction.full_clean() transaction.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) self.assertEqual(expected_date, transaction_in_db.date)
def test_locked_no_change_to_account(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() account = models.Account(name=_get_random_name(), balance=0) account.save() transaction_in_db = models.Transaction.objects.get(pk=transaction.pk) transaction_in_db.account = account 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