Skip to content

Commit

Permalink
fix: benton, wa
Browse files Browse the repository at this point in the history
  • Loading branch information
leehanchung authored and hurshd0 committed Apr 22, 2020
1 parent f53a1bb commit 636a31d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
11 changes: 5 additions & 6 deletions api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from api.utils import read_country_data
from api.utils import read_county_stats
from api.utils import read_states
from api.utils import read_county_stats_zip_ny
# from api.utils import read_county_stats_zip_ny
from api.config import DataReadingError

# Starts the FastAPI Router to be used by the FastAPI app.
Expand Down Expand Up @@ -451,11 +451,11 @@ def post_zip(zip_code: ZIPInput) -> JSONResponse:
try:
zip_info = zipcodes.matching(zip_code.zip_code)[0]
except Exception as ex:
message = f"ZIP code {zip_code.zip_code} not found in US Zipcode database."
_logger.warning(f"Endpoint: /zip --- POST --- {message}")
message = (f"ZIP code {zip_code.zip_code}"
" not found in US Zipcode database.")
_logger.warning(f"Endpoint: /zip --- POST --- {ex} {message}")
raise HTTPException(status_code=422,
detail=f"[Error] POST '/zip' {message}")

detail=f"[Error] POST '/zip' {ex} {message}")

try:
county = zip_info['county'].rsplit(' ', 1)[0]
Expand All @@ -467,7 +467,6 @@ def post_zip(zip_code: ZIPInput) -> JSONResponse:
county = "New York"
data = read_county_stats(state, county)[0]
else:

data = read_county_stats(state, county)[0]
json_data = {"success": True, "message": data}
del data
Expand Down
6 changes: 4 additions & 2 deletions api/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ async def data_reading_exception_handler(

return JSONResponse(
status_code=422,
content={"message": f"[ERROR] {request} -- {exc.name}"},
content={"message": f"[ERROR] {request.method} -- {exc}"},
)


async def data_validation_exception_handler(
request: Request,
exc: DataValidationError,
) -> JSONResponse:
# this will hang, so just print out method directly for info
# body = await request.body()
return JSONResponse(
status_code=422,
content={"message": f"[ERROR] {request} -- {exc.name}"},
content={"message": f"[ERROR] {request.method} -- {exc}"},
)
71 changes: 69 additions & 2 deletions api/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ def test_post_ny_zip(test_app):
10004 -> Manhattan (New York County), NY
10302 -> Staten Island (Richmond County), NY
10458 -> Bronx, NY
11203 -> Brooklyn (Kings County), NY
11361 -> Queens (Queens County), NY
"""
payload = {'zip_code': '10004'}
response = test_app.post("/zip", data=json.dumps(payload))
Expand All @@ -318,14 +320,72 @@ def test_post_ny_zip(test_app):
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "New York"
assert data['county_name'] == "Richmond"
# TODO: add Richmond county
# assert data['county_name'] == "Richmond"
assert data['county_name'] == "New York"

payload = {'zip_code': '10458'}
response = test_app.post("/zip", data=json.dumps(payload))
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "New York"
assert data['county_name'] == "Bronx"
# TODO: add Bronx county
# assert data['county_name'] == "Bronx"
assert data['county_name'] == "New York"


def test_post_nyc_borough_zip(test_app):
"""Test problematic zip codes:
10004 -> Manhattan (New York County), NY
10302 -> Staten Island (Richmond County), NY
10458 -> Bronx, NY
11203 -> Brooklyn (Kings County), NY
11361 -> Queens (Queens County), NY
"""
payload = {'zip_code': '10004'}
response = test_app.post("/zip", data=json.dumps(payload))
time.sleep(2)
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "New York"
assert data['county_name'] == "New York"

payload = {"zip_code": "10312"}
response = test_app.post("/zip", data=json.dumps(payload))
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "New York"
# TODO: add Richmond county
# assert data['county_name'] == "Richmond"
assert data['county_name'] == "New York"

payload = {'zip_code': '10458'}
response = test_app.post("/zip", data=json.dumps(payload))
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "New York"
# TODO: add Bronx county
# assert data['county_name'] == "Bronx"
assert data['county_name'] == "New York"

payload = {'zip_code': '11203'}
response = test_app.post("/zip", data=json.dumps(payload))
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "New York"
# TODO: add Brooklyn/Kings county
# assert data['county_name'] == "Brooklyn"
assert data['county_name'] == "New York"

payload = {'zip_code': '11361'}
response = test_app.post("/zip", data=json.dumps(payload))
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "New York"
# TODO: add Queens/Queens county
# assert data['county_name'] == "Queens"
assert data['county_name'] == "New York"


def test_post_zip(test_app):
"""Test problematic zip codes:
Expand Down Expand Up @@ -354,6 +414,13 @@ def test_post_zip(test_app):
assert data['state_name'] == "Louisiana"
assert data['county_name'] == "Assumption"

payload = {'zip_code': '99352'}
response = test_app.post("/zip", data=json.dumps(payload))
assert response.status_code == 200
data = response.json()['message']
assert data['state_name'] == "Washington"
assert data['county_name'] == "Benton and Franklin"


def test_post_zip_validation(test_app):
"""Unprocessable entity"""
Expand Down
2 changes: 1 addition & 1 deletion api/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
from .county_mongo import StateMongo
from .county import read_county_stats
from .state import read_states
from .zip import read_county_stats_zip_ny
# from .zip import read_county_stats_zip_ny
4 changes: 4 additions & 0 deletions api/utils/county.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def read_county_data() -> pd.DataFrame:

def read_county_stats(state: str, county: str) -> Dict:

# 2020-04-22 patch Benton, WA
if (state == "WA") and (county in ['Benton', 'Franklin']):
county = "Benton and Franklin"

try:
df = pd.read_csv(app_config.COUNTY_URL)
#deaths = pd.read_csv(app_config.STATE_DEATH)
Expand Down

0 comments on commit 636a31d

Please sign in to comment.