def test_loan_account_match_then_decrease_matched_amount(self): """ Does match between loan_account_deposit and loan_account_withdraw then decrease matched amount """ test_account = Account.objects.create(name='Test Account', currency=CURRENCY_CUC, balance=1000) test_account2 = Account.objects.create(name='Test Loan Account', currency=CURRENCY_CUC, balance=1000) test_loan_account = LoanAccount.objects.create(account=test_account2) test_date = timezone.now() test_amount = 100 test_status = STATUS_READY loan_account_deposit = LoanAccountDeposit( date=test_date, account=test_account, amount=test_amount, loan_account=test_loan_account, status=test_status) loan_account_deposit = FinanceServices.save_loan_account_deposit( user=self.test_user, loan_account_deposit=loan_account_deposit) loan_account_withdraw = LoanAccountWithdraw( date=test_date, account=test_account, amount=test_amount, loan_account=test_loan_account, status=test_status) loan_account_withdraw = FinanceServices.save_loan_account_withdraw( user=self.test_user, loan_account_withdraw=loan_account_withdraw) loan_account_match = LoanAccountMatch( loan_account_deposit=loan_account_deposit, loan_account_withdraw=loan_account_withdraw, matched_amount=test_amount) loan_account_match = FinanceServices.save_loan_account_match( loan_account_match) test_loan_account = LoanAccount.objects.get(pk=test_loan_account.pk) # entity matched incremented self.assertEqual(test_loan_account.matched_amount, test_amount) loan_account_deposit.refresh_from_db() loan_account_withdraw.refresh_from_db() self.assertEqual(loan_account_deposit.matched_amount, test_amount) self.assertEqual(loan_account_withdraw.matched_amount, test_amount) # change amount delta = -10 loan_account_match.matched_amount += delta loan_account_match = FinanceServices.save_loan_account_match( loan_account_match) test_loan_account = LoanAccount.objects.get(pk=test_loan_account.pk) # entity matched changed self.assertEqual(test_loan_account.matched_amount, test_amount + delta) loan_account_deposit.refresh_from_db() loan_account_withdraw.refresh_from_db() self.assertEqual(loan_account_deposit.matched_amount, test_amount + delta) self.assertEqual(loan_account_withdraw.matched_amount, test_amount + delta)
def test_loan_account_match_then_change_loan_account(self): """ Does match between loan_account_deposit and loan_account_withdraw then change loan_account """ test_account = Account.objects.create(name='Test Account', currency=CURRENCY_CUC, balance=1000) test_account2 = Account.objects.create(name='Test Loan Account1', currency=CURRENCY_CUC, balance=1000) test_loan_account1 = LoanAccount.objects.create(account=test_account2) test_date = timezone.now() test_amount = 100 test_status = STATUS_READY loan_account_deposit = LoanAccountDeposit( date=test_date, account=test_account, amount=test_amount, loan_account=test_loan_account1, status=test_status) loan_account_deposit = FinanceServices.save_loan_account_deposit( user=self.test_user, loan_account_deposit=loan_account_deposit) loan_account_withdraw = LoanAccountWithdraw( date=test_date, account=test_account, amount=test_amount, loan_account=test_loan_account1, status=test_status) loan_account_withdraw = FinanceServices.save_loan_account_withdraw( user=self.test_user, loan_account_withdraw=loan_account_withdraw) loan_account_match = LoanAccountMatch( loan_account_deposit=loan_account_deposit, loan_account_withdraw=loan_account_withdraw, matched_amount=test_amount) loan_account_match = FinanceServices.save_loan_account_match( loan_account_match) loan_account_deposit.refresh_from_db() loan_account_withdraw.refresh_from_db() self.assertEqual(loan_account_deposit.matched_amount, test_amount) self.assertEqual(loan_account_withdraw.matched_amount, test_amount) test_account4 = Account.objects.create(name='Test Loan Account2', currency=CURRENCY_CUC, balance=1000) test_loan_account2 = LoanAccount.objects.create(account=test_account4) loan_account_withdraw.loan_account = test_loan_account2 with self.assertRaisesMessage(ValidationError, ERROR_MATCH_LOAN_ACCOUNT): loan_account_withdraw = FinanceServices.save_loan_account_withdraw( user=self.test_user, loan_account_withdraw=loan_account_withdraw)