Skip to content

Commit

Permalink
added get game card and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ychebyshev committed Feb 1, 2024
1 parent 85397ed commit 81515d7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 16 deletions.
49 changes: 48 additions & 1 deletion shvatka/api/models/responses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from datetime import datetime

from shvatka.core.models import dto
from shvatka.core.models.dto import scn
from shvatka.core.models.enums import GameStatus


Expand Down Expand Up @@ -54,3 +55,49 @@ def from_core(cls, core: dto.Game | None):
status=core.status,
start_at=core.start_at,
)


@dataclass
class Level:
db_id: int
name_id: str
author: Player
scenario: scn.LevelScenario
game_id: int | None = None
number_in_game: int | None = None

@classmethod
def from_core(cls, core: dto.Level | None = None):
if core is None:
return None
return cls(
db_id=core.db_id,
name_id=core.name_id,
author=Player.from_core(core.author),
scenario=core.scenario,
game_id=core.game_id,
number_in_game=core.number_in_game,
)


@dataclass
class FullGame:
id: int
author: Player
name: str
status: GameStatus
start_at: datetime | None
levels: list[Level] = field(default_factory=list)

@classmethod
def from_core(cls, core: dto.FullGame | None = None):
if core is None:
return None
return cls(
id=core.id,
author=Player.from_core(core.author),
name=core.name,
status=core.status,
start_at=core.start_at,
levels=[Level.from_core(level) for level in core.levels],
)
14 changes: 12 additions & 2 deletions shvatka/api/routes/game.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Sequence

from fastapi import APIRouter
from fastapi.params import Depends
from fastapi.params import Depends, Path

from shvatka.api.dependencies import dao_provider, player_provider, active_game_provider
from shvatka.api.models import responses
from shvatka.core.models import dto
from shvatka.core.services.game import get_authors_games, get_completed_games
from shvatka.core.services.game import get_authors_games, get_completed_games, get_full_game
from shvatka.infrastructure.db.dao.holder import HolderDao


Expand All @@ -30,9 +30,19 @@ async def get_all_games(
return [responses.Game.from_core(game) for game in games]


async def get_game_card(
dao: HolderDao = Depends(dao_provider), # type: ignore[assignment]
player: dto.Player = Depends(player_provider), # type: ignore[assignment]
id_: int = Path(alias="id"), # type: ignore[assignment]
):
game = await get_full_game(id_, player, dao.game)
return responses.FullGame.from_core(game)


def setup() -> APIRouter:
router = APIRouter(prefix="/games")
router.add_api_route("", get_all_games, methods=["GET"])
router.add_api_route("/my", get_my_games_list, methods=["GET"])
router.add_api_route("/active", get_active_game, methods=["GET"])
router.add_api_route("/{id}", get_game_card, methods=["GET"])
return router
12 changes: 12 additions & 0 deletions tests/integration/api_full/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from shvatka.api.dependencies import AuthProvider
from shvatka.api.main_factory import create_app
from shvatka.common import Paths
from shvatka.core.models import dto
from shvatka.core.services.user import upsert_user, set_password
from shvatka.infrastructure.db.dao.holder import HolderDao
from tests.fixtures.user_constants import create_dto_harry
from tests.integration.conftest import game_log # noqa: F401
from tests.mocks.config import DBConfig

Expand Down Expand Up @@ -45,3 +49,11 @@ def auth(api_config: ApiConfig):
async def client(app: FastAPI) -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient(app=app, base_url="http://test") as ac:
yield ac


@pytest_asyncio.fixture
async def user(dao: HolderDao, auth: AuthProvider) -> dto.User:
user_ = await upsert_user(create_dto_harry(), dao.user)
password = auth.get_password_hash("12345")
await set_password(user_, password, dao.user)
return user_
49 changes: 48 additions & 1 deletion tests/integration/api_full/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from dataclass_factory import Factory
from httpx import AsyncClient

from shvatka.api.dependencies import AuthProvider
from shvatka.api.models import responses
from shvatka.core.models import dto
from shvatka.core.models.enums import GameStatus
from shvatka.infrastructure.db.dao.holder import HolderDao


@pytest.mark.asyncio
async def test_game(game: dto.FullGame, dao: HolderDao, client: AsyncClient):
async def test_active_game(game: dto.FullGame, dao: HolderDao, client: AsyncClient):
await dao.game.start_waivers(game)
await dao.commit()
resp = await client.get("/games/active")
Expand All @@ -20,3 +21,49 @@ async def test_game(game: dto.FullGame, dao: HolderDao, client: AsyncClient):
actual = dcf.load(resp.json(), responses.Game)
assert game.id == actual.id
assert actual.status == GameStatus.getting_waivers


@pytest.mark.asyncio
async def test_games_list(finished_game: dto.FullGame, dao: HolderDao, client: AsyncClient):
await dao.game.set_completed(finished_game)
await dao.game.set_number(finished_game, 1)
await dao.commit()
resp = await client.get("/games")
assert resp.is_success
resp.read()

dcf = Factory()
actual = dcf.load(resp.json(), list[responses.Game])
assert len(actual) == 1
game = actual[0]
assert game.id == finished_game.id
assert game.status == GameStatus.complete


@pytest.mark.asyncio
async def test_game_card(
finished_game: dto.FullGame,
dao: HolderDao,
client: AsyncClient,
auth: AuthProvider,
user: dto.User,
):
token = auth.create_user_token(user)
await dao.game.set_completed(finished_game)
await dao.game.set_number(finished_game, 1)
await dao.commit()
resp = await client.get(
f"/games/{finished_game.id}",
headers={"Authorization": "Bearer " + token.access_token},
)
assert resp.is_success
resp.read()

dcf = Factory()
actual = dcf.load(resp.json(), responses.FullGame)
assert actual.id == finished_game.id
assert actual.status == GameStatus.complete
assert len(actual.levels) == len(finished_game.levels)
assert [lvl.scenario for lvl in actual.levels] == [
lvl.scenario for lvl in finished_game.levels
]
12 changes: 0 additions & 12 deletions tests/integration/api_full/test_user.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import pytest
import pytest_asyncio
from httpx import AsyncClient

from shvatka.api.dependencies import AuthProvider
from shvatka.core.models import dto
from shvatka.core.services.user import set_password, upsert_user
from shvatka.infrastructure.db.dao.holder import HolderDao
from tests.fixtures.user_constants import create_dto_harry


@pytest_asyncio.fixture
async def user(dao: HolderDao, auth: AuthProvider) -> dto.User:
user_ = await upsert_user(create_dto_harry(), dao.user)
password = auth.get_password_hash("12345")
await set_password(user_, password, dao.user)
return user_


@pytest.mark.asyncio
Expand Down

0 comments on commit 81515d7

Please sign in to comment.