def test_get_for_currencies_multiple_currencies(self): currencies = [Mock(), Mock()] moneys = [Money(10, currencies[0]), Money(-8, currencies[1])] balance = Balance(moneys) assert balance.get_for_currency(currencies[0]) == Money( 10, currencies[0]) assert balance.get_for_currency(currencies[1]) == Money( -8, currencies[1])
def get_balances(self) -> List[Balance]: """ Returns a list with the same length as transactions, showing the Balance for the account after the transaction. """ out = [] current_balance = Balance([]) for transaction in self.transactions: moneys = transaction.get_moneys_for_account(self.account) current_balance = current_balance.add_moneys(moneys) out.append(current_balance) return out
def test_get_balances(self): currency_one, currency_two = Mock(), Mock() transactions = [ self.gen_transaction_mock([Money('10', currency_one)]), self.gen_transaction_mock([Money('20', currency_two)]) ] initial_balance = Balance([Money('20', currency_two)]) journal = Journal(Mock(), initial_balance, transactions) result = journal.get_balances() assert result[0] == initial_balance.add_money(Money( '10', currency_one)) assert result[1] == result[0].add_money(Money('20', currency_two))
def journal(self, request, pk=None): def get_transaction_base_qset(): o = Transaction.objects o = o.prefetch_related("movement_set__currency", "movement_set__account__acc_type") o = o.order_by('date', 'pk') o = o.distinct() return o account = self.get_object() journal = Journal( account, Balance([]), get_transaction_base_qset().filter_by_account(account)) serializer = JournalSerializer(journal) data = serializer.data return Response(data)
def test_get_journal(self, m_Transaction_objects, m_Journal, m_JournalSerializer): self.populate_accounts() account = AccountTestFactory() m_JournalSerializer.return_value.data = {"some": "unique value"} resp = self.client.get(f"/accounts/{account.pk}/journal/") # Prepares the queryset for Transactions that should be used m_qset = Transaction.objects m_qset = m_qset.prefetch_related("movement_set__currency", "movement_set__account__acc_type") m_qset = m_qset.order_by('date', 'pk') m_qset = m_qset.distinct() m_qset = m_qset.filter_by_account(account) m_Journal.assert_called_with(account, Balance([]), m_qset) m_JournalSerializer.assert_called_with(m_Journal()) assert resp.json() == m_JournalSerializer().data
def test_equal_false_diff_quantities(self): currency = Mock() one = Balance([Money('10', currency)]) two = Balance([Money('9', currency)]) assert one != two
def test_equal_false_diff_currencies(self): currencies = [Mock(), Mock()] one = Balance([Money('10', currencies[0])]) two = Balance([Money('10', currencies[1])]) assert one != two
def test_equal_true(self): currency = Mock() one = Balance([Money('10', currency)]) two = Balance([Money('7', currency), Money('3', currency)]) assert one == two
def test_get_currencies_base(self): currencies = [Mock(), Mock()] balance = Balance( [Money('11', currencies[0]), Money('7', currencies[1])]) assert balance.get_currencies() == set(currencies)
def test_add_money_two_long(self): moneys = [Mock(quantity=Decimal(5)), Mock(quantity=Decimal(2))] balance = Balance([moneys[0]]) assert balance.add_money(moneys[1]) == Balance(moneys)
def test_add_money_zero(self): money = Mock(quantity=Decimal('10')) balance = Balance([]) assert balance.add_money(money) == Balance([money])
def test_get_for_currency_present_two_movements(self): currency = Mock() moneys = [Money(10, currency), Money(20, currency)] balance = Balance(moneys) assert balance.get_for_currency(currency) == Money(30, currency)
def test_get_for_currency_not_present_return_zero(self): currencies = [Mock(), Mock()] money = Money(10, currencies[0]) balance = Balance([money]) assert balance.get_for_currency(currencies[1]) == Money( 0, currencies[1])
def test_get_for_currency_empty(self): currency = Mock() balance = Balance([]) assert balance.get_for_currency(currency) == Money(0, currency)