def test_post_data_fail_wrong_create_at(self): with app.test_client() as client: message = b"Incorrect created_at specified, example '2018-09-19 01:55:19'(year-month-day hour:minute:second))" data = {"stock_id": 3, "price": 300, "created_at": "20-01-01"} response = client.post('/stocks_data/', json=data) assert response.status_code == 400 assert response.data == message
def client(): app.config['TESTING'] = True rabbitmq_my_proc = factories.rabbitmq_proc(port=8888) rabbitmq = factories.rabbitmq('rabbitmq_my_proc') with app.test_client() as client: yield client
def test_create(self): stock_id, stock_name, stock_company_name, stock_country, stock_industry, stock_sector, stock_in_use = ( 1, 'mock create name', 'mocked create company name', 'mocked create country', 'mocked create industry', 'mocked create sector', False) create = self.stock_mock.create create.return_value = Stock(pk=stock_id, name=stock_name, company_name=stock_company_name, country=stock_country, industry=stock_industry, sector=stock_sector, in_use=stock_in_use) with app.test_client() as client: response = client.post(BASE_URL, json={ 'name': stock_name, 'company_name': stock_company_name, 'country': stock_country, 'industry': stock_industry, 'sector': stock_sector }) self.assertEqual( json.loads(response.data), { 'id': stock_id, 'name': stock_name, 'company_name': stock_company_name, 'country': stock_country, 'industry': stock_industry, 'sector': stock_sector, 'in_use': False })
def test_get_by_id(self): stock_id, stock_name, stock_company_name, stock_country, stock_industry, stock_sector = ( 1, 'mocked get name', 'mocked get company name', 'mocked get country', 'mocked industry', 'mocked sector') get_by_id = self.stock_mock.get_by_id get_by_id.return_value.to_dict.return_value = { 'id': stock_id, 'name': stock_name, 'company_name': stock_company_name, 'country': stock_country, 'industry': stock_industry, 'sector': stock_sector } with app.test_client() as client: response = client.get(BASE_URL + '{stock_id}'.format(stock_id=stock_id)) self.assertEqual( json.loads(response.data), { 'company_name': stock_company_name, 'id': stock_id, 'name': stock_name, 'country': stock_country, 'industry': stock_industry, 'sector': stock_sector })
def test_post_dashboard_fail_invalid_data(self): message = b"Wrong data provided" incorrect_json = '{ "stocks: [1,23,4]" }' with app.test_client() as client: response = client.post('/api/dashboard/', data=incorrect_json) assert response.status_code == 400 assert response.data == message
def test_get_stock_data_for_time_period_wrong_time(self): get_by_id = self.stock_mock.get_by_id get_by_id.return_value = self.stock_mock(1, 'IBM', 'IBM', 'United States', 'Computer Manufacturing', 'Technology', False) self.stock_mock.to_dict.return_value = { 'pk': 1, 'name': 'IBM', 'company_name': 'IBM', 'country': 'United States', 'industry': 'Computer Manufacturing', 'sector': 'Technology', 'in_use': False } stock_id = 1 with app.test_client() as client: response = client.get( BASE_URL + '{stock_id}?from=2020/01 05:21:22&to=2020-05-11 04:22:30'. format(stock_id=stock_id)) self.assertEqual( response.data, b"Incorrect date specified, example '2018-09-19 01:55:19'" b"(year-month-day hour:minute:second)") self.assertEqual(response.status, STATUS_400)
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_delete_by_id_missing_id(self): delete_by_id = self.stock_mock.delete_by_id delete_by_id.return_value = False stock_id = 1 with app.test_client() as client: response = client.delete(BASE_URL + '{stock_id}'.format(stock_id=stock_id)) self.assertEqual(response.status, STATUS_400)
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_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_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_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_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_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_post_data_fail(self): with app.test_client() as client: message = b"Wrong data provided" incorrect_json = '{ "stock_id:"12 "price":30 "created_at:"None" }' response = client.post('/stocks_data/', data=incorrect_json) assert response.status_code == 400 assert response.data == message response = client.post('/stocks_data/') assert response.status_code == 400 assert response.data == message
def test_get_by_id_missing_id(self): stock_id, stock_name, stock_company_name, stock_country, stock_industry, stock_sector, stock_in_use = ( 1, 'mock create name', 'mocked create company name', 'mocked create country', 'mocked create industry', 'mocked create sector', False) get_by_id = self.stock_mock.get_by_id get_by_id.return_value = False with app.test_client() as client: response = client.get(BASE_URL + '{stock_id}'.format(stock_id=stock_id)) self.assertEqual(response.status, STATUS_400)
def test_post_data_fail_wrong_stock_id(self): with app.test_client() as client: message = b"Incorrect stock id specified, stock id should be integer (ex. 1)" data = { "stock_id": "wrong", "price": 300, "createв_at": "18/09/19 01:55:19" } response = client.post('/stocks_data/', json=data) 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_update_by_id_wrong_data(self): stock_id, new_stock_name, new_stock_company_name = 1, 'tooooooo long update stock name', 165428653 stock_id, new_stock_name, new_stock_company_name, new_stock_country, new_stock_industry, new_stock_sector = ( 1, 'tooooooo long update stock name', 165428653, 1, 2, 3) with app.test_client() as client: response = client.put(BASE_URL + '{stock_id}'.format(stock_id=stock_id), json={ 'name': new_stock_name, 'company_name': new_stock_company_name }) self.assertEqual(response.status, STATUS_400)
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_create_wrong_data2(self): stock_id, stock_name, stock_company_name, stock_country, stock_industry, stock_sector, stock_in_use = ( 1, 'mock create name', 'mocked create company name', 'mocked create country', 'mocked create industry', 'mocked create sector', False) create = self.stock_mock.create create.return_value = None with app.test_client() as client: response = client.post(BASE_URL, json={ 'name': stock_name, 'company_name': stock_company_name, 'country': stock_country, 'industry': stock_industry, 'sector': stock_sector }) self.assertEqual(response.status, STATUS_400)
def test_get_all(self): data = { 'pk': 1, 'name': 'IBM', 'company_name': 'IBM', 'country': 'United States', 'industry': 'Computer Manufacturing', 'sector': 'Technology' } expected_result = [data] stock = self.stock_mock(**data) stock.to_dict.return_value = data.copy() get_all = self.stock_mock.get_all get_all.return_value = [stock] with app.test_client() as client: response = client.get(BASE_URL) self.assertEqual(expected_result, json.loads(response.data))
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_update_by_id_missing_id(self): get_by_id = self.stock_mock.get_by_id get_by_id.return_value = None stock_id, new_stock_name, new_stock_company_name, new_stock_country, new_stock_industry, new_stock_sector = ( 1, 'mocked update name', 'mocked update company name', 'mocked update country', 'mocked update industry', 'mocked update sector') with app.test_client() as client: response = client.put(BASE_URL + '{stock_id}'.format(stock_id=stock_id), json={ 'id': stock_id, 'name': new_stock_name, 'company_name': new_stock_company_name, 'country': new_stock_country, 'industry': new_stock_industry, 'sector': new_stock_sector }) self.assertEqual(response.status, STATUS_400)
def test_get_stock_data_for_time_period(self): get_by_id = self.stock_mock.get_by_id get_by_id.return_value = self.stock_mock(1, 'IBM', 'IBM', 'United States', 'Computer Manufacturing', 'Technology') get_data_for_time_period = get_by_id.return_value.get_data_for_time_period stock_data_id = 1 stock_data_stock_id = 1 stock_data_price = 144.15 stock_data_created_at = datetime.datetime(2020, 4, 1, 5, 21, 22) stock_data = self.stock_data_mock(stock_data_id, stock_data_stock_id, stock_data_price, stock_data_created_at) stock_data.to_dict.return_value = { 'id': stock_data_id, 'stock_id': stock_data_stock_id, 'price': stock_data_price, 'created_at': stock_data_created_at } get_data_for_time_period.return_value = [ stock_data, ] stock_id = 1 expected_response = [{ "created_at": "Wed, 01 Apr 2020 05:21:22 GMT", "id": 1, "price": 144.15, "stock_id": 1 }] with app.test_client() as client: response = client.get( BASE_URL + '{stock_id}?from=2020-04-01 05:21:22&to=2020-05-11 04:22:30'. format(stock_id=stock_id)) self.assertEqual(json.loads(response.data), expected_response)
def test_update_by_id(self): stock_id, new_stock_name, new_stock_company_name, new_stock_country, new_stock_industry, new_stock_sector = ( 1, 'mocked update name', 'mocked update company name', 'mocked update country', 'mocked update industry', 'mocked update sector') get_by_id = self.stock_mock.get_by_id get_by_id.return_value = self.stock_mock(1, 'sdfsd', 'sdfsdf', 'agadgadg', 'adgadg', 'adgadgshfhs') get_by_id.return_value.to_dict.return_value = { 'name': new_stock_name, 'id': stock_id, 'company_name': new_stock_company_name, 'country': new_stock_country, 'industry': new_stock_industry, 'sector': new_stock_sector } with app.test_client() as client: response = client.put(BASE_URL + '{}'.format(stock_id), json={ 'name': new_stock_name, 'company_name': new_stock_company_name, 'country': new_stock_country, 'industry': new_stock_industry, 'sector': new_stock_sector }) self.assertEqual( json.loads(response.data), { 'id': stock_id, 'name': new_stock_name, 'company_name': new_stock_company_name, 'country': new_stock_country, 'industry': new_stock_industry, 'sector': new_stock_sector })