Skip to content

Commit

Permalink
Normalize BCP-47 lang codes to ISO 639-1
Browse files Browse the repository at this point in the history
Add unit tests for lang code handling
  • Loading branch information
NeonDaniel committed Feb 14, 2025
1 parent 4da7624 commit 692d2ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions neon_api_proxy/services/owm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _get_api_response(self, lat: str, lng: str, units: str,
f"is supported", "1.0.0")
api = "onecall"
assert units in ("metric", "imperial", "standard")
lang = lang.split('-')[0] # `de-de` is treated as `en`
query_params = {"lat": lat,
"lon": lng,
"appid": self._api_key,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_owm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
"lng": "-122.2060",
"units": "imperial"}

VALID_QUERY_ES = {"lat": "47.6769",
"lng": "-122.2060",
"units": "imperial",
"lang": "es"}

VALID_QUERY_ES_MX = {"lat": "47.6769",
"lng": "-122.2060",
"units": "imperial",
"lang": "es-mx"}

VALID_QUERY_ONECALL = {"lat": "47.6769",
"lng": "-122.2060",
"units": "imperial",
Expand Down Expand Up @@ -105,6 +115,15 @@ def test_handle_query_valid(self):
self.assertEqual(resp["encoding"], "utf-8")
self.assertIsInstance(json.loads(resp["content"]), dict)

resp_es = self.api.handle_query(**VALID_QUERY_ES)
self.assertEqual(resp_es["status_code"], 200)
self.assertEqual(resp_es["encoding"], "utf-8")
self.assertIsInstance(json.loads(resp_es["content"]), dict)
self.assertNotEqual(resp, resp_es)

resp_es_mx = self.api.handle_query(**VALID_QUERY_ES_MX)
self.assertEqual(resp_es, resp_es_mx)

def test_handle_query_valid_onecall(self):
resp = self.api.handle_query(**VALID_QUERY_ONECALL)
self.assertIsInstance(resp, dict)
Expand Down

0 comments on commit 692d2ef

Please sign in to comment.