def test_get_dashboard_pass(self, mock_get, mock_get_stocks): with app.app_context(): mock_get.return_value = Dashboard(dashboard_hash='awf241af') mock_get_stocks.return_value = [ Stock( pk=1, name="A", company_name="Agilent Technologies Inc. Common Stock", country="United States", industry="Biotechnology: Laboratory Analytical Instruments", sector="Capital Goods", in_use=False) ] with app.test_client() as client: response = client.get('/api/dashboard/awf241af') body = json.loads(response.data) assert response.status_code == 200 assert body['stocks'] == [{ "company_name": "Agilent Technologies Inc. Common Stock", "id": 1, "in_use": False, "name": "A", "country": "United States", "industry": "Biotechnology: Laboratory Analytical Instruments", "sector": "Capital Goods" }]
def test_get_fail(self, mock_get): with app.app_context(): message = b"Can not find stock data, wrong id" mock_get.return_value = None with app.test_client() as client: response = client.get('/stocks_data/2') assert response.status_code == 400 assert response.data == message
def test_delete_fail(self, mock_delete): with app.app_context(): message = b"Stock data not deleted" mock_delete.return_value = None with app.test_client() as client: response = client.delete('/stocks_data/1') assert response.status_code == 400 assert response.data == message
def test_get_dashboard_wrong_hash(self, mock_get): message = b'Can not find dashboard, wrong hash' with app.app_context(): mock_get.return_value = None with app.test_client() as client: response = client.get('/api/dashboard/bbf03b8') assert response.status_code == 400 assert response.data == message
def test_put_not_existing_pk(self, mock_get): with app.app_context(): message = b"Can not find stock data, wrong id" mock_get.return_value = None with app.test_client() as client: data = {"price": 500, "created_at": "2021-01-08"} response = client.put('/stocks_data/2', json=data) assert response.status_code == 400 assert response.data == message
def test_post_create_fail(self, mock_post): with app.app_context(): message = b"Incorrect created_at specified, example '2018-09-19 01:55:19'(year-month-day hour:minute:second))" mock_post.return_value = None with app.test_client() as client: data = {"stock_id": 3, "price": 300} response = client.post('/stocks_data/', json=data) assert response.status_code == 400 assert response.data == message
def test_get_dashboard_fail_no_stocks(self, mock_get, mock_get_stocks): message = b'Can not find any stocks in dashboard' with app.app_context(): mock_get.return_value = Dashboard(dashboard_hash='00e947a3') mock_get_stocks.return_value = [] with app.test_client() as client: response = client.get('/api/dashboard/00e947a3') assert response.status_code == 400 assert response.data == message
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 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_post_dashboard_pass(self, mock_get_by_id, mock_get_ids, mock_post): data = {"all_stocks": [{"id": 1, "name": "A"}], "missing_names": []} with app.app_context(): mock_get_ids.return_value = Stock( pk=1, name="A", company_name="Agilent Technologies Inc. Common Stock", country="United States", industry="Biotechnology: Laboratory Analytical Instruments", sector="Capital Goods", in_use=False) mock_post.return_value = Dashboard(dashboard_hash='f79ee4f2') mock_get_by_id.return_value = Stock( pk=1, name="A", company_name="Agilent Technologies Inc. Common Stock", country="United States", industry="Biotechnology: Laboratory Analytical Instruments", sector="Capital Goods", in_use=True) with app.test_client() as client: response = client.post('/api/dashboard/', json=data) assert response.status_code == 201