Beispiel #1
0
    def test_calculate_balance_may_do_it_properly(self):
        user = UserFactory()
        account_1 = '00001'
        inflow_transaction_1_account_1 = TransactionFactory(
            type=Transaction.INFLOW, account=account_1, user=user)
        inflow_transaction_2_account_1 = TransactionFactory(
            type=Transaction.INFLOW, account=account_1, user=user)
        outflow_transaction_1_account_1 = TransactionFactory(
            type=Transaction.OUTFLOW, account=account_1, user=user)
        outflow_transaction_2_account_1 = TransactionFactory(
            type=Transaction.OUTFLOW, account=account_1, user=user)

        total_inflow_account_1 = round(
            Decimal(inflow_transaction_1_account_1.amount +
                    inflow_transaction_2_account_1.amount), 2)
        total_outflow_account_1 = round(
            Decimal(outflow_transaction_1_account_1.amount +
                    outflow_transaction_2_account_1.amount), 2)
        balance_account_1 = round(
            Decimal(total_inflow_account_1 + total_outflow_account_1), 2)

        account_2 = '00002'
        inflow_transaction_1_account_2 = TransactionFactory(
            type=Transaction.INFLOW, account=account_2, user=user)
        inflow_transaction_2_account_2 = TransactionFactory(
            type=Transaction.INFLOW, account=account_2, user=user)
        outflow_transaction_1_account_2 = TransactionFactory(
            type=Transaction.OUTFLOW, account=account_2, user=user)
        outflow_transaction_2_account_2 = TransactionFactory(
            type=Transaction.OUTFLOW, account=account_2, user=user)

        total_inflow_account_2 = round(
            Decimal(inflow_transaction_1_account_2.amount +
                    inflow_transaction_2_account_2.amount), 2)
        total_outflow_account_2 = round(
            Decimal(outflow_transaction_1_account_2.amount +
                    outflow_transaction_2_account_2.amount), 2)
        balance_account_2 = round(
            Decimal(total_inflow_account_2 + total_outflow_account_2), 2)

        transactions = Transaction.objects.filter(user=user)
        queryset = TransactionService.calculate_balance(transactions)

        expected_account_1 = {
            'account': '00001',
            'total_inflow': total_inflow_account_1,
            'total_outflow': total_outflow_account_1,
            'balance': balance_account_1
        }
        expected_account_2 = {
            'account': '00002',
            'total_inflow': total_inflow_account_2,
            'total_outflow': total_outflow_account_2,
            'balance': balance_account_2
        }
        self.assertEquals(expected_account_1, queryset[0])
        self.assertEquals(expected_account_2, queryset[1])
Beispiel #2
0
    def retrieve(self, request, *args, **kwargs):
        user_id = kwargs['user_id']

        transactions = Transaction.objects.filter(user_id=user_id)
        filtered_transactions = self.filter_queryset(transactions)
        processed_transactions = TransactionService.calculate_balance(
            filtered_transactions)
        paginated_transactions = self.paginate_queryset(processed_transactions)
        serializer = TransactionOutputSerializer(paginated_transactions,
                                                 many=True)
        return self.get_paginated_response(serializer.data)