Skip to content

Commit

Permalink
Merge pull request #37 from UBC-MDS/feat-add-docstring-to-mock-function
Browse files Browse the repository at this point in the history
feat: add docstring to mocked_requests_get_pollution function
  • Loading branch information
mel-liow authored Jan 22, 2022
2 parents 3e0aa3b + 538d241 commit bc7e36c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 21 deletions.
40 changes: 21 additions & 19 deletions src/airpyllution/airpyllution.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,17 @@ def get_pollution_history(start_date, end_date, lat, lon, api_key):
"appid": api_key,
}

response = requests.get(url=url, params=params)
response_obj = response.json()

try:
data = convert_data_to_pandas(response_obj)

return data
response = requests.get(url=url, params=params)
response_obj = response.json()

try:
data = convert_data_to_pandas(response_obj)
return data
except:
if "cod" in response_obj:
return response_obj["message"]
except:
if "cod" in response_obj:
return response_obj["message"]

return "An error occurred requesting data from the API"


Expand Down Expand Up @@ -146,13 +145,13 @@ def get_air_pollution(lat, lon, api_key, fig_title=""):
try:
response = requests.get(url=url, params=params)
response_obj = response.json()
data = convert_data_to_pandas(response_obj)
try:
data = convert_data_to_pandas(response_obj)
except:
if "cod" in response_obj:
return response_obj["message"]
except:
response = requests.get(url=url, params=params)
response_obj = response.json()
if "cod" in response_obj:
return response_obj["message"]
return "An error occurred requesting data from API"
return "An error occurred requesting data from the API"

data = data.melt(
id_vars=["lon", "lat"],
Expand Down Expand Up @@ -242,12 +241,15 @@ def get_pollution_forecast(lat, lon, api_key):

try:
response = requests.get(url=url, params=params)
print(f'RESPONSE', response)
response_obj = response.json()
data = convert_data_to_pandas(response_obj)
try:
data = convert_data_to_pandas(response_obj)
except:
if "cod" in response_obj:
return response_obj["message"]
except:
if "cod" in response_obj:
return response_obj["message"]
return "An error occurred requesting data from API"
return "An error occurred requesting data from the API"

if len(data) >= 1:
try:
Expand Down
10 changes: 10 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,13 @@
"end": 3.14159,
"appid": "invalid_api_key",
}

mock_error_params = {
"lat": "latitude_val",
"lon": "longitude_val",
"lat_oor": -100.0,
"lon_oor": 181.0,
"start": 1234.567,
"end": 3.14159,
"appid": "api_error",
}
46 changes: 44 additions & 2 deletions tests/test_airpyllution.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@


def mocked_requests_get_pollution(*args, **kwargs):
"""
Function for mocking a Response object.
This intercepts any API calls when called within the test suite and returns
an instance of a MockResponse object. This should be used with the @patch decorator.
"""
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
Expand All @@ -21,13 +26,19 @@ def json(self):

if kwargs["params"]["appid"] == "invalid_api_key":
return MockResponse(mock_api_invalid_key_error, 404)

if kwargs["params"]["appid"] == "api_error":
return "ERROR"

return MockResponse(mock_history_data, 200)

elif kwargs["url"] == "http://api.openweathermap.org/data/2.5/air_pollution":

if kwargs["params"]["appid"] == "invalid_api_key":
return MockResponse(mock_api_invalid_key_error, 404)

if kwargs["params"]["appid"] == "api_error":
return "ERROR"

return MockResponse(mock_pollution_data, 200)

Expand All @@ -37,7 +48,9 @@ def json(self):

if kwargs["params"]["appid"] == "invalid_api_key":
return MockResponse(mock_api_invalid_key_error, 404)


if kwargs["params"]["appid"] == "api_error":
return "ERROR"
return MockResponse(mock_forecast_data, 200)

return MockResponse(
Expand Down Expand Up @@ -101,7 +114,19 @@ def test_pollution_history(mock_api_call):
== "Longitude input should be a float"
)

# Invalid API key
# API error
assert (
airpyllution.get_pollution_history(
mock_params["start"],
mock_params["end"],
mock_params["lat"],
mock_params["lon"],
mock_error_params["appid"],
)
== "An error occurred requesting data from the API"
)

# Invalid API key, tests nested try-except
assert (
airpyllution.get_pollution_history(
mock_params["start"],
Expand Down Expand Up @@ -163,6 +188,16 @@ def test_air_pollution(mock_api_call):
== "Enter valid longitude values (Range should be -180<Longitude<180)"
)

# API error
assert (
airpyllution.get_air_pollution(
mock_params["lat"],
mock_params["lon"],
mock_error_params["appid"],
)
== "An error occurred requesting data from the API"
)

assert (
airpyllution.get_air_pollution(
mock_params["lat"], mock_params["lon"], mock_incorrect_params["appid"]
Expand Down Expand Up @@ -234,6 +269,13 @@ def test_pollution_forecast(mock_api_call):
== "Enter valid longitude values (Range should be -180<Longitude<180)"
)

assert (
airpyllution.get_pollution_forecast(
mock_params["lat"], mock_params["lon"], mock_error_params["appid"],
)
== "An error occurred requesting data from the API"
)

assert (
airpyllution.get_pollution_forecast(
mock_params["lat"], mock_params["lon"], mock_incorrect_params["appid"]
Expand Down

0 comments on commit bc7e36c

Please sign in to comment.