Skip to content

Commit df856d8

Browse files
committed
Add seasons tests
1 parent 4f3e800 commit df856d8

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

tests/conftest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ def episode(faker: Faker, production_code):
5858
)
5959

6060

61+
@pytest.fixture
62+
def season(faker: Faker, production_code):
63+
return SeasonModel(
64+
id=faker.random_int(
65+
min=1,
66+
max=INT32,
67+
),
68+
episodes=[
69+
EpisodeModel(
70+
id=faker.random_int(
71+
min=1,
72+
max=INT32,
73+
),
74+
created_at=faker.date_time(),
75+
name=faker.name(),
76+
air_date=faker.date_object(),
77+
duration=faker.random_int(),
78+
production_code=production_code,
79+
broadcast_number=faker.random_int(
80+
min=1,
81+
max=INT32,
82+
),
83+
),
84+
],
85+
)
86+
87+
6188
@pytest.fixture
6289
def mock_session_manager(request):
6390
mock_context = AsyncMock()

tests/unit/routers/services/seasons/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from unittest.mock import MagicMock
2+
3+
import pytest
4+
from faker import Faker
5+
from fastapi import HTTPException
6+
from sqlalchemy.exc import NoResultFound
7+
8+
from futuramaapi.repositories import INT32
9+
from futuramaapi.repositories.models import SeasonModel
10+
from futuramaapi.routers.services.seasons.get_season import GetSeasonService
11+
12+
13+
class TestGetSeasonService:
14+
@pytest.mark.asyncio
15+
async def test_get_season_service_success(self, season: SeasonModel, mock_session_manager):
16+
# Arrange
17+
mock_result = MagicMock()
18+
mock_result.scalars.return_value.one.return_value = season
19+
mock_session_manager.execute.return_value = mock_result
20+
21+
service = GetSeasonService(pk=season.id)
22+
23+
# Act
24+
result = await service()
25+
26+
# Assert
27+
assert result.id == season.id
28+
assert result.episodes[0].id == season.episodes[0].id
29+
30+
@pytest.mark.asyncio
31+
async def test_get_season_service_not_found(self, faker: Faker, mock_session_manager):
32+
# Arrange
33+
mock_result = MagicMock()
34+
mock_result.scalars.return_value.one.side_effect = NoResultFound
35+
mock_session_manager.execute.return_value = mock_result
36+
37+
service = GetSeasonService(
38+
pk=faker.random_int(
39+
min=1,
40+
max=INT32,
41+
),
42+
)
43+
44+
# Act & Assert
45+
with pytest.raises(HTTPException):
46+
await service()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from unittest.mock import AsyncMock, MagicMock, patch
2+
3+
import pytest
4+
5+
from futuramaapi.routers.services.seasons.list_seasons import ListSeasonsService
6+
7+
8+
@pytest.fixture
9+
def mock_paginate(request):
10+
patcher = patch(
11+
"futuramaapi.routers.services.seasons.list_seasons.paginate",
12+
new_callable=AsyncMock,
13+
)
14+
mocked = patcher.start()
15+
request.addfinalizer(patcher.stop)
16+
return mocked
17+
18+
19+
class TestListSeasonsService:
20+
@pytest.mark.asyncio
21+
async def test_list_seasons_success(
22+
self,
23+
mock_session_manager,
24+
mock_paginate,
25+
):
26+
# Arrange
27+
page = MagicMock()
28+
mock_paginate.return_value = page
29+
30+
service = ListSeasonsService()
31+
32+
# Act
33+
result = await service()
34+
35+
# Assert
36+
assert result is page
37+
mock_paginate.assert_called_once()
38+
39+
args, _ = mock_paginate.call_args
40+
assert args[0] is mock_session_manager

0 commit comments

Comments
 (0)