Skip to content

Commit

Permalink
tests: improve test coverage and remove unused fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
bendikrb committed Oct 24, 2024
1 parent 6c6b7b9 commit cdb563e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion nrk_psapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ async def get_podcast_episodes(
season_id: str | None = None,
*,
page_size: int | None = None,
page: int = 1,
page: int | None = None,
) -> list[Episode]:
"""Get podcast episodes.
Expand Down
28 changes: 0 additions & 28 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import contextlib
from importlib import reload
import logging
import os
import tempfile
Expand All @@ -18,11 +17,6 @@ def pytest_addoption(parser):
)


@pytest.fixture
def update_fixtures(request):
return request.config.getoption("update_fixtures")


@pytest.fixture
def refresh_environment(): # noqa: PT004
"""Refresh the test environment."""
Expand All @@ -48,25 +42,3 @@ def test_cache(refresh_environment):
yield nrk_psapi.caching.cache()

memory.clear()


@pytest.fixture
def temp_cache_dir(): # noqa: PT004
import os
import tempfile

import nrk_psapi.caching

with tempfile.TemporaryDirectory() as tempdir:
os.environ["NRK_PSAPI_CACHE_DIR"] = tempdir
nrk_psapi.caching.get_cache.cache_clear()
# noinspection PyTypeChecker
reload(nrk_psapi)
reload(nrk_psapi.api)
# noinspection PyProtectedMember
cache_status = nrk_psapi.caching._caching_enabled
try:
nrk_psapi.caching._caching_enabled = True
yield
finally:
nrk_psapi.caching._caching_enabled = cache_status
14 changes: 12 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import asyncio
import logging
from unittest.mock import AsyncMock
import socket
from unittest.mock import AsyncMock, patch

import aiohttp
from aiohttp.web_response import json_response
Expand Down Expand Up @@ -355,7 +356,7 @@ async def test_get_series_episodes(
)
async with aiohttp.ClientSession() as session:
nrk_api = NrkPodcastAPI(session=session, enable_cache=False)
result = await nrk_api.get_series_episodes(series_id, season_id, page_size=20, page=1)
result = await nrk_api.get_series_episodes(series_id, season_id)
assert isinstance(result, list)
assert len(result) > 0
assert all(isinstance(item, Episode) for item in result)
Expand Down Expand Up @@ -881,6 +882,15 @@ async def test_http_error429(aresponses: ResponsesMockServer):
assert await nrk_api._request("ipcheck")


async def test_network_error():
"""Test network error handling."""
async with aiohttp.ClientSession() as session:
with patch.object(session, 'request', side_effect=socket.gaierror):
nrk_api = NrkPodcastAPI(session=session, enable_cache=False)
with pytest.raises(NrkPsApiConnectionError):
assert await nrk_api._request("ipcheck")


async def test_unexpected_response(aresponses: ResponsesMockServer):
"""Test unexpected response handling."""
aresponses.add(
Expand Down

0 comments on commit cdb563e

Please sign in to comment.