def get(self, pk: int) -> Response:  # pylint: disable=C0103, R0201
        """A get method is used to send a specific GET request to access Stock Data by id

        :param pk: Stock Data primary key
        :return: a Response object with specific data and status code
        """
        stock_data = StockData.get_by_id(pk=pk)
        if stock_data is None:
            message = "Can not find stock data, wrong id"
            logger.info(message)
            return make_response(message, 400)
        return make_response(jsonify(stock_data.to_dict()), 200)
    def put(self, pk: int) -> Response:  # pylint: disable=C0103, R0201
        """A put method is used to send a specific PUT request to edit Stock Data by id

        :param pk: Stock Data primary key
        :return: a Response object with specific data and status code
        """
        body = get_body(request)
        if not body:
            return make_response("Wrong data provided", 400)
        stock_data = StockData.get_by_id(pk=pk)
        if stock_data is None:
            message = "Can not find stock data, wrong id"
            logger.info(message)
            return make_response(message, 400)
        price, created_at = body.get('price'), body.get('created_at')

        if price is not None and not isinstance(price, int):
            message = "Incorrect price specified, price should be integer (ex. 300)"
            logger.info(message)
            return make_response(message, 400)

        if created_at:
            if not isinstance(created_at, str):
                message = (
                    "Incorrect created_at specified, "
                    "example '2018-09-19 01:55:19'(year-month-day hour:minute:second)"
                )
                logger.info(message)
                return make_response(message, 400)
            try:
                created_at = datetime.strptime(created_at, '%Y-%m-%d %H:%M:%S')
            except ValueError:
                message = "Incorrect date specified, example '2018-09-19 01:55:19'(year-month-day hour:minute:second))"
                logger.info(message)
                return make_response(message, 400)

        data_to_update = {"price": price, "created_at": created_at}
        stock_data_updated = stock_data.update(**data_to_update)

        if stock_data_updated:
            return make_response(jsonify(stock_data.to_dict()), 200)
        message = "Stock Data is not updated, possible you input wrong data"
        logger.info(message)
        return make_response(message, 400)
示例#3
0
 def test_get_by_id_error(self, pool_manager):
     pool_manager.return_value.__enter__.return_value.cursor.execute.side_effect = psycopg2.DataError
     self.assertEqual(StockData.get_by_id(1), None)
示例#4
0
 def test_get_by_id(self, pool_manager):
     data = {"id": 1, "stock_id": 1, "price": 441.5, "created_at": datetime(2021, 1, 2, 3, 4, 6)}
     pool_manager.return_value.__enter__.return_value.cursor.fetchone.return_value = (
     1, 1, 441.5, datetime(2021, 1, 2, 3, 4, 6))
     self.assertEqual(StockData.get_by_id(1).to_dict(), data)