def test_light_endpoint_mqtt_params(mocker): mocker.patch('paho.mqtt.publish.single') app.test_client().post('/light/set', json={ 'friendly_name': 'living_room', 'payload': { 'state': 'ON', 'brightness': 255, 'color': '#0000ff' }, 'feedback': { 'text': 'Okay, ich schalte Licht im Wohnzimmer an' } }) expected_payload = {'brightness': 255, 'color': '#0000ff', 'state': 'ON'} expected_feedback_payload = { 'text': 'Okay, ich schalte Licht im Wohnzimmer an' } paho.mqtt.publish.single.assert_has_calls([ mocker.call('zigbee2mqtt/living_room/set', json.dumps(expected_payload), hostname=mocker.ANY, port=mocker.ANY), mocker.call('hermes/tts/say', json.dumps(expected_feedback_payload), hostname=mocker.ANY, port=mocker.ANY) ]) # pylint: disable=no-member
def test_404_page_not_found(): """ test if can return http 404 error if input invalid url """ with app.test_client() as c: response = c.get('/this_page_not_exists') assert response.status_code == 404
def test_processing_file_is_bad_request(self, MockAPIClass): mock_instance = MockAPIClass.return_value mock_instance.fetch_all.return_value = FILES with app.test_client() as c: rv = c.get( '/api/presentation/files/3cd97393-b441-4d7c-a58f-ec40fa2fee50') self.assertEqual(rv.status_code, 400)
def setUp(self): self.app = api_app.test_client() self.base_response = ({ 'execution_error': None, 'stderr': '', 'stdout': 'Hello World!\n', 'successful': True}).copy()
def test_get_model_list(): """ test if can return model list """ with app.test_client() as c: response = c.get('/REST/api/v1.0/model_list') assert response.status_code == 201
def test_api_helloworld(): """ test if can return http 200 if calling the simplest url """ with app.test_client() as c: response = c.get('/') assert response.status_code == 200
def test_failed_file_is_bad_request(self, MockAPIClass): mock_instance = MockAPIClass.return_value mock_instance.fetch_all.return_value = FILES with app.test_client() as c: rv = c.get( '/api/presentation/files/33e6c735-21bc-422b-a0dc-12a1a4e479bd') self.assertEqual(rv.status_code, 400)
def test_external_paginated_api_error_is_bad_request(self, MockAPIClass): mock_instance = MockAPIClass.return_value mock_instance.fetch_all.side_effect = APIException with app.test_client() as c: rv = c.get( '/api/presentation/files/4a551eec-7dac-46d2-8f17-b6972b864b34') self.assertEqual(rv.status_code, 400)
def test_light_endpoint_response_data(mocker): mocker.patch( 'paho.mqtt.publish.single') # Prevent from publishing a real message response = app.test_client().post( '/light/set', json={ 'friendly_name': 'living_room', 'payload': { 'state': 'ON', 'brightness': 255, 'color': '#0000ff' }, 'feedback': { 'text': 'Okay, ich schalte Licht im Wohnzimmer an' } }) assert ordered(json.loads(response.data)) == ordered( json.loads("""{ "friendly_name": "living_room", "payload": { "state": "ON", "brightness": 255, "color": "#0000ff" }, "feedback": { "text": "Okay, ich schalte Licht im Wohnzimmer an" } }"""))
def test_get_predict_list(): """ test if can return prediction list """ with app.test_client() as c: response = c.get('/REST/api/v1.0/predict_list') assert response.status_code == 201
def test_train_house_price_model(): """ test if can run train method """ with app.test_client()as c: response = c.get('/REST/api/v1.0/train') assert response.status_code == 201
def test_predict_house_price(): """ test if can run predict method """ with app.test_client()as c: response = c.get('/REST/api/v1.0/predict') assert response.status_code == 201
def setUp(self): self.app = api_app.test_client() self.base_response = ({ 'execution_error': None, 'stderr': '', 'stdout': 'Hello World!\n', 'successful': True }).copy()
def setUpClass(cls): db.setup(clean=True) cls.api = app.test_client() cls.api.testing = True cls.user = {'lts_id': '1234', 'secret': ''} result = cls.api.post('/api/v1.0/enroll', query_string=cls.user) data = json.loads(result.data) cls.user['secret'] = data['keep_this']
def client() -> FlaskClient: """ Flask test REST client """ flask_app.config['TESTING'] = True with flask_app.test_client() as client: yield client
def test_app(mocker): from api.app import app records = loader.read_csv("./resources/initial.csv") client = app.test_client() mocker = mocker.patch.object(ProductController, "_list_all") mocker.return_value = records yield client
def setUp(self): self.client = app.test_client() # propagate the exceptions to the test client self.client.testing = True Base.metadata.drop_all(bind=db.engine) Base.metadata.create_all(bind=db.engine) db.session.commit()
def test_raw_endpoint_status_ok(mocker): mocker.patch( 'paho.mqtt.publish.single') #Prevent from publishing a real message given_json = load_rhasspy_intent_json() response = app.test_client().post('/light/set/raw', json=json.loads(given_json)) assert response.status_code == 200
def test_predict_house_price_with_input(): """ test if can run predict-with-input (json) method """ headers={"Content-Type": "application/json"} data = '{"MSSubClass":20.0,"LotFrontage":100.0,"LotArea":17500.0,"OverallQual":7.0,"OverallCond":8.0,"YearBuilt":1959.0,"YearRemodAdd":2002.0,"MasVnrArea":0.0,"BsmtFinSF1":1406.0,"BsmtFinSF2":0.0,"BsmtUnfSF":496.0,"TotalBsmtSF":1902.0,"1stFlrSF":1902.0,"2ndFlrSF":0.0,"LowQualFinSF":0.0,"GrLivArea":1902.0,"BsmtFullBath":1.0,"BsmtHalfBath":0.0,"FullBath":2.0,"HalfBath":0.0,"BedroomAbvGr":3.0,"KitchenAbvGr":1.0,"TotRmsAbvGrd":7.0,"Fireplaces":2.0,"GarageYrBlt":1959.0,"GarageCars":2.0,"GarageArea":567.0,"WoodDeckSF":0.0,"OpenPorchSF":207.0,"EnclosedPorch":162.0,"3SsnPorch":0.0,"ScreenPorch":0.0,"PoolArea":0.0,"MiscVal":0.0,"MoSold":5.0,"YrSold":2010.0}' with app.test_client()as c: response = c.post('/REST/api/v1.0/predict_with_input', headers=headers, data=data) assert response.status_code == 201
def setUp(self): self.app = app.test_client() self.app.testing = True self.user_input = { 'name': 'testuser', 'email': '*****@*****.**', 'password': '******' } self.user_logins = {'email': '*****@*****.**', 'password': '******'}
def test_small_image_size(self): with app.test_client() as client: img_frame = np.random.randint(0, 255, size=(args.frame_size, args.image_size - 1, args.image_size - 1, 3)) rv = client.post('/predict', json={'img': img_frame.tolist()}) json_data = rv.get_json() self.assertTrue(json_data['prob_class'] == 1)
def test_external_api_timeout_is_bad_request(self, MockAPIClass): import gevent mock_instance = MockAPIClass.return_value mock_instance.fetch_all.return_value = FILES mock_instance.fetch_details.side_effect = gevent.Timeout with app.test_client() as c: rv = c.get( '/api/presentation/files/4a551eec-7dac-46d2-8f17-b6972b864b34') self.assertEqual(rv.status_code, 400)
def client(): app.Testing = True client = app.test_client() yield client database_url = env_config('MONGO_URL') database_name = database_url.split('/')[-1] db.connection.drop_database(database_name)
def test_value_not_in_storage_is_fetched_from_api(self, MockAPIClass): mock_instance = MockAPIClass.return_value mock_instance.fetch_all.return_value = FILES mock_instance.fetch_details.return_value = FILE_DETAILS[ '4a551eec-7dac-46d2-8f17-b6972b864b34'] mock_instance.fetch_segments.return_value = SEGMENTS[ '4a551eec-7dac-46d2-8f17-b6972b864b34'] with app.test_client() as c: rv = c.get( '/api/presentation/files/4a551eec-7dac-46d2-8f17-b6972b864b34') self.assertEqual(rv.status_code, 201)
def test_get_sources_endpoint(self): """Tests the endpoint to get all sources.""" client = app.test_client() rv = client.get("/api/exchange") self.assertEqual(rv.status_code, 200) data = json.loads(rv.data) self.assertIn("rates", data) rates = data["rates"] self.assertIn("banxico", rates) self.assertIn("diario_oficial", rates) self.assertIn("fixer", rates)
class TestBase(unittest.TestCase): test_app = app.test_client() def setUp(self): Redis.hset('currencies', 'USD', 1) Redis.hset('currencies', 'BRL', 1) self.patcher = patch(OPEN_EXCHANGE_PATH, side_effect=mock_currency_rate) self.patcher.start() def tearDown(self): Redis.delete('currencies') self.patcher.stop()
def test_raw_endpoint_mqtt_params(mocker): mocker.patch('paho.mqtt.publish.single') given_json = load_rhasspy_intent_json() given_dict = json.loads(given_json) app.test_client().post('/light/set/raw', json=given_dict) expected_payload = {'state': 'on', 'brightness': 255, 'color': '#ffffff'} expected_feedback_payload = { 'text': 'Okay, schalte licht im wohnzimmer an' } paho.mqtt.publish.single.assert_has_calls([ mocker.call('zigbee2mqtt/living_room/set', json.dumps(expected_payload), hostname=mocker.ANY, port=mocker.ANY), mocker.call('hermes/tts/say', json.dumps(expected_feedback_payload), hostname=mocker.ANY, port=mocker.ANY) ]) # pylint: disable=no-member
def setUp(self): app.testing = True self.app = app.test_client() self.ride = { "starting_point": "nairobi", "destination": "kiambu", "date": "12/02/2018", "time": "10:00" } self.empty_ride = { "ride_id": "", "starting_point": "", "destination": "kiambu", "date": "12/02/2018", "time": "10:00" }
def test_vincinv(self): query = { 'lat1': -37.57037203, 'lon1': 144.25295244, 'lat2': -37.39101561, 'lon2': 143.5535383, 'from_angle_type': 'dms', 'to_angle_type': 'dms' } expected_response = { 'ell_dist': 54972.289, 'azimuth1to2': 306.52053231124, 'azimuth2to1': 127.10250207968 } response = app.test_client().get('/vincinv', query_string=query) self.assertEqual(json.loads(response.data), expected_response)
def test_light_endpoint_status_ok(mocker): mocker.patch( 'paho.mqtt.publish.single') #Prevent from publishing a real message response = app.test_client().post( '/light/set', json={ 'friendly_name': 'living_room', 'payload': { 'state': 'ON', 'brightness': 255, 'color': '#0000ff' }, 'feedback': { 'text': 'Okay, ich schalte Licht im Wohnzimmer an' } }) assert response.status_code == 200
def test_value_in_storage_is_not_fetched_from_api(self, MockAPIClass): the_file = { "fileId": "4a551eec-7dac-46d2-8f17-b6972b864b34", "processingStatus": "FINISHED", "fileName": "1a4bae87-1eec-46de-9efb-657be6eaa8fa", "fileLength": 2870700, "mp3Path": "https://s3.amazonaws.com/snackable-test-audio/mp3Audio/1a4bae87-1eec-46de-9efb-657be6eaa8fa_constant_bitrate.mp3", "originalFilePath": "https://s3.amazonaws.com/snackable-test-crawler-audio/1a4bae87-1eec-46de-9efb-657be6eaa8fa.mp3", "seriesTitle": "Royally Obsessed", "segments": [{ "fileSegmentId": 1342, "fileId": "4a551eec-7dac-46d2-8f17-b6972b864b34", "segmentText": "Hey please rise for their majesties of royalty obsessed the podcast for all things modern. Welcome back to royalty obsessed.", "startTime": 1730, "endTime": 16080 }] } self.model(**the_file).save() mock_instance = MockAPIClass.return_value mock_instance.fetch_all.return_value = FILES mock_instance.fetch_details.return_value = FILE_DETAILS[ '4a551eec-7dac-46d2-8f17-b6972b864b34'] mock_instance.fetch_segments.return_value = SEGMENTS[ '4a551eec-7dac-46d2-8f17-b6972b864b34'] with app.test_client() as c: rv = c.get( '/api/presentation/files/4a551eec-7dac-46d2-8f17-b6972b864b34') self.assertEqual(rv.status_code, 200)