def post(self) -> Response: # pylint: disable=R0201 """A post method is used to send a specific POST request to create Stock Data :return: a Response object with specific data and status code """ body = get_body(request) if not body: return make_response("Wrong data provided", 400) price = body.get('price') created_at = body.get('created_at') stock_id = body.get('stock_id') if not isinstance(price, int): message = "Incorrect price specified, price should be integer (ex. 300)" logger.info(message) return make_response(message, 400) if not isinstance(stock_id, int): message = "Incorrect stock id specified, stock id should be integer (ex. 1)" logger.info(message) return make_response(message, 400) 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 created_at specified, " "example '2018-09-19 01:55:19'(year-month-day hour:minute:second))" ) logger.info(message) return make_response(message, 400) data_to_create = { 'price': price, 'created_at': created_at, 'stock_id': stock_id } stock_data = StockData.create(**data_to_create) if stock_data: return make_response(jsonify(stock_data.to_dict()), 201) return make_response("Creating error", 400)
def test_create_false(self, pool_manager): pool_manager.return_value.__enter__.return_value.cursor.execute.side_effect = psycopg2.DataError self.assertEqual(StockData.create(1, 441.5, datetime(2021, 1, 2, 3, 4, 5)), None)
def test_create_true(self, pool_manager): data = {"id": 1, "stock_id": 1, "price": 441.5, "created_at": datetime(2021, 1, 2, 3, 4, 5)} pool_manager.return_value.__enter__.return_value.cursor.fetchone.return_value = ( 1, 1, 441.5, datetime(2021, 1, 2, 3, 4, 5)) self.assertEqual(StockData.create(1, 441.5, datetime(2021, 1, 2, 3, 4, 5)).to_dict(), data)