Exemplo n.º 1
0
 def test_get_transaction(self):
     transaction_details = {"id": 15, "amount": 12, "type": 'debit'}
     transaction, err = TransactionController.create_transaction_record(
         **transaction_details)
     transaction, err = TransactionController.get_transaction(
         **transaction_details)
     self.assertEqual(transaction_details.get("id"), transaction.get("id"))
Exemplo n.º 2
0
    def test_get_transaction_list(self):
        first_transaction_details = {"id": 20, "amount": 11, "type": 'debit'}
        first_transaction, err = TransactionController.create_transaction_record(
            **first_transaction_details)
        second_transaction_details = {
            "id": 21,
            "amount": 12,
            "type": 'debit',
            "parent_id": 20
        }

        second_transaction, err = TransactionController.create_transaction_record(
            **second_transaction_details)

        third_transaction_details = {
            "id": 22,
            "amount": 100,
            "type": 'credit',
            "parent_id": 21
        }

        third_transaction, err = TransactionController.create_transaction_record(
            **third_transaction_details)

        transaction_filter = {"type": "debit"}
        transaction_list, err = TransactionController.get_transaction_list(
            **transaction_filter)

        self.assertEqual(len(transaction_list), 2)
Exemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        logger.info("********** Inside Transaction List API By Type**********")
        logger.info("Request data: %s" % request.data)
        transaction_type = kwargs.get('type')

        code = None
        msg = None
        if not transaction_type:
            code, msg = Errors.Missing.TRANSACTION_ID_MISSING
            error = True

        transaction_filter={"type":transaction_type}
        transaction_list,err = TransactionController.get_transaction_list(**transaction_filter)
        resp = []
        for transaction in transaction_list:
            del transaction['id']
            resp.append(transaction)
        if err:
            code, msg = err[0],err[1]
            response = {"status": code, "message": msg}
            return Response(response, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        response = {"transactions":resp}

        logger.info("Response: %s" % response)

        return Response(response, status=status.HTTP_200_OK)
Exemplo n.º 4
0
    def get(self, request, *args, **kwargs):
        logger.info("********** Inside Transaction API **********")
        logger.info("Request data: %s" % request.data)
        transaction_id = kwargs.get('transaction_id')

        code = None
        msg = None
        if not transaction_id:
            code, msg = Errors.Missing.TRANSACTION_ID_MISSING
            response = {"status": code, "message": msg}
            return Response(response, status=status.HTTP_400_BAD_REQUEST)

        transaction_filter={"id":transaction_id}
        transaction,err = TransactionController.get_transaction(**transaction_filter)

        if err:
            code, msg = err[0],err[1]
            response = {"status": code, "message": msg}
            return Response(response, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        del transaction['id']
        response = transaction

        logger.info("Response: %s" % response)

        return Response(response, status=status.HTTP_200_OK)
Exemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        logger.info("****Inside Related Transaction Sum API *****")
        transaction_id = kwargs.get('transaction_id')

        code = None
        msg = None
        if not transaction_id:
            code, msg = Errors.Missing.TRANSACTION_ID_MISSING
            response = {"status": code, "message": msg}
            return Response(response, status=status.HTTP_400_BAD_REQUEST)
        try:
            total_sum, err = TransactionController.calculate_sum_of_linked_transactions(
                transaction_id=transaction_id)
            if err:
                code, msg = err[0], err[1]
                response = {"status": code, "message": msg}
                return Response(response,
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)

            response = {"sum": total_sum}

            logger.info("Response: %s" % response)

            return Response(response, status=status.HTTP_200_OK)
        except Exception as e:
            response = {
                "status": Errors.Generic.GENERIC[0],
                "message": Errors.Generic.GENERIC[1]
            }
            return Response(response,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 6
0
    def test_create_transaction_without_parent(self):
        """Create a transaction record without parent"""
        transaction_details = {"id": 8, "amount": 11, "type": 'debit'}

        transaction, err = TransactionController.create_transaction_record(
            **transaction_details)

        self.assertEqual(transaction_details.get("id"), transaction.get("id"))
Exemplo n.º 7
0
    def test_create_transaction_with_parent(self):
        """Create a transaction record with parent"""
        parent_transaction_details = {"id": 1, "amount": 12, "type": 'debit'}

        parent_transaction, err = TransactionController.create_transaction_record(
            **parent_transaction_details)

        transaction_details = {
            "id": 9,
            "amount": 12,
            "type": 'debit',
            "parent_id": 1
        }

        transaction, err = TransactionController.create_transaction_record(
            **transaction_details)

        self.assertEqual(parent_transaction.get("id"),
                         transaction.get("parent_id"))
Exemplo n.º 8
0
    def test_calculate_sum_of_linked_transaction(self):
        first_transaction_details = {"id": 9, "amount": 11, "type": 'debit'}
        first_transaction, err = TransactionController.create_transaction_record(
            **first_transaction_details)
        second_transaction_details = {
            "id": 10,
            "amount": 12,
            "type": 'debit',
            "parent_id": 9
        }

        second_transaction, err = TransactionController.create_transaction_record(
            **second_transaction_details)

        third_transaction_details = {
            "id": 11,
            "amount": 100,
            "type": 'credit',
            "parent_id": 10
        }

        third_transaction, err = TransactionController.create_transaction_record(
            **third_transaction_details)

        forth_transaction_details = {
            "id": 12,
            "amount": 100,
            "type": 'credit',
            "parent_id": None
        }

        forth_transaction, err = TransactionController.create_transaction_record(
            **forth_transaction_details)

        total_amount, err = TransactionController.calculate_sum_of_linked_transactions(
            transaction_id=9)
        actual_total_amount = first_transaction_details.get(
            "amount") + second_transaction_details.get(
                "amount") + third_transaction_details.get("amount")
        self.assertEqual(total_amount, actual_total_amount)
Exemplo n.º 9
0
    def put(self, request, *args, **kwargs):
        logger.info("********** Inside Transaction API **********")
        logger.info("Request data: %s" % request.data)

        transaction_id = kwargs.get('transaction_id')
        code = None
        msg = None
        if not transaction_id:
            code, msg = Errors.Missing.TRANSACTION_ID_MISSING
            response = {"status": code, "message": msg}
            return Response(response, status=status.HTTP_400_BAD_REQUEST)

        validation_check_response = CreateTransactionSerializer(data=request)
        validation_check_response.is_valid(raise_exception=True)

        create_transaction_dict = {
            "id": transaction_id,
            "amount": request.data["amount"],
            "parent_id": request.data.get("parent_id", None),
            "type": request.data["type"]
        }
        try:
            transaction, err = TransactionController.create_transaction_record(
                **create_transaction_dict)

            if transaction:
                code = 0
                msg = "ok"
            if err:
                code, msg = err[0], err[1]
                response = {"status": code, "message": msg}
                return Response(response,
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)

            response = {
                "status": "code",
                "message": msg,
            }

            logger.info("Response: %s" % response)

            return Response(response, status=status.HTTP_201_CREATED)
        except Exception as e:
            response = {
                "status": Errors.DatabaseError.GENERIC_CREATE[0],
                "message": Errors.DatabaseError.GENERIC_CREATE[1]
            }
            return Response(response,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 10
0
    def get(self, request, *args, **kwargs):
        logger.info("********** Inside Transaction API **********")
        logger.info("Request data: %s" % request.data)
        transaction_id = kwargs.get('transaction_id')

        code = None
        msg = None
        if not transaction_id:
            code, msg = Errors.Missing.TRANSACTION_ID_MISSING
            response = {"status": code, "message": msg}
            return Response(response, status=status.HTTP_400_BAD_REQUEST)
        try:
            transaction_filter = {"id": transaction_id}
            transaction, err = TransactionController.get_transaction(
                **transaction_filter)

            if err:
                code, msg = err[0], err[1]
                response = {"status": code, "message": msg}
                return Response(response,
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)

            response = {
                "amount": transaction["amount"],
                "parent_id": transaction["parent_id"],
                "type": transaction["type"]
            }

            logger.info("Response: %s" % response)

            return Response(response, status=status.HTTP_200_OK)

        except Exception as e:
            response = {
                "status": Errors.DatabaseError.GENERIC_LIST[0],
                "message": Errors.DatabaseError.GENERIC_LIST[1]
            }
            return Response(response,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 11
0
    def test_calculate_sum_of_linked_transaction_with_no_transaction(self):

        total_amount, err = TransactionController.calculate_sum_of_linked_transactions(
            transaction_id=22)

        self.assertEqual(total_amount, 0)