Skip to content

Commit

Permalink
Finished model tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edelgm6 committed Jan 15, 2024
1 parent 117db68 commit 655f0d7
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 6 deletions.
107 changes: 107 additions & 0 deletions api/tests/models/test_amortization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import datetime
from django.test import TestCase
from django.core.exceptions import ValidationError
from decimal import Decimal
from api.models import Amortization, Account
from api.tests.testing_factories import AccountFactory, TransactionFactory

class AmortizationTests(TestCase):

def setUp(self):
self.suggested_account = AccountFactory.create()
self.accrued_transaction = TransactionFactory.create()

def test_create_amortization(self):
# Test creating an Amortization instance
amortization = Amortization.objects.create(
accrued_transaction=self.accrued_transaction,
amount=Decimal('1000.00'),
periods=12,
description="Amortization Test",
suggested_account=self.suggested_account
)
self.assertEqual(amortization.accrued_transaction, self.accrued_transaction)
self.assertEqual(amortization.amount, Decimal('1000.00'))
self.assertEqual(amortization.periods, 12)
self.assertEqual(amortization.description, "Amortization Test")
self.assertEqual(amortization.suggested_account, self.suggested_account)
self.assertFalse(amortization.is_closed)

def test_amortize(self):
prepaid_account = AccountFactory(
special_type=Account.SpecialType.PREPAID_EXPENSES
)
amortization = Amortization.objects.create(
accrued_transaction=self.accrued_transaction,
amount=Decimal('1000.00'),
periods=6,
description="Amortization Test",
suggested_account=self.suggested_account
)
amortization.amortize(datetime.date(2023,1,15))
self.assertEqual(amortization.get_remaining_periods(), 5)
self.assertEqual(amortization.get_remaining_balance(), Decimal('833.34'))
self.assertEqual(amortization.get_related_transactions().count(), 1)
self.assertEqual(amortization.get_related_transactions()[0].amount, Decimal('-166.66'))

amortization.amortize(datetime.date(2023,1,16))
amortization.amortize(datetime.date(2023,1,17))
amortization.amortize(datetime.date(2023,1,18))
amortization.amortize(datetime.date(2023,1,19))
amortization.amortize(datetime.date(2023,1,20))
self.assertEqual(amortization.get_remaining_periods(), 0)
self.assertEqual(amortization.get_remaining_balance(), 0)
self.assertEqual(amortization.get_related_transactions().count(), 6)
self.assertEqual(amortization.get_related_transactions()[5].amount, Decimal('-166.70'))

with self.assertRaises(ValidationError):
amortization.amortize(datetime.date(2023,1,21))


def test_get_remaining_balance(self):
amortization = Amortization.objects.create(
accrued_transaction=self.accrued_transaction,
amount=Decimal('1000.00'),
periods=12,
description="Amortization Test",
suggested_account=self.suggested_account
)
TransactionFactory(amortization=amortization, amount=-100)
TransactionFactory(amortization=amortization, amount=-100)
TransactionFactory(amortization=amortization, amount=-100)

self.assertEqual(amortization.get_remaining_balance(), 700)

def test_get_remaining_periods(self):
amortization = Amortization.objects.create(
accrued_transaction=self.accrued_transaction,
amount=Decimal('1000.00'),
periods=12,
description="Amortization Test",
suggested_account=self.suggested_account
)
TransactionFactory(amortization=amortization)
TransactionFactory(amortization=amortization)
TransactionFactory(amortization=amortization)

self.assertEqual(amortization.get_remaining_periods(), 9)

def test_get_related_transactions(self):
amortization = Amortization.objects.create(
accrued_transaction=self.accrued_transaction,
amount=Decimal('1000.00'),
periods=12,
description="Amortization Test",
suggested_account=self.suggested_account
)
TransactionFactory(amortization=amortization)
TransactionFactory(amortization=amortization)
TransactionFactory(amortization=amortization)

self.assertEqual(amortization.get_related_transactions().count(), 3)

def test_round_down(self):
NUMBER = 12.121234
round_down = Amortization._round_down(n=NUMBER)
self.assertEqual(round_down, 12.12)
self.assertEqual(NUMBER, 12.121234)
16 changes: 12 additions & 4 deletions api/tests/models/test_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_unique_together_constraint(self):
def test_plug_reconciliation(self):
today = datetime.date.today()
unrealized_account = AccountFactory(
type=Account.Type.INCOME,
special_type=Account.SpecialType.UNREALIZED_GAINS_AND_LOSSES
)
cash_account = AccountFactory(
Expand All @@ -51,8 +52,14 @@ def test_plug_reconciliation(self):
name='income',
type=Account.Type.INCOME
)
transaction = TransactionFactory(
date=today,
account=income_account,
amount=100
)
journal_entry = JournalEntryFactory(
date=today
date=today,
transaction=transaction
)
journal_entry_item = JournalEntryItemFactory(
journal_entry=journal_entry,
Expand All @@ -74,11 +81,12 @@ def test_plug_reconciliation(self):
reconciliation.plug_investment_change()

self.assertEqual(reconciliation.transaction.amount, 100)
self.assertEqual(cash_account.get_balance(today), 200)
self.assertEqual(unrealized_account.get_balance(today), 100)
print(cash_account.name)
self.assertEqual(cash_account.get_balance(end_date=today), 200)
self.assertEqual(unrealized_account.get_balance(today,start_date=today), 100)

reconciliation.amount = Decimal('50.00')
reconciliation.plug_investment_change()
self.assertEqual(reconciliation.transaction.amount, Decimal('-50.00'))
self.assertEqual(cash_account.get_balance(today), 50)
self.assertEqual(unrealized_account.get_balance(today), Decimal('-50.00'))
self.assertEqual(unrealized_account.get_balance(today,start_date=today), Decimal('-50.00'))
4 changes: 2 additions & 2 deletions api/tests/testing_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Meta:
amount = factory.Faker('pydecimal', left_digits=5, right_digits=2, positive=True)
description = factory.Faker('sentence')
category = factory.Faker('word')
is_closed = factory.Faker('boolean')
is_closed = False
date_closed = factory.Maybe('is_closed', yes_declaration=factory.Faker('date_object'), no_declaration=None)
suggested_account = factory.SubFactory(AccountFactory)
suggested_account = None
type = factory.Iterator(Transaction.TransactionType.choices, getter=lambda c: c[0])
linked_transaction = None # To be set manually if needed
amortization = None # To be set manually if needed
Expand Down

0 comments on commit 655f0d7

Please sign in to comment.