def test_host_can_be_overridden_via_environment_variable(overridden_host: str): connection = Connection() with patch.dict("os.environ", {"SIGNAL_OCEAN_API_HOST": overridden_host}, clear=True), patch("requests.get") as get: connection._make_get_request(None) assert get.call_args.args == ("host/", )
def test_host_can_be_overridden_via_parameter(overridden_host: str): connection = Connection(api_host=overridden_host) with patch.dict("os.environ", {}, clear=True), patch("requests.get") as get: connection._make_get_request(None) assert get.call_args.args == ("host/", )
def test_targets_production_host_by_default(): connection = Connection() with patch.dict("os.environ", {}, clear=True), patch("requests.get") as get: connection._make_get_request(None) assert get.call_args.args == ("https://api-gateway.signalocean.com/", )
def test_has_no_api_key_if_not_provided_and_no_environment_variable(): connection = Connection() with patch.dict("os.environ", {}, clear=True), patch("requests.get") as get: connection._make_get_request(None) assert get.call_args.kwargs["headers"]["Api-Key"] is None
def test_uses_provided_api_key(): provided_key = "provided key" connection = Connection(api_key=provided_key) with patch.dict("os.environ", clear=True), patch("requests.get") as get: connection._make_get_request(None) assert get.call_args.kwargs["headers"]["Api-Key"] == provided_key
def test_uses_api_key_from_environment_variables_by_default(): key_from_env_var = "key from env var" connection = Connection() with patch.dict("os.environ", {"SIGNAL_OCEAN_API_KEY": key_from_env_var}, clear=True), patch("requests.get") as get: connection._make_get_request(None) assert get.call_args.kwargs["headers"]["Api-Key"] == key_from_env_var
def test_makes_get_requests_with_specified_parameters(): connection = Connection("api_key", "api_host") query_string = {"key": "value"} with patch("requests.get") as get: connection._make_get_request("relative_url", query_string) get.assert_called_with( "api_host/relative_url", params=query_string, headers={ "Api-Key": "api_key", "Content-Type": "application/json" }, )
def get_multiple(connection: Connection, relative_url: str, cls: Type[TModel], query_string: Optional[QueryString] = None, rename_keys: Optional[Dict[str, str]] = None) \ -> Tuple[TModel, ...]: """Get a multiple objects from the API. Make a get request to the specified URL to retrieve a sequence of results and return a list of objects of the provided class instantiated with the retrieved data. If the API responds with an empty sequence an empty list is returned. Args: connection: The connection object to use to make the appropriate get request to the API. relative_url: The relative URL to make the request to. cls: The class to instantiate the object for the retrieved data. query_string: Query parameters for the request. rename_keys: Key names to rename to match model attribute names, used when an automated translation of the name from CapsWords to snake_case is to sufficient. Renaming must provide the name in CapsWords. """ response = connection._make_get_request(relative_url, query_string=query_string) response.raise_for_status() data = response.json() return tuple(parse_model(d, cls, rename_keys=rename_keys) for d in data)
def get_single(connection: Connection, relative_url: str, cls: Type[TModel], query_string: Optional[QueryString] = None, rename_keys: Optional[Dict[str, str]] = None) \ -> Optional[TModel]: """Get a single object from the API. Make a get request to the specified URL and return an object of the provided class instantiated with the retrieved data. If the API responds with a "Not Found" status code, return None. Args: connection: The connection object to use to make the appropriate get request to the API. relative_url: The relative URL to make the request to. cls: The class to instantiate the object for the retrieved data. query_string: Query parameters for the request. rename_keys: Key names to rename to match model attribute names, used when an automated translation of the name from CapsWords to snake_case is to sufficient. Renaming must provide the name in CapsWords. Returns: An object of the provided class instantiated with the data retrieved from the specified URL, or None if the API responds with a "Not Found" status code. """ response = connection._make_get_request(relative_url, query_string=query_string) if response.status_code == requests.codes.not_found: return None response.raise_for_status() data = response.json() return parse_model(data, cls, rename_keys=rename_keys)
def create_vessels_api(response: requests.Response) \ -> Tuple[VesselsAPI, MagicMock]: connection = Connection('', '') mocked_make_request = MagicMock(return_value=response) connection._make_get_request = mocked_make_request api = VesselsAPI(connection) return api, mocked_make_request
def create_voyages_api(response_data: Union[Dict, List] = None) \ -> Tuple[VoyagesAPI, MagicMock]: connection = Connection('', '') response = MagicMock() response.json.return_value = response_data mocked_make_request = MagicMock(return_value=response) connection._make_get_request = mocked_make_request api = VoyagesAPI(connection) return api, mocked_make_request
def create_sf_api( response_data: List = None, ) -> Tuple[ScrapedFixturesAPI, MagicMock]: connection = Connection("", "") response = MagicMock() response.json.return_value = response_data mocked_make_request = MagicMock(return_value=response) connection._make_get_request = mocked_make_request api = ScrapedFixturesAPI(connection, max_pages=2) return api, mocked_make_request
def create_voyages_api_multiple_requests( responses_data: List[Union[Dict, List]]) \ -> Tuple[VoyagesAPI, MagicMock]: connection = Connection('', '') response = MagicMock() response.json = MagicMock(side_effect=responses_data) mocked_make_request = MagicMock(return_value=response) connection._make_get_request = mocked_make_request api = VoyagesAPI(connection) return api, mocked_make_request
def test_get_vessels(): connection = Connection('', '') response = MagicMock() response.json.return_value = __mock_vessels_response mocked_make_request = MagicMock(return_value=response) connection._make_get_request = mocked_make_request api = VoyagesAPI(connection) vessels = api.get_imos(VesselFilter("Mit")) assert [v.imo for v in vessels ] == [vessel.imo for vessel in __mock_vessels_response_model]
def create_mock_connection(response_data: Union[Dict, Tuple[Dict, ...], None]) \ -> Tuple[Connection, MagicMock]: connection = Connection('', '') response = MagicMock() if response_data is None: response.status_code = requests.codes.not_found response.json.return_value = response_data mocked_make_request = MagicMock(return_value=response) connection._make_get_request = mocked_make_request return connection, mocked_make_request
def test_get_vessel_classes(): connection = Connection('', '') response = MagicMock() response.json.return_value = __mock_vessel_classes_response mocked_make_request = MagicMock(return_value=response) connection._make_get_request = mocked_make_request api = VoyagesAPI(connection) vessel_classes = api.get_vessel_classes() assert [v.vessel_class_id for v in vessel_classes] == [ vessel_class.vessel_class_id for vessel_class in __mock_vessel_classes_response_model ]