-
Notifications
You must be signed in to change notification settings - Fork 0
Feature unittests #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,11 @@ jobs: | |
# Run our linting | ||
- name: Lint code | ||
run: | | ||
./bin/task lint | ||
./bin/task lint | ||
|
||
# Testing | ||
- name: Test code | ||
env: | ||
TNT_EX1_OPENWEATHERMAP_API_KEY: ${{ secrets.TNT_EX1_OPENWEATHERMAP_API_KEY}} | ||
run: | ||
./bin/task test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a newline at the end of the file since some tools don't execute the last line if it ends with EOF (end of file) instead of a newline |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,13 @@ tasks: | |
run: | ||
desc: Run the Weather Data Fetcher | ||
cmds: | ||
- poetry run python weather_data_fetcher.py | ||
- poetry run python weather_data_fetcher.py | ||
|
||
test: | ||
desc: Runs tests on the code | ||
cmds: | ||
- > | ||
poetry run pytest | ||
-s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pleas write out command line options if you can. I have no fricking clue what |
||
--cov=. | ||
--cov-report=html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another newline 👀 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import dotenv | ||
from unittest import mock | ||
|
||
from weather_data_fetcher import fetch_location_coords, fetch_weather_data, get_api_key | ||
|
||
|
||
class TestWeatherDataFetcher: | ||
def test_fetch_coords(self): | ||
coords_berlin = fetch_location_coords("Berlin") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot to mock the real API call underneath. Please do that otherwise I cannot run this test without internet. |
||
coords_difference = [sum(x) for x in zip(coords_berlin, (-52.520008, -13.404954))] | ||
assert sum(list(coords_difference)) < 0.01 | ||
Comment on lines
+10
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting way to test this 😅 is there maybe an easier and more transparent way to possible test this? Like checking if something equals something without summing up and other tricks? |
||
|
||
@mock.patch("weather_data_fetcher.fetch_new_data", autospec=True) | ||
def test_fetch_data(self, request_mock): | ||
request_mock.return_value = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can make your life easier here and just put into the json payload what you extract in the end. This makes everything much much shorter. |
||
"cod": "200", | ||
"message": 0, | ||
"cnt": 40, | ||
"list": [ | ||
{ | ||
"dt": 1665824400, | ||
"main": { | ||
"temp": 12.95, | ||
"feels_like": 12.76, | ||
"temp_min": 12.95, | ||
"temp_max": 13.93, | ||
"pressure": 1000, | ||
"sea_level": 1000, | ||
"grnd_level": 1004, | ||
"humidity": 94, | ||
"temp_kf": -0.98, | ||
}, | ||
"weather": [ | ||
{"id": 803, "main": "Clouds", "description": "broken clouds", "icon": "04d"} | ||
], | ||
"clouds": {"all": 81}, | ||
"wind": {"speed": 1.55, "deg": 215, "gust": 3.1}, | ||
"visibility": 10000, | ||
"pop": 0, | ||
"sys": {"pod": "d"}, | ||
"dt_txt": "2022-10-15 09:00:00", | ||
}, | ||
], | ||
"city": { | ||
"id": 7576815, | ||
"name": "Alt-Kölln", | ||
"coord": {"lat": 52.517, "lon": 13.3889}, | ||
"country": "DE", | ||
"population": 2000, | ||
"timezone": 7200, | ||
"sunrise": 1665811879, | ||
"sunset": 1665850372, | ||
}, | ||
} | ||
if "TNT_EX1_OPENWEATHERMAP_API_KEY" not in os.environ: | ||
dotenv_file = dotenv.find_dotenv() | ||
dotenv.load_dotenv(dotenv_file) | ||
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this and mock the environment variable getter. |
||
|
||
fetch_weather_data(os.environ["TNT_EX1_OPENWEATHERMAP_API_KEY"], (52.520008, 13.404954)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is nice how you call the function here, but you only check that it does not error out basically. If you really want to know what was done inside, you need to mock the |
||
assert request_mock.called |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
from geopy.geocoders import Nominatim | ||
|
||
|
||
def fetch_weather_data(api_key: str, location_coords: Tuple[int, int]) -> None: | ||
def fetch_weather_data(api_key: str, location_coords: Tuple[float, float]) -> None: | ||
""" | ||
Fetches weather data for 25 timepoints from the url if no recent data is available | ||
Stores the data in the folder where it is executed for future use. | ||
|
@@ -47,19 +47,7 @@ def fetch_weather_data(api_key: str, location_coords: Tuple[int, int]) -> None: | |
fetch_data_from_url = True | ||
|
||
if not os.path.exists(file_path_json) or fetch_data_from_url: | ||
print("Getting new data from URL", file=sys.stderr) | ||
response = requests.get( | ||
"https://api.openweathermap.org/data/2.5/forecast", | ||
params={ | ||
"lat": location_coords[0], | ||
"lon": location_coords[1], | ||
"dt": 25, | ||
"units": "metric", | ||
"appid": api_key, | ||
}, | ||
timeout=15, | ||
) | ||
data = response.json() | ||
data = fetch_new_data(api_key, location_coords) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so beautiful and smart 💡 I'm proud of you 😘 |
||
with open(file_path_json, "w", encoding="utf-8") as file: | ||
json.dump(data, file, ensure_ascii=False, indent=4) | ||
else: | ||
|
@@ -81,6 +69,25 @@ def fetch_weather_data(api_key: str, location_coords: Tuple[int, int]) -> None: | |
print(output) | ||
|
||
|
||
def fetch_new_data(api_key: str, location_coords: Tuple[float, float]) -> json: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have two choices:
|
||
""" | ||
Uses the request module to fetch new data | ||
""" | ||
print("Getting new data from URL", file=sys.stderr) | ||
response = requests.get( | ||
"https://api.openweathermap.org/data/2.5/forecast", | ||
params={ | ||
"lat": location_coords[0], | ||
"lon": location_coords[1], | ||
"dt": 25, | ||
"units": "metric", | ||
"appid": api_key, | ||
}, | ||
timeout=15, | ||
) | ||
return response.json() | ||
|
||
|
||
def fetch_location_coords(city: str) -> Tuple[int, int]: | ||
""" | ||
Gets the (lat, long) coordinates of a city name. If the city cannot be found | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not supposed to happen 🤯 you ought to mock the environment variable away. Unit test means to test a standalone unit 😘