def test_balance_decimal_places(self): balance = Decimal('123456789.123') account = models.Account(name=_get_random_name(), balance=balance) with self.assertRaises(ValidationError): account.full_clean()
def test_total_ignore_other_accounts(self): #pylint: disable=unused-variable starting_balance = Decimal(random.uniform(-9999.99, 9999.99)).quantize( Decimal('.01')) account = models.Account() account.balance = starting_balance account.save() tomorrow = datetime.date.today() + datetime.timedelta(days=1) transactions = ([ _create_transaction(tomorrow.month, tomorrow.year, account) for _ in range(10) ]) for _ in range(10): _create_transaction(tomorrow.month, tomorrow.year, account, locked=True) expected_total = ((starting_balance + sum(Decimal(t.value) for t in transactions)).quantize( Decimal('.01'))) account_in_db = models.Account.objects.get(pk=account.pk) self.assertAlmostEqual(expected_total, account_in_db.total, 2)
def test_name_max_value(self): expected_name = ''.join( random.choices(string.ascii_letters + string.digits, k=201)) account = models.Account() account.name = expected_name account.balanace = 0 with self.assertRaises(ValidationError): account.full_clean()
def test_total_no_transactions(self): expected_total = (Decimal(random.uniform(-999999999.99, 999999999.99)).quantize( Decimal('.01'))) account = models.Account() account.balance = expected_total account.save() account_in_db = models.Account.objects.get(pk=account.pk) self.assertEqual(expected_total, account_in_db.total)
def test_balance(self): expected_balance = (Decimal(random.uniform(-999999999.99, 999999999.99)).quantize( Decimal('.01'))) account = models.Account() account.balance = expected_balance account.save() account_in_db = models.Account.objects.get(pk=account.pk) self.assertEqual(expected_balance, account_in_db.balance)
def test_name_save(self): expected_name = ''.join( random.choices(string.ascii_letters + string.digits, k=200)) account = models.Account() account.name = expected_name account.balance = Decimal(0) account.full_clean() account.save() account_in_db = models.Account.objects.get(pk=account.pk) self.assertEqual(expected_name, account_in_db.name)
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_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_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
def test_total_only_unlocked_transactions(self): starting_balance = (Decimal(random.uniform(-9999.99, 9999.99)).quantize( Decimal('.01'))) account = models.Account() account.balance = starting_balance account.save() tomorrow = datetime.date.today() + datetime.timedelta(days=1) transactions = ([ _create_transaction(tomorrow.month, tomorrow.year, account) for _ in range(1) ]) expected_total = ((starting_balance + sum(Decimal(t.value) for t in transactions)).quantize( Decimal('.01'))) account_in_db = models.Account.objects.get(pk=account.pk) self.assertEqual(expected_total, account_in_db.total)
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() account = models.Account(name=expected_name) self.assertEqual(expected_name, account.__str__())