Skip to content

Commit 9d4a7a5

Browse files
CIW8981Arun Raigithub-actions[bot]
authored
refactor: remove unittest dependency and start using pytest (#5)
* refactor: remove unittest dependency and start using pytest * code format * ci(github-actions): update coverage badge --------- Co-authored-by: Arun Rai <ciw8981@bb-usmia-v019-l-s-nafd-dscn01.krft.net> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 4c279cc commit 9d4a7a5

File tree

5 files changed

+89
-47
lines changed

5 files changed

+89
-47
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pytest-cov = "*"
1414
pre-commit = "*"
1515
isort = "*"
1616
commitizen = "*"
17+
pytest-mock = "*"
1718

1819
[requires]
1920
python_version = "3.10"

Pipfile.lock

Lines changed: 51 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example for Setting Up Pytest and Pipenv
22

3-
![Coverage Badge](https://img.shields.io/badge/coverage-95%25-brightgreen)
3+
![Coverage Badge](https://img.shields.io/badge/coverage-96%25-brightgreen)
44
![Python Version](https://img.shields.io/badge/Python-3.10-blue)
55
![License](https://img.shields.io/badge/License-MIT-green)
66

src/weather_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22

33
import requests
4+
import json
45
from requests.exceptions import RequestException
56

67

@@ -41,5 +42,5 @@ def get_weather(city: str, api_key: Optional[str] = None) -> Optional[float]:
4142
if temperature is None:
4243
raise WeatherServiceError("Temperature data is missing in the response")
4344
return temperature
44-
except ValueError as e:
45+
except AttributeError as e:
4546
raise WeatherServiceError("Failed to parse weather data") from e

tests/test_weather_service.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,92 @@
1-
from unittest.mock import Mock, patch
2-
31
import pytest # type: ignore
42
from requests.exceptions import RequestException
53

64
from src.weather_service import WeatherServiceError, get_weather
75

86

9-
@patch("src.weather_service.requests.get")
10-
def test_get_weather_success(mock_get):
7+
def test_get_weather_success(mocker):
118
"""
129
Test that get_weather returns the correct temperature when the API call succeeds.
1310
"""
14-
mock_response = Mock()
15-
mock_response.status_code = 200
16-
mock_response.json.return_value = {"temperature": 22}
17-
mock_get.return_value = mock_response
11+
mocker.patch(
12+
"requests.get",
13+
return_value=mocker.MagicMock(
14+
status_code=200, json=lambda: {"temperature": 22}
15+
),
16+
)
1817

1918
result = get_weather("New York", api_key="test_api_key")
2019
assert result == 22
2120

2221

23-
@patch("src.weather_service.requests.get")
24-
def test_get_weather_none_temperature(mock_get):
22+
def test_get_weather_none_temperature(mocker):
2523
"""
2624
Test that get_weather raises WeatherServiceError if the temperature is None.
2725
"""
28-
mock_response = Mock()
29-
mock_response.status_code = 200
30-
mock_response.json.return_value = {"temperature": None}
31-
mock_get.return_value = mock_response
26+
mocker.patch(
27+
"requests.get",
28+
return_value=mocker.MagicMock(
29+
status_code=200, json=lambda: {"temperature": None}
30+
),
31+
)
3232

3333
with pytest.raises(
3434
WeatherServiceError, match="Temperature data is missing in the response"
3535
):
3636
get_weather("New York", api_key="test_api_key")
3737

3838

39-
@patch("src.weather_service.requests.get")
40-
def test_get_weather_failure(mock_get):
39+
def test_get_weather_failure(mocker):
4140
"""
4241
Test that get_weather raises WeatherServiceError when the API call fails.
4342
"""
44-
mock_response = Mock()
45-
mock_response.status_code = 404
46-
mock_get.return_value = mock_response
43+
44+
mocker.patch(
45+
"requests.get",
46+
return_value=mocker.MagicMock(
47+
status_code=404, json=lambda: {"temperature": None}
48+
),
49+
)
4750

4851
with pytest.raises(
4952
WeatherServiceError, match="Failed to fetch weather data for NonExistentCity"
5053
):
5154
get_weather("NonExistentCity", api_key="test_api_key")
5255

5356

54-
@patch("src.weather_service.requests.get")
55-
def test_get_weather_missing_temperature(mock_get):
57+
def test_get_weather_missing_temperature(mocker):
5658
"""
5759
Test that get_weather raises WeatherServiceError if temperature is missing in the response.
5860
"""
59-
mock_response = Mock()
60-
mock_response.status_code = 200
61-
mock_response.json.return_value = {}
62-
mock_get.return_value = mock_response
6361

62+
mocker.patch(
63+
"requests.get",
64+
return_value=mocker.MagicMock(status_code=200, json=lambda: {}),
65+
)
6466
with pytest.raises(
6567
WeatherServiceError, match="Temperature data is missing in the response"
6668
):
6769
get_weather("New York", api_key="test_api_key")
6870

6971

70-
@patch("src.weather_service.requests.get")
71-
def test_get_weather_json_parsing_error(mock_get):
72+
def test_get_weather_json_parsing_error(mocker):
7273
"""
7374
Test that get_weather raises WeatherServiceError if JSON parsing fails.
7475
"""
75-
mock_response = Mock()
76-
mock_response.status_code = 200
77-
mock_response.json.side_effect = ValueError
78-
mock_get.return_value = mock_response
76+
mocker.patch(
77+
"requests.get",
78+
return_value=mocker.MagicMock(status_code=200, json=lambda: "Invalid Json"),
79+
)
7980

8081
with pytest.raises(WeatherServiceError, match="Failed to parse weather data"):
8182
get_weather("New York", api_key="test_api_key")
8283

8384

84-
@patch("src.weather_service.requests.get")
85-
def test_get_weather_request_exception(mock_get):
85+
def test_get_weather_request_exception(mocker):
8686
"""
8787
Test that get_weather raises WeatherServiceError if a request exception occurs.
8888
"""
89+
mock_get = mocker.patch("requests.get")
8990
mock_get.side_effect = RequestException("Network error")
9091

9192
with pytest.raises(

0 commit comments

Comments
 (0)