def get_data_for_last_day(cls, pk: int) -> list: stock_data_for_last_day = [] datetime_now = datetime.now().strftime(DATETIME_PATTERN) datetime_yesterday = (datetime.now() - timedelta(days=1)).strftime(DATETIME_PATTERN) with pool_manager() as conn: query = """SELECT * FROM stocks_data WHERE stock_id = %(stock_id)s AND %(yesterday)s <= created_at AND created_at < %(today)s ORDER BY created_at;""" try: conn.cursor.execute( query, { 'stock_id': pk, 'yesterday': datetime_yesterday, 'today': datetime_now }) stock_data_for_last_day = conn.cursor.fetchall() except (psycopg2.DataError, psycopg2.ProgrammingError, TypeError) as err: logger.info(f"Error! {err}") stock_data_for_last_day = [ StockData(pk=pk, stock_id=stock_id, price=price, created_at=created_at) for pk, stock_id, price, created_at in stock_data_for_last_day ] return stock_data_for_last_day
def test_put_success(self, mock_get, mock_put): with app.app_context(): mock_get.return_value = StockData(stock_id=2, price=300, created_at="2020-08-19 01:55:19", pk=1) mock_put.return_value = StockData(stock_id=2, price=500, created_at="2020-09-19 01:55:19", pk=1) with app.test_client() as client: data = {"price": 500, "created_at": "2020-09-19 01:55:19"} response = client.put('/stocks_data/1', json=data) body = json.loads(response.data) assert response.status_code == 200 assert body['created_at'] == "2020-09-19 01:55:19" assert body['price'] == 500 assert body['stock_id'] == 2
def test_put_wrong_create_at(self, mock_get): with app.app_context(): message = b"Incorrect date specified, example '2018-09-19 01:55:19'(year-month-day hour:minute:second))" mock_get.return_value = StockData(stock_id=2, price=300, created_at="19/09/19 01:55:19", pk=1) with app.test_client() as client: data = {"price": 300, "created_at": "2021/01/08"} response = client.put('/stocks_data/2', json=data) assert response.status_code == 400 assert response.data == message
def test_put_wrong_price(self, mock_get): with app.app_context(): message = b"Incorrect price specified, price should be integer (ex. 300)" mock_get.return_value = StockData(stock_id=2, price=300, created_at="19/09/19 01:55:19", pk=1) with app.test_client() as client: data = {"price": "wrong", "created_at": "19/09/19 01:55:19"} response = client.put('/stocks_data/2', json=data) assert response.status_code == 400 assert response.data == message
def test_put_wrong_json(self, mock_get): with app.app_context(): message = b"Wrong data provided" mock_get.return_value = StockData(stock_id=2, price=300, created_at="2020-09-19 01:55:19", pk=1) with app.test_client() as client: incorrect_json = '{ "stock_id:"12 "price":30 "created_at:"None" }' response = client.put('/stocks_data/2', data=incorrect_json) assert response.status_code == 400 assert response.data == message
def test_put_unknown_error(self, mock_put, mock_get): with app.app_context(): message = b"Stock Data is not updated, possible you input wrong data" mock_get.return_value = StockData(stock_id=2, price=300, created_at="18/09/19 01:55:19", pk=1) mock_put.return_value = None with app.test_client() as client: data = {"price": 500, "created_at": "2020-09-19 01:55:19"} response = client.put('/stocks_data/1', json=data) assert response.status_code == 400 assert response.data == message
def test_get_pass(self, mock_get): with app.app_context(): mock_get.return_value = StockData(stock_id=2, price=300, created_at="18-09-19 01:55:19", pk=1) with app.test_client() as client: response = client.get('/stocks_data/1') body = json.loads(response.data) assert response.status_code == 200 assert body['id'] == 1 assert body['created_at'] == '18-09-19 01:55:19' assert body['price'] == 300 assert body['stock_id'] == 2
def test_post_pass(self, mock_post): with app.app_context(): mock_post.return_value = StockData( stock_id=1, price=500, created_at="2020-05-11 04:22:30") with app.test_client() as client: data = { "price": 500, "created_at": "2020-05-11 04:22:30", "stock_id": 1 } response = client.post('/stocks_data/', json=data) assert response.status_code == 201 body = json.loads(response.data) assert response.status_code == 201 assert body['created_at'] == "2020-05-11 04:22:30" assert body['price'] == 500 assert body['stock_id'] == 1
def get_data_for_time_period(self, datetime_from: datetime, datetime_to: datetime) -> list: """ Return list of stock datas for current stock in specified period of time :param datetime_from: start time of period to get stock data :param datetime_to: end time of period to get stock data :return: list of StockData """ stock_data_for_time_period = [] datetime_from_str, datetime_to_str = datetime_from.strftime( DATETIME_PATTERN), datetime_to.strftime(DATETIME_PATTERN) with pool_manager() as conn: query = "SELECT id, stock_id, price, created_at FROM stocks_data " \ "WHERE stock_id = %(stock_id)s " \ "AND %(datetime_from)s <= created_at AND created_at < %(datetime_to)s " \ "ORDER BY created_at;" try: conn.cursor.execute( query, { 'stock_id': self.pk, 'datetime_from': datetime_from_str, 'datetime_to': datetime_to_str }) stock_data_for_time_period = conn.cursor.fetchall() except (psycopg2.DataError, psycopg2.ProgrammingError, TypeError): message = f"Could not get stock data for pk={self.pk}, datetime_from={datetime_from_str}, " \ f"datetime_to={datetime_to_str}" logger.warning(message) stock_data_for_time_period = [ StockData(pk=pk, stock_id=stock_id, price=price, created_at=created_at) for pk, stock_id, price, created_at in stock_data_for_time_period ] if Stock._are_gaps_in_data(datetime_from, datetime_to, stock_data_for_time_period): self._fill_gaps_in_data(datetime_from, datetime_to, stock_data_for_time_period) return stock_data_for_time_period
def test_update_false(self, pool_manager): pool_manager.return_value.__enter__.return_value.cursor.execute.side_effect = psycopg2.DataError self.assertEqual( StockData(2, 114.6, datetime(2020, 1, 2, 3, 4, 5)).update(441.5, datetime(2021, 1, 2, 3, 4, 5)), False)
def test_update_true(self, pool_manager): pool_manager.return_value.__enter__.return_value.cursor.fetchone.return_value = ( 2, 1, 441.5, datetime(2021, 1, 2, 3, 4, 5)) pool_manager.return_value.__exit__.return_value = True self.assertEqual( StockData(1, 111.1, datetime(2020, 3, 2, 1, 5, 6)).update(441.5, datetime(2021, 1, 2, 3, 4, 5)), True)