|
| 1 | +import pytest |
| 2 | +from httpx import ConnectTimeout, HTTPStatusError |
| 3 | +from pytest_httpx import HTTPXMock |
| 4 | + |
| 5 | +from src.owm_api import fetch_weather, retrieve_weather_info |
| 6 | +from tests.constants import OPENWEATHERMAP_REQUEST, OWM_RESPONSE_FRAGMENT |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_retrieve_weather_info( |
| 11 | + httpx_mock: HTTPXMock, fake_owmweather, set_owm_api_env_variables |
| 12 | +): |
| 13 | + httpx_mock.add_response( |
| 14 | + method="GET", url=OPENWEATHERMAP_REQUEST, text=OWM_RESPONSE_FRAGMENT |
| 15 | + ) |
| 16 | + |
| 17 | + owmweather_instance = await retrieve_weather_info() |
| 18 | + |
| 19 | + assert owmweather_instance == fake_owmweather |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +async def test_fetch_weather(httpx_mock: HTTPXMock, set_owm_api_env_variables): |
| 24 | + httpx_mock.add_response(method="GET", url=OPENWEATHERMAP_REQUEST, json={}) |
| 25 | + |
| 26 | + json_response = await fetch_weather() |
| 27 | + |
| 28 | + assert json_response == {} |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.asyncio |
| 32 | +async def test_fetch_weather__connection_timeout( |
| 33 | + httpx_mock: HTTPXMock, set_owm_api_env_variables |
| 34 | +): |
| 35 | + httpx_mock.add_exception(ConnectTimeout(""), url=OPENWEATHERMAP_REQUEST) |
| 36 | + |
| 37 | + with pytest.raises( |
| 38 | + ConnectTimeout, |
| 39 | + match="Request to OpenWeatherMap API took more than 15.0 seconds", |
| 40 | + ): |
| 41 | + await fetch_weather() |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_fetch_weather__unauthorized( |
| 46 | + httpx_mock: HTTPXMock, set_owm_api_env_variables |
| 47 | +): |
| 48 | + httpx_mock.add_response( |
| 49 | + method="GET", url=OPENWEATHERMAP_REQUEST, json={}, status_code=401 |
| 50 | + ) |
| 51 | + |
| 52 | + with pytest.raises(HTTPStatusError): |
| 53 | + await fetch_weather() |
0 commit comments