From 19e19d01e1b404a49c57729393bdd262f7c18b76 Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Mon, 2 Jun 2025 17:09:23 -0700 Subject: [PATCH 1/8] src/api.default_location try except, tests/test_api.test_default_location timeout --- src/api.py | 23 +++++++++++++---------- tests/test_api.py | 6 ++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/api.py b/src/api.py index 955f80d..5727bb4 100644 --- a/src/api.py +++ b/src/api.py @@ -50,16 +50,19 @@ def default_location(): If no location specified in cli, find user's location Make a GET request to the API endpoint """ - response = requests.get("https://ipinfo.io/json", timeout=10) - - if response.status_code == HTTPStatus.OK: - data = response.json() - location = data["loc"].split(",") - lat = location[0] - long = location[1] - city = data["city"] - return [lat, long, city] - return "No data" + try: + response = requests.get("https://ipinfo.io/json", timeout=10) + + if response.status_code == HTTPStatus.OK: + data = response.json() + location = data["loc"].split(",") + lat = location[0] + long = location[1] + city = data["city"] + return [lat, long, city] + return "No data" + except requests.exceptions.Timeout: + return "No data" def get_uv(lat, long, decimal, unit="imperial"): diff --git a/tests/test_api.py b/tests/test_api.py index b63d842..7c737f8 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -9,6 +9,7 @@ import pytest from openmeteo_requests.Client import OpenMeteoRequestsError +from requests.exceptions import Timeout from src.api import ( default_location, @@ -33,6 +34,7 @@ ["43.03", "-72.001", "New York"], ), (HTTPStatus.BAD_REQUEST, {}, "No data"), + (Timeout, {}, "No data"), ], ) def test_default_location_mocked( @@ -46,6 +48,10 @@ def test_default_location_mocked( # Mock the 'requests.get' method mock_requests = mocker.patch("requests.get", return_value=mock_response) + # Check for Timeout and set side effect + if status_code == Timeout: + mock_requests.side_effect = Timeout("Test Timeout") + # Act: Call the function result = default_location() From 1d7e92b16fb7b81384ea0d09f71b746e44fb8a96 Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Mon, 2 Jun 2025 17:11:41 -0700 Subject: [PATCH 2/8] src/api raise_for_status and minimize try except --- src/api.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/api.py b/src/api.py index 5727bb4..b80ac22 100644 --- a/src/api.py +++ b/src/api.py @@ -52,18 +52,19 @@ def default_location(): """ try: response = requests.get("https://ipinfo.io/json", timeout=10) - - if response.status_code == HTTPStatus.OK: - data = response.json() - location = data["loc"].split(",") - lat = location[0] - long = location[1] - city = data["city"] - return [lat, long, city] - return "No data" + response.raise_for_status() except requests.exceptions.Timeout: return "No data" + if response.status_code == HTTPStatus.OK: + data = response.json() + location = data["loc"].split(",") + lat = location[0] + long = location[1] + city = data["city"] + return [lat, long, city] + return "No data" + def get_uv(lat, long, decimal, unit="imperial"): """ From 22e38dc17dc35cda9c713ab1db9f3208f50ea44c Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Tue, 3 Jun 2025 17:34:49 -0700 Subject: [PATCH 3/8] RequestException block and exception parametrized --- src/api.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api.py b/src/api.py index b80ac22..b670b51 100644 --- a/src/api.py +++ b/src/api.py @@ -55,6 +55,8 @@ def default_location(): response.raise_for_status() except requests.exceptions.Timeout: return "No data" + except requests.exceptions.RequestException: + return "No data" if response.status_code == HTTPStatus.OK: data = response.json() From 132e0cf5fbfecbac6d8e5eec983d85ea8aef1750 Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Tue, 3 Jun 2025 17:36:48 -0700 Subject: [PATCH 4/8] change constant name --- tests/test_api.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 7c737f8..26fa73f 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -24,17 +24,20 @@ ) from src.helper import arguments_dictionary +HTTP_TIMEOUT = 999 + @pytest.mark.parametrize( - ("status_code", "json_data", "expected_result"), + ("status_code", "json_data", "expected_result", "exception"), [ ( HTTPStatus.OK, {"loc": "43.03,-72.001", "city": "New York"}, ["43.03", "-72.001", "New York"], + None, ), - (HTTPStatus.BAD_REQUEST, {}, "No data"), - (Timeout, {}, "No data"), + (HTTPStatus.BAD_REQUEST, {}, "No data", None), + (HTTP_TIMEOUT, {}, "No data", Timeout("Test Timeout")), ], ) def test_default_location_mocked( @@ -48,9 +51,8 @@ def test_default_location_mocked( # Mock the 'requests.get' method mock_requests = mocker.patch("requests.get", return_value=mock_response) - # Check for Timeout and set side effect - if status_code == Timeout: - mock_requests.side_effect = Timeout("Test Timeout") + # Set side effect + mock_requests.side_effect = Timeout("Test Timeout") # Act: Call the function result = default_location() From 088e676691032d11af119b0424bb49b46135d1ee Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Tue, 3 Jun 2025 18:03:42 -0700 Subject: [PATCH 5/8] side_effect parameter --- tests/test_api.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 26fa73f..352269d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -28,7 +28,7 @@ @pytest.mark.parametrize( - ("status_code", "json_data", "expected_result", "exception"), + ("status_code", "json_data", "expected_result", "side_effect"), [ ( HTTPStatus.OK, @@ -36,12 +36,12 @@ ["43.03", "-72.001", "New York"], None, ), - (HTTPStatus.BAD_REQUEST, {}, "No data", None), - (HTTP_TIMEOUT, {}, "No data", Timeout("Test Timeout")), + (HTTPStatus.BAD_REQUEST, {}, "No data", None,), + (HTTP_TIMEOUT, {}, "No data", Timeout("Test Timeout"),), ], ) def test_default_location_mocked( - mocker, status_code, json_data, expected_result + mocker, status_code, json_data, expected_result, side_effect ): # Arrange: Mock the response from the API mock_response = Mock() @@ -52,7 +52,7 @@ def test_default_location_mocked( mock_requests = mocker.patch("requests.get", return_value=mock_response) # Set side effect - mock_requests.side_effect = Timeout("Test Timeout") + mock_requests.side_effect = side_effect # Act: Call the function result = default_location() From 0ca67083f8e55b9c13152561a9c05290204c3a78 Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Tue, 3 Jun 2025 18:06:38 -0700 Subject: [PATCH 6/8] lint fix --- tests/test_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 352269d..e04c595 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -36,8 +36,8 @@ ["43.03", "-72.001", "New York"], None, ), - (HTTPStatus.BAD_REQUEST, {}, "No data", None,), - (HTTP_TIMEOUT, {}, "No data", Timeout("Test Timeout"),), + (HTTPStatus.BAD_REQUEST, {}, "No data", None), + (HTTP_TIMEOUT, {}, "No data", Timeout("Test Timeout")), ], ) def test_default_location_mocked( From c4364debc2448ca2336025e799b308c9ce927e80 Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Sat, 28 Jun 2025 12:48:43 -0700 Subject: [PATCH 7/8] remove raise_for_status from src/api.py --- src/api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api.py b/src/api.py index b670b51..34183a4 100644 --- a/src/api.py +++ b/src/api.py @@ -52,7 +52,6 @@ def default_location(): """ try: response = requests.get("https://ipinfo.io/json", timeout=10) - response.raise_for_status() except requests.exceptions.Timeout: return "No data" except requests.exceptions.RequestException: From 27900298856188004392b6e716b41a6d23277b52 Mon Sep 17 00:00:00 2001 From: christe6-osu Date: Sat, 28 Jun 2025 12:54:47 -0700 Subject: [PATCH 8/8] RequestException test --- tests/test_api.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index e04c595..85d6132 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -9,7 +9,7 @@ import pytest from openmeteo_requests.Client import OpenMeteoRequestsError -from requests.exceptions import Timeout +from requests.exceptions import RequestException, Timeout from src.api import ( default_location, @@ -25,6 +25,7 @@ from src.helper import arguments_dictionary HTTP_TIMEOUT = 999 +HTTP_REQUEST_EXCEPTION = 998 @pytest.mark.parametrize( @@ -38,6 +39,12 @@ ), (HTTPStatus.BAD_REQUEST, {}, "No data", None), (HTTP_TIMEOUT, {}, "No data", Timeout("Test Timeout")), + ( + HTTP_REQUEST_EXCEPTION, + {}, + "No data", + RequestException("Test RequestException"), + ), ], ) def test_default_location_mocked(