Skip to content

Commit

Permalink
Merge pull request #256 from Whist-Team/fix/min_max_player_validation
Browse files Browse the repository at this point in the history
FIX: Minimum player must be higher than maximum player
  • Loading branch information
iTitus authored Nov 8, 2022
2 parents f80c5fc + bab4499 commit 6a10d74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion tests/whist_core/session/test_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tests.whist_core.base_test_case import BaseTestCase
from whist_core.error.table_error import TeamFullError, TableFullError, TableNotReadyError, \
TableNotStartedError, PlayerNotJoinedError
TableNotStartedError, PlayerNotJoinedError, TableSettingsError
from whist_core.game.rubber import Rubber
from whist_core.session.matcher import RandomMatcher, RoundRobinMatcher
from whist_core.session.table import Table
Expand All @@ -12,6 +12,10 @@ def setUp(self) -> None:
super().setUp()
self.table = Table(name='test table', min_player=1, max_player=4)

def test_min_max_validation(self):
with self.assertRaises(TableSettingsError):
_ = Table(name='faulty table', min_player=3, max_player=2)

def test_ready(self):
self.table.join(self.player)
self.table.player_ready(self.player)
Expand Down
6 changes: 6 additions & 0 deletions whist_core/error/table_error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""Errors regarding table logic."""


class TableSettingsError(Exception):
"""
Thrown if the table is not setup correctly.
"""


class TableFullError(Exception):
"""
Thrown if the table is already full.
Expand Down
16 changes: 15 additions & 1 deletion whist_core/session/table.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""DAO of session."""
from pydantic import root_validator

from whist_core.error.table_error import TableFullError, TeamFullError, TableNotReadyError, \
TableNotStartedError
TableNotStartedError, TableSettingsError
from whist_core.game.rubber import Rubber
from whist_core.session.matcher import Matcher
from whist_core.session.session import Session
Expand All @@ -18,6 +19,19 @@ class Table(Session):
started: bool = False
rubbers: list[Rubber] = []

# pylint: disable=no-self-argument
@root_validator(pre=True)
def validate_min_is_lower_max_player(cls, values):
"""
Checks if the min_player is less or equal than max_player.
:param values:
:return:
"""
if values.get('min_player') > values.get('max_player'):
raise TableSettingsError('The amount of minimum player must not be higher than the '
'maximum amount.')
return values

# pylint: disable=too-few-public-methods
class Config:
"""
Expand Down

0 comments on commit 6a10d74

Please sign in to comment.