Skip to content

Commit

Permalink
#106 added few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Nov 28, 2024
1 parent 8545264 commit 41f89c4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion shvatka/core/models/dto/action/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@dataclass(frozen=True)
class BonusKey:
text: str
text: SHKey
bonus_minutes: float

def __eq__(self, other: object) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions shvatka/core/models/dto/scn/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ def get_keys(self) -> set[str]:
result: set[SHKey] = set()
for condition in self.conditions:
if isinstance(condition, KeyWinCondition):
result.union(condition.keys)
result = result.union(condition.keys)
return result

def get_bonus_keys(self) -> set[BonusKey]:
result: set[BonusKey] = set()
for condition in self.conditions:
if isinstance(condition, KeyBonusCondition):
result.union(condition.keys)
result = result.union(condition.keys)
return result

@overload
Expand Down
25 changes: 10 additions & 15 deletions shvatka/core/services/key.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import logging
import typing
from dataclasses import dataclass


from shvatka.core.interfaces.dal.game_play import GamePlayerDao
from shvatka.core.models import dto, enums
from shvatka.core.models.dto import action
from shvatka.core.models.dto.action import DecisionType
from shvatka.core.utils import exceptions
from shvatka.core.utils.input_validation import is_key_valid
from shvatka.core.utils.key_checker_lock import KeyCheckerFactory
Expand Down Expand Up @@ -76,29 +74,26 @@ async def submit_key(
logger.warning("impossible decision here is %s", type(decision))
return None

def decision_to_parsed_key(decision: action.KeyDecision | action.BonusKeyDecision | action.WrongKeyDecision) -> dto.ParsedKey:

def decision_to_parsed_key(
decision: action.KeyDecision | action.BonusKeyDecision | action.WrongKeyDecision,
) -> dto.ParsedKey:
match decision:
case action.KeyDecision:
return dto.ParsedKey(
type_=decision.key_type,
text=decision.key_text,
)
case action.BonusKeyDecision:
if decision.type == DecisionType.BONUS_TIME:
return dto.ParsedBonusKey(
type_=enums.KeyType.bonus,
text=decision.key_text,
bonus_minutes=decision.key.bonus_minutes,
)
else:
return dto.ParsedKey(
type_=enums.KeyType.wrong,
text=decision.key_text,
)
return dto.ParsedBonusKey(
type_=enums.KeyType.bonus,
text=decision.key_text,
bonus_minutes=decision.key.bonus_minutes,
)
case action.WrongKeyDecision:
return dto.ParsedKey(
type_=decision.key_type,
text=decision.key,
)
case _:
typing.assert_never(decision)
raise NotImplementedError(f"unknown decision type {type(decision)}")
27 changes: 27 additions & 0 deletions tests/unit/domain/conditions_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from shvatka.core.models.dto.action import keys
from shvatka.core.models.dto import scn
from shvatka.core.models.dto import action
from shvatka.core.utils import exceptions


def test_create_one_key():
c = scn.Conditions([action.KeyWinCondition({keys.SHKey("SH321")})])
assert len(c) == 1
assert len(c.get_keys()) == 1
assert len(c.get_bonus_keys()) == 0
actual = c[0]
assert isinstance(actual, action.KeyWinCondition)
assert actual.keys == {keys.SHKey("SH321")}
assert c.get_keys() == {keys.SHKey("SH321")}


def test_create_empty_condition():
with pytest.raises(exceptions.LevelError):
scn.Conditions([])


def test_create_only_bonus_condition():
with pytest.raises(exceptions.LevelError):
scn.Conditions([action.KeyBonusCondition({keys.BonusKey(text="SH123", bonus_minutes=1)})])

0 comments on commit 41f89c4

Please sign in to comment.