Skip to content

Commit

Permalink
feat: Update locationforecast tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroWave022 committed Oct 11, 2023
1 parent 7a22694 commit 2586171
Showing 1 changed file with 57 additions and 35 deletions.
92 changes: 57 additions & 35 deletions tests/test_locationforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,85 +5,107 @@
import requests

from yr_weather import Locationforecast
from yr_weather.locationforecast import (
from yr_weather.data.locationforecast import (
Forecast,
ForecastTime,
ForecastTimeDetails,
ForecastFuture,
ForecastFutureDetails,
ForecastFutureSummary,
ForecastGeometry,
ForecastUnits,
)

HEADERS = {"User-Agent": "testing/latest https://github.com/ZeroWave022/yr-weather"}

@pytest.fixture(scope="module")
def client():
headers = {"User-Agent": "testing/latest https://github.com/ZeroWave022/yr-weather"}
return Locationforecast(headers)


@pytest.fixture(scope="module")
def api_available(client):
"""Test if the API is available."""
def api_available():
"""Checks if the API is available."""
status_req = requests.get(
"https://api.met.no/weatherapi/locationforecast/2.0/status",
headers=client._global_headers,
headers=HEADERS,
timeout=30,
)

# API testing can't continue if the API isn't functional
if not status_req.ok:
return False
return status_req.ok


@pytest.fixture(name="client", scope="module")
def fixture_client():
"""The Locationforecast client"""
return Locationforecast(HEADERS)

return True

@pytest.fixture(name="forecast", scope="module")
def fixture_forecast(client: Locationforecast):
"""An example locationforecast"""
return client.get_forecast(59.91, 10.75)


@pytest.mark.skipif(
not api_available(),
reason="Testing cannot continue: MET Locationforecast API is not responding.",
)
class TestLocationforecast:
def _raise_if_api_unavailable(self, api_available):
if not api_available:
raise RuntimeError(
"MET Locationforecast API is not responding. Testing cannot continue."
)
"""Test yr_weather.Locationforecast"""

def test_headers(self):
"""Test that headers are required."""
"""Test that headers are required"""
with pytest.raises(ValueError, match="A custom 'User-Agent' is required"):
Locationforecast({})

def test_forecast(self, api_available, client):
"""Test various Locationforecast methods and classes."""
self._raise_if_api_unavailable(api_available)

forecast = client.get_forecast(59.91, 10.75)

def test_forecast(self, forecast: Forecast):
"""Test initial Forecast data"""
assert isinstance(forecast, Forecast)
assert isinstance(forecast.type, str)
assert isinstance(forecast.geometry, ForecastGeometry)
assert isinstance(forecast.updated_at, str)
assert isinstance(forecast.units, ForecastUnits)

def test_forecast_now(self, forecast: Forecast):
"""Test Forecast.now()"""
now = forecast.now()

assert isinstance(now, ForecastTime)
assert isinstance(now.details, ForecastTimeDetails)
assert isinstance(now.next_hour, ForecastFuture)
assert isinstance(now.next_6_hours, ForecastFuture)
assert isinstance(now.next_12_hours, ForecastFuture)

def test_forecast_get_time(self, forecast: Forecast):
"""Test Forecast.get_forecast_time()"""
datetime_in_3_hrs = datetime.now() + timedelta(hours=3)
in_3_hrs = forecast.get_forecast_time(datetime_in_3_hrs)

assert isinstance(in_3_hrs, ForecastTime)
assert isinstance(in_3_hrs.details, ForecastTimeDetails)
assert isinstance(in_3_hrs.next_hour, ForecastFuture)
assert isinstance(in_3_hrs.next_6_hours, ForecastFuture)
assert isinstance(in_3_hrs.next_12_hours, ForecastFuture)

forecast_in_3_hrs = forecast.get_forecast_time(datetime_in_3_hrs)
def test_future_forecast(self, forecast: Forecast):
"""Test ForecastFuture data"""
now = forecast.now()

assert isinstance(forecast_in_3_hrs, ForecastTime)
assert isinstance(now.next_hour.details, ForecastFutureDetails)
assert isinstance(now.next_hour.summary, ForecastFutureSummary)

def test_air_temperature(self, api_available, client):
"""Test air temperature function."""
self._raise_if_api_unavailable(api_available)
assert isinstance(now.next_6_hours.details, ForecastFutureDetails)
assert isinstance(now.next_6_hours.summary, ForecastFutureSummary)

assert isinstance(now.next_12_hours.details, ForecastFutureDetails)
assert isinstance(now.next_12_hours.summary, ForecastFutureSummary)

def test_air_temperature(self, client: Locationforecast):
"""Test air temperature function"""
air_temp_1 = client.get_air_temperature(59.91, 10.75)
air_temp_2 = client.get_air_temperature(59.91, 10.75, 100)

assert isinstance(air_temp_1, float)
assert isinstance(air_temp_2, float)

def test_instant_data(self, api_available, client):
"""Test instant data function."""
self._raise_if_api_unavailable(api_available)

def test_instant_data(self, client: Locationforecast):
"""Test instant data function"""
data = client.get_instant_data(59.91, 10.75)
data_2 = client.get_instant_data(59.91, 10.75, 100)

Expand Down

0 comments on commit 2586171

Please sign in to comment.