Skip to content

Commit

Permalink
Fix create game
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-takker committed Mar 2, 2024
1 parent bfa0233 commit 2616362
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion industry_game/handlers/games/create_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ async def post(self) -> Response:
game = await self.game_storage.create(
name=new_game_data.name,
description=new_game_data.description,
created_by=self.user.id,
created_by_id=self.user.id,
)
return msgspec_json_response(game, status=HTTPStatus.CREATED)
4 changes: 2 additions & 2 deletions industry_game/utils/games/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ async def create(
session: AsyncSession,
name: str,
description: str,
created_by: int,
created_by_id: int,
commit: bool = True,
) -> Game:
stmt = (
insert(GameDb)
.values(
name=name,
description=description,
created_by=created_by,
created_by_id=created_by_id,
)
.returning(GameDb)
)
Expand Down
81 changes: 81 additions & 0 deletions tests/test_api/test_games/test_game_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from collections.abc import Mapping
from http import HTTPStatus

from aiohttp.test_utils import TestClient
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from yarl import URL

from industry_game.db.models import Game
from industry_game.utils.users.base import UserType
from tests.utils.datetime import format_tz

API_URL = URL("/api/v1/games/")


async def test_game_create_unauthorized(api_client: TestClient):
response = await api_client.post(API_URL)
assert response.status == HTTPStatus.UNAUTHORIZED


async def test_game_create_player_forbidden(
api_client: TestClient,
player_headers: Mapping[str, str],
):
response = await api_client.post(API_URL, headers=player_headers)
assert response.status == HTTPStatus.FORBIDDEN


async def test_game_create_empty_json(
api_client: TestClient,
admin_headers: Mapping[str, str],
):
response = await api_client.post(API_URL, headers=admin_headers)
assert response.status == HTTPStatus.BAD_REQUEST


async def test_game_create_successful_status_created(
api_client: TestClient,
admin_headers: Mapping[str, str],
create_user,
):
await create_user(type=UserType.ADMIN, id=1)
response = await api_client.post(
API_URL,
headers=admin_headers,
json={
"name": "This is new game",
"description": "This is new game description",
},
)
assert response.status == HTTPStatus.CREATED


async def test_game_create_successful_format(
api_client: TestClient,
admin_headers: Mapping[str, str],
session: AsyncSession,
create_user,
):
await create_user(type=UserType.ADMIN, id=1)
response = await api_client.post(
API_URL,
headers=admin_headers,
json={
"name": "This is new game",
"description": "This is new game description",
},
)
game = (await session.scalars(select(Game))).one()
result = await response.json()
assert result == {
"id": game.id,
"name": game.name,
"description": game.description,
"status": game.status.value,
"created_by_id": game.created_by_id,
"finished_at": format_tz(game.finished_at),
"started_at": format_tz(game.started_at),
"created_at": format_tz(game.created_at),
"updated_at": format_tz(game.updated_at),
}

0 comments on commit 2616362

Please sign in to comment.