def test_convert_aircraft_props(): """Tests for convert_aircraft_props""" ac_props = props.AircraftProperties( aircraft_type="A380", altitude=types.Altitude(18_500), callsign=types.Callsign("TEST"), cleared_flight_level=types.Altitude("FL225"), ground_speed=types.GroundSpeed(23), heading=types.Heading(47), initial_flight_level=types.Altitude(18_500), position=types.LatLon(43.8, 123.4), requested_flight_level=types.Altitude(25_000), route_name=None, vertical_speed=types.VerticalSpeed(32), ) converted = utils.convert_aircraft_props(ac_props) assert isinstance(converted, dict) assert len(converted) == 1 converted_props = converted["TEST"] assert len(converted_props) == 9 assert converted_props["actype"] == "A380" assert converted_props["cleared_fl"] == 22_500 assert converted_props["current_fl"] == 18_500 assert converted_props["gs"] == 23.0 assert converted_props["hdg"] == 47 assert converted_props["lat"] == 43.8 assert converted_props["lon"] == 123.4 assert converted_props["requested_fl"] == 25_000 assert converted_props["vs"] == 32.0
def test_pos_get_single(test_flask_client): """Tests the GET method with a single aircraft""" # Test arg parsing endpoint_str = f"{_ENDPOINT_PATH}?{utils.CALLSIGN_LABEL}" callsign = "" resp = test_flask_client.get(f"{endpoint_str}={callsign}") assert resp.status_code == HTTPStatus.BAD_REQUEST assert utils.CALLSIGN_LABEL in resp.json["message"] with mock.patch(patch_utils_path(_ENDPOINT), wraps=utils) as utils_patch: utils_patch.CALLSIGN_LABEL = utils.CALLSIGN_LABEL sim_proxy_mock = mock.Mock() utils_patch.sim_proxy.return_value = sim_proxy_mock # Test error from simulation properties sim_proxy_mock.simulation.properties = "Error" endpoint_str = f"{endpoint_str}=TEST" resp = test_flask_client.get(endpoint_str) assert resp.status_code == HTTPStatus.INTERNAL_SERVER_ERROR assert resp.data.decode() == "Error" # Test aircraft existence check sim_proxy_mock.simulation.properties = TEST_SIM_PROPS with api.FLASK_APP.test_request_context(): utils_patch.check_exists.return_value = bad_request_resp( "Missing aircraft") resp = test_flask_client.get(endpoint_str) assert resp.status_code == HTTPStatus.BAD_REQUEST assert resp.data.decode() == "Missing aircraft" # Test error from aircraft properties utils_patch.check_exists.return_value = None sim_proxy_mock.aircraft.properties.return_value = "Missing properties" resp = test_flask_client.get(endpoint_str) assert resp.status_code == HTTPStatus.INTERNAL_SERVER_ERROR assert resp.data.decode() == "Missing properties" # Test valid response sim_proxy_mock.aircraft.properties.return_value = TEST_AIRCRAFT_PROPS resp = test_flask_client.get(endpoint_str) assert resp.status_code == HTTPStatus.OK assert resp.json == { **utils.convert_aircraft_props(TEST_AIRCRAFT_PROPS), "scenario_time": TEST_SIM_PROPS.scenario_time, }
def get(): """Logic for GET events. Returns properties for the specified aircraft""" req_args = utils.parse_args(_PARSER) callsign = req_args[utils.CALLSIGN_LABEL] sim_props = utils.sim_proxy().simulation.properties if not isinstance(sim_props, SimProperties): return responses.internal_err_resp(sim_props) if callsign: resp = utils.check_exists(utils.sim_proxy(), callsign) if resp: return resp props = utils.sim_proxy().aircraft.properties(callsign) if not isinstance(props, AircraftProperties): return internal_err_resp(props) data = utils.convert_aircraft_props(props) data.update({"scenario_time": sim_props.scenario_time}) return responses.ok_resp(data) # else: get_all_properties props = utils.sim_proxy().aircraft.all_properties if isinstance(props, str): return responses.internal_err_resp( f"Couldn't get the aircraft properties: {props}") if not props: return responses.bad_request_resp("No aircraft in the simulation") data = {} for prop in props.values(): data.update(utils.convert_aircraft_props(prop)) data["scenario_time"] = sim_props.scenario_time return responses.ok_resp(data)
def test_pos_get_all(test_flask_client): """Tests the GET method with all aircraft""" with mock.patch(patch_utils_path(_ENDPOINT), wraps=utils) as utils_patch: utils_patch.CALLSIGN_LABEL = utils.CALLSIGN_LABEL sim_proxy_mock = mock.Mock() utils_patch.sim_proxy.return_value = sim_proxy_mock # Test error from all_properties sim_proxy_mock.simulation.properties = TEST_SIM_PROPS sim_proxy_mock.aircraft.all_properties = "Error" resp = test_flask_client.get(_ENDPOINT_PATH) assert resp.status_code == HTTPStatus.INTERNAL_SERVER_ERROR assert resp.data.decode( ) == "Couldn't get the aircraft properties: Error" # Test error when no aircraft sim_proxy_mock.aircraft.all_properties = None resp = test_flask_client.get(_ENDPOINT_PATH) assert resp.status_code == HTTPStatus.BAD_REQUEST assert resp.data.decode() == "No aircraft in the simulation" # Test valid response sim_proxy_mock.aircraft.all_properties = { str(TEST_AIRCRAFT_PROPS.callsign): TEST_AIRCRAFT_PROPS, } resp = test_flask_client.get(_ENDPOINT_PATH) assert resp.status_code == HTTPStatus.OK assert resp.json == { **utils.convert_aircraft_props(TEST_AIRCRAFT_PROPS), **{ "scenario_time": TEST_SIM_PROPS.scenario_time }, }