-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed unit tests for fetch_weather_api()
- Loading branch information
1 parent
5e589a7
commit 87f774c
Showing
5 changed files
with
176 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
import requests | ||
|
||
|
||
import json | ||
|
||
from src.config import VISUAL_CROSSING_API_URL | ||
import requests | ||
|
||
from src.config import VISUAL_CROSSING_API_URL | ||
|
||
# Location is the address, partial address or latitude,longitude location for which to retrieve weather data. You can also use US ZIP Codes | ||
|
||
|
||
def fetch_weather_api(endpoint: str, location: str, unit: str, api_key: str) -> dict: | ||
|
||
base_url = f"{VISUAL_CROSSING_API_URL}/{endpoint}" | ||
|
||
params = { | ||
"location": location, | ||
"aggregateHours": 24, | ||
"unitGroup": unit, | ||
"contentType": "json", | ||
"key": api_key | ||
"key": api_key, | ||
} | ||
|
||
try: | ||
response = requests.get(base_url, params) | ||
response.raise_for_status() # raises an error for 4xx/5xx responses | ||
response.raise_for_status() # raises an error for 4xx/5xx responses | ||
except requests.exceptions.ConnectionError: | ||
raise ConnectionError("Failed to connect to the API. Check your network connection.") | ||
raise ConnectionError( | ||
"Failed to connect to the API. Check your network connection." | ||
) | ||
except requests.exceptions.Timeout: | ||
raise TimeoutError("The request timed out. Try again later.") | ||
except requests.exceptions.HTTPError as http_err: | ||
if response.status_code == 400: | ||
raise ValueError("Invalid parameters were provided to the API.") from http_err | ||
raise ValueError( | ||
"Invalid parameters were provided to the API." | ||
) from http_err | ||
elif response.status_code == 401: | ||
raise PermissionError("Invalid API key.") from http_err | ||
else: | ||
raise RuntimeError(f"HTTP error occured: {response.status_code}") from http_err | ||
raise RuntimeError( | ||
f"HTTP error occured: {response.status_code}" | ||
) from http_err | ||
except requests.exceptions.RequestException as e: | ||
raise RuntimeError("An unexpected error occurred with the request.") from e | ||
|
||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
|
||
|
||
|
||
VISUAL_CROSSING_API_URL = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/" | ||
VISUAL_CROSSING_API_URL = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,64 @@ | ||
import pytest | ||
|
||
|
||
|
||
|
||
@pytest.fixture(scope="function") | ||
def weather_api_data(): | ||
"""Fixture that returns simplified mock data for weather API response.""" | ||
return { | ||
'columns': { | ||
'address': {'id': 'address', 'name': 'Address', 'type': 1, 'unit': None}, | ||
'temp': {'id': 'temp', 'name': 'Temperature', 'type': 2, 'unit': 'degf'}, | ||
'humidity': {'id': 'humidity', 'name': 'Relative Humidity', 'type': 2, 'unit': None}, | ||
'conditions': {'id': 'conditions', 'name': 'Conditions', 'type': 1, 'unit': None}, | ||
'datetime': {'id': 'datetime', 'name': 'Date time', 'type': 3, 'unit': None} | ||
"columns": { | ||
"address": {"id": "address", "name": "Address", "type": 1, "unit": None}, | ||
"temp": {"id": "temp", "name": "Temperature", "type": 2, "unit": "degf"}, | ||
"humidity": { | ||
"id": "humidity", | ||
"name": "Relative Humidity", | ||
"type": 2, | ||
"unit": None, | ||
}, | ||
"conditions": { | ||
"id": "conditions", | ||
"name": "Conditions", | ||
"type": 1, | ||
"unit": None, | ||
}, | ||
"datetime": { | ||
"id": "datetime", | ||
"name": "Date time", | ||
"type": 3, | ||
"unit": None, | ||
}, | ||
}, | ||
'locations': { | ||
'Santa Monica': { | ||
'address': 'Santa Monica, CA, United States', | ||
'currentConditions': { | ||
'temp': 61.8, | ||
'humidity': 82.3, | ||
'conditions': 'Clear', | ||
'datetime': '2024-10-26T10:35:00-07:00', | ||
"locations": { | ||
"Santa Monica": { | ||
"address": "Santa Monica, CA, United States", | ||
"currentConditions": { | ||
"temp": 61.8, | ||
"humidity": 82.3, | ||
"conditions": "Clear", | ||
"datetime": "2024-10-26T10:35:00-07:00", | ||
}, | ||
'values': [ | ||
"values": [ | ||
{ | ||
'temp': 64.2, | ||
'humidity': 89.5, | ||
'conditions': 'Overcast', | ||
'datetimeStr': '2024-10-26T00:00:00-07:00' | ||
"temp": 64.2, | ||
"humidity": 89.5, | ||
"conditions": "Overcast", | ||
"datetimeStr": "2024-10-26T00:00:00-07:00", | ||
}, | ||
{ | ||
'temp': 73.2, | ||
'humidity': 67.3, | ||
'conditions': 'Sunny', | ||
'datetimeStr': '2024-10-27T00:00:00-07:00' | ||
"temp": 73.2, | ||
"humidity": 67.3, | ||
"conditions": "Sunny", | ||
"datetimeStr": "2024-10-27T00:00:00-07:00", | ||
}, | ||
{ | ||
'temp': 69.5, | ||
'humidity': 78.8, | ||
'conditions': 'Windy', | ||
'datetimeStr': '2024-10-28T00:00:00-07:00' | ||
"temp": 69.5, | ||
"humidity": 78.8, | ||
"conditions": "Windy", | ||
"datetimeStr": "2024-10-28T00:00:00-07:00", | ||
}, | ||
] | ||
], | ||
} | ||
}, | ||
'messages': None, | ||
'queryCost': 1, | ||
'remainingCost': 0 | ||
} | ||
"messages": None, | ||
"queryCost": 1, | ||
"remainingCost": 0, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters