Skip to content

Commit

Permalink
Merge pull request #136 from hufscheer/refactor/game
Browse files Browse the repository at this point in the history
[REFACTOR] 게임 생성시 리그의 최대 라운드보다 높지 못하게 변경
  • Loading branch information
leehjhjhj authored Mar 25, 2024
2 parents e00e1a0 + cdb1354 commit 2ee3341
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/sportslive/game/application/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
from league.domain import LeagueRepository, League
from django.core.exceptions import PermissionDenied
from utils.exceptions.game_exceptions import CantDeleteGameError, CantParsingYoutubeUrl
from utils.exceptions.game_exceptions import CantDeleteGameError, CantParsingYoutubeUrl, BiggerThanMaxRoundError
from datetime import datetime
import re

Expand All @@ -22,6 +22,7 @@ def create_game(self, league_id: int, request_data, user_data: Member):
game_data: dict = game_request_serializer.validated_data

league = self._league_repository.find_league_by_id(league_id)
self._check_round(game_data, league)
new_game: Game = self._create_game_object(game_data, user_data, league)
self._game_repository.save_game(new_game)

Expand All @@ -34,10 +35,11 @@ def change_game(self, game_id: int, request_data, user_data: Member):
game: Game = self._game_repository.find_game_with_manger_by_id(game_id)
if game.manager.organization != user_data.organization:
raise PermissionDenied

league = self._league_repository.find_league_by_id(game.league_id)
game_change_serializer = GameChangeSerializer(game, data=request_data)
game_change_serializer.is_valid(raise_exception=True)
game_change_data = game_change_serializer.validated_data
self._check_round(game_change_data, league)
self._change_game_object(game, game_change_data)

def delete_game(self, game_id: int, user_data: Member):
Expand All @@ -54,6 +56,12 @@ def get_game_info(self, game_id: int):
game_info_response_serialzier = GameInfoResponseSerializer(game)
return game_info_response_serialzier.data

def _check_round(self, game_data: dict, league: League):
round = game_data.get('round')
league_max_round = league.max_round
if round > league_max_round:
raise BiggerThanMaxRoundError

def _create_game_object(self, game_data: dict, user_data: Member, league: League) -> Game:
return Game(
sport_id=game_data.get('sport_id'),
Expand Down
8 changes: 7 additions & 1 deletion src/sportslive/utils/exceptions/game_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ class CantDeleteGameError(APIException):

class CantParsingYoutubeUrl(APIException):
status_code = 400
default_detail = "유튜브 링크 형식이 잘못되었습니다."
default_detail = "유튜브 링크 형식이 잘못되었습니다."
default_code = "cant_parsing_youtube_url"

class BiggerThanMaxRoundError(APIException):
status_code = 400
default_detail = "리그의 최대 라운드 보다 크게 할 수 없습니다."
default_code = "bigger_than_max_round_error"
2 changes: 1 addition & 1 deletion src/tests/resources/fixture.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VALUES (1, '전반전', 1),
(2, '후반전', 1);

INSERT INTO leagues (id, name, is_deleted, start_at, end_at, manager_id, organization_id, max_round, in_progress_round)
VALUES (1, '외대 월드컵', 0, '2024-03-20 00:00:00', '2024-03-24 00:00:00', 1, 1, 8, 2),
VALUES (1, '외대 월드컵', 0, '2024-03-20 00:00:00', '2024-03-24 00:00:00', 1, 1, 16, 2),
(2, '아대 월드컵', 0, '2024-03-20 00:00:00', '2024-03-24 00:00:00', 1, 1, 16, 16);

INSERT INTO league_sports (id, league_id, sport_id) VALUES (1, 1, 1);
Expand Down
39 changes: 37 additions & 2 deletions src/tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .fixture import load_sql_fixture
from game.containers import GameContainer
from game.domain import Game, GameTeam
from utils.exceptions.game_exceptions import CantDeleteGameError, CantParsingYoutubeUrl
from utils.exceptions.game_exceptions import CantDeleteGameError, CantParsingYoutubeUrl, BiggerThanMaxRoundError
from django.core.exceptions import PermissionDenied
class TestGame:

Expand Down Expand Up @@ -54,6 +54,22 @@ def test_create_game2(self, load_sql_fixture, dependency_fixture):
assert GameTeam.objects.filter(id=7).exists()
assert GameTeam.objects.filter(id=8).exists()

@pytest.mark.django_db
def test_fail_create_game(self, load_sql_fixture, dependency_fixture):
member = Member.objects.get(id=1)
request_data = {
"sportsId": 1,
"startTime": "2024-03-22 14:00:00",
"gameName": "1경기",
"videoId": "https://youtu.be/yE0MWJN6KCU?si=Z_EQc0VbbQwSdmVk",
"teamIds": [
2, 3
],
"round": 32
}
with pytest.raises(BiggerThanMaxRoundError):
self._game_service.create_game(1, request_data, member)

@pytest.mark.django_db
def test_change_game(self, load_sql_fixture, dependency_fixture):
"""
Expand Down Expand Up @@ -105,11 +121,30 @@ def test_fail_change_game2(self, load_sql_fixture, dependency_fixture):
"gameName": "결승",
"videoId": "video.com",
"gameQuarter": "전반전",
"state": "PLAYING"
"state": "PLAYING",
"round": 2
}
with pytest.raises(CantParsingYoutubeUrl):
self._game_service.change_game(3, request_data, member)

@pytest.mark.django_db
def test_fail_change_game3(self, load_sql_fixture, dependency_fixture):
"""
결승전 바꾸기 실패3 (라운드가 max보다 큼)
"""
member = Member.objects.get(id=1)
request_data = {
"sportsId": 1,
"startTime": "2024-03-22 14:00:00",
"gameName": "결승",
"videoId": "video.com",
"gameQuarter": "전반전",
"state": "PLAYING",
"round": 32
}
with pytest.raises(BiggerThanMaxRoundError):
self._game_service.change_game(3, request_data, member)

@pytest.mark.django_db
def test_delete_game(self, load_sql_fixture, dependency_fixture):
"""
Expand Down

0 comments on commit 2ee3341

Please sign in to comment.