Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make get_multiple_* methods pass tests by not halting for one bad input #128

Merged
merged 3 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/ozone/ozone.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,18 @@ def get_multiple_coordinate_air(
selected another data format, this dataframe will be empty)
"""
for loc in locations:
# This just makes sure that it's always a returns a pandas.DataFrame.
# Makes mypy happy.
df = pandas.DataFrame(
self.get_coordinate_air(loc[0], loc[1], df=df, params=params)
)
try:
# This just makes sure that it's always a returns a pandas.DataFrame.
# Makes mypy happy.
df = pandas.DataFrame(
self.get_coordinate_air(loc[0], loc[1], df=df, params=params)
)
except Exception:
# NOTE: If we have custom exception we can catch it instead.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm overall I do think we should have custom exceptions.

However, it's a good idea to add custom exceptions for the entirety of Ozone in one go (I'm sure there are other sections that need them). This way we can come up with a consistent set of exceptions for the package.

empty_row = pandas.DataFrame(
{"latitude": [_as_float(loc[0])], "longitude": [_as_float(loc[1])]}
)
df = pandas.concat([df, empty_row], ignore_index=True)

df.reset_index(inplace=True, drop=True)
return self._format_output(data_format, df)
Expand Down Expand Up @@ -551,9 +558,16 @@ def get_multiple_city_air(
selected another data format, this dataframe will be empty)
"""
for city in cities:
# This just makes sure that it's always a returns a pandas.DataFrame.
# Makes mypy happy.
df = pandas.DataFrame(self.get_city_air(city=city, df=df, params=params))
try:
# This just makes sure that it's always a returns a pandas.DataFrame.
# Makes mypy happy.
df = pandas.DataFrame(
self.get_city_air(city=city, df=df, params=params)
)
except Exception:
# NOTE: If we have custom exception we can catch it instead.
empty_row = pandas.DataFrame({"city": [city]})
df = pandas.concat([df, empty_row], ignore_index=True)

df.reset_index(inplace=True, drop=True)
return self._format_output(data_format, df)
Expand Down
74 changes: 70 additions & 4 deletions tests/cassettes/test_get_multiple_city_air/test_bad_city.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ interactions:
- Fri, 22 Apr 2022 01:26:13 GMT
Server:
- nginx
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding
X-Gen-Time:
Expand Down Expand Up @@ -134,8 +132,6 @@ interactions:
- Fri, 22 Apr 2022 01:26:15 GMT
Server:
- nginx
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding
X-Gen-Time:
Expand All @@ -147,4 +143,74 @@ interactions:
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.27.1
method: GET
uri: https://api.waqi.info/feed//a%20definitely%20nonexistent%20city/?token=DUMMY_TOKEN
response:
body:
string: ''
headers:
Connection:
- keep-alive
Content-Length:
- '0'
Date:
- Sat, 23 Apr 2022 14:41:19 GMT
Location:
- /feed/a%20definitely%20nonexistent%20city/?token=DUMMY_TOKEN
Server:
- nginx
status:
code: 301
message: Moved Permanently
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- python-requests/2.27.1
method: GET
uri: https://api.waqi.info/feed/a%20definitely%20nonexistent%20city/?token=DUMMY_TOKEN
response:
body:
string: '{"status":"error","data":"Unknown station"}'
headers:
Access-Control-Allow-Origin:
- '*'
Connection:
- keep-alive
Content-Type:
- application/json; charset=UTF-8
Date:
- Sat, 23 Apr 2022 14:41:19 GMT
Server:
- nginx
Transfer-Encoding:
- chunked
Vary:
- Accept-Encoding
X-Gen-Time:
- "188.033\xC2\xB5s"
X-Powered-By:
- rxstreamer-waqi/1.3
content-length:
- '43'
status:
code: 200
message: OK
version: 1
Loading