Skip to content

Commit

Permalink
#106 added tests for key action
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Nov 28, 2024
1 parent f4c4683 commit 622788f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
2 changes: 1 addition & 1 deletion shvatka/core/models/dto/action/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class State(Protocol):
pass


T = typing.TypeVar("T")
T = typing.TypeVar("T", bound=State)


class StateHolder(Protocol):
Expand Down
4 changes: 2 additions & 2 deletions shvatka/core/models/dto/action/state_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class InMemoryStateHolder(StateHolder):
all_typed: set[SHKey]

def get(self, state_class: type[T]) -> T:
if isinstance(state_class, TypedKeysState):
return TypedKeysState(
if state_class == TypedKeysState:
return TypedKeysState( # type: ignore[return-value]
typed_correct=self.typed_correct,
all_typed=self.all_typed,
)
Expand Down
112 changes: 112 additions & 0 deletions tests/unit/domain/level_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import pytest

from shvatka.core.models import enums
from shvatka.core.models.dto import scn, action


@pytest.fixture
def hints() -> scn.HintsList:
return scn.HintsList(
[
scn.TimeHint(time=0, hint=[scn.TextHint("hint")]),
scn.TimeHint(time=5, hint=[scn.TextHint("other hint")]),
]
)


@pytest.fixture
def level_one_key(hints: scn.HintsList) -> scn.LevelScenario:
return scn.LevelScenario(
id="test1",
time_hints=hints,
conditions=scn.Conditions([action.KeyWinCondition({"SH123"})]),
)

@pytest.fixture
def level_three_keys(hints: scn.HintsList) -> scn.LevelScenario:
return scn.LevelScenario(
id="test2",
time_hints=hints,
conditions=scn.Conditions([
action.KeyWinCondition({"SH123", "SH321", "СХ123"})
]),
)


def test_win_level_single_key(level_one_key: scn.LevelScenario):
decision = level_one_key.check(
action.TypedKeyAction("SH123"), action.InMemoryStateHolder(set(), set())
)

assert isinstance(decision, action.KeyDecision)
assert decision.key == "SH123"
assert decision.key_text == "SH123"
assert decision.key_type == enums.KeyType.simple
assert decision.type == action.DecisionType.LEVEL_UP
assert decision.is_level_up()
assert not decision.duplicate


def test_wrong_level_single_key(level_one_key: scn.LevelScenario):
decision = level_one_key.check(
action.TypedKeyAction("SHWRONG"), action.InMemoryStateHolder(set(), set())
)

assert isinstance(decision, action.WrongKeyDecision)
assert decision.key == "SHWRONG"
assert decision.key_text == "SHWRONG"
assert decision.key_type == enums.KeyType.wrong
assert decision.type == action.DecisionType.NO_ACTION
assert not decision.duplicate


def test_duplicate_wrong_level_single_key(level_one_key: scn.LevelScenario):
decision = level_one_key.check(
action.TypedKeyAction("SHWRONG"), action.InMemoryStateHolder(set(), {"SHWRONG"})
)

assert isinstance(decision, action.WrongKeyDecision)
assert decision.key == "SHWRONG"
assert decision.key_text == "SHWRONG"
assert decision.key_type == enums.KeyType.wrong
assert decision.type == action.DecisionType.NO_ACTION
assert decision.duplicate

def test_second_key_of_three(level_three_keys: scn.LevelScenario):
decision = level_three_keys.check(
action.TypedKeyAction("SH123"), action.InMemoryStateHolder({"SH321"}, {"SH321"})
)

assert isinstance(decision, action.KeyDecision)
assert decision.key == "SH123"
assert decision.key_text == "SH123"
assert decision.key_type == enums.KeyType.simple
assert decision.type == action.DecisionType.SIGNIFICANT_ACTION
assert not decision.is_level_up()
assert not decision.duplicate

def test_duplicate_second_key_of_three(level_three_keys: scn.LevelScenario):
decision = level_three_keys.check(
action.TypedKeyAction("SH123"), action.InMemoryStateHolder({"SH321", "SH123"}, {"SH321", "SH123"})
)

assert isinstance(decision, action.KeyDecision)
assert decision.key == "SH123"
assert decision.key_text == "SH123"
assert decision.key_type == enums.KeyType.simple
assert decision.type == action.DecisionType.NO_ACTION
assert not decision.is_level_up()
assert decision.duplicate

def test_third_key_of_three(level_three_keys: scn.LevelScenario):
decision = level_three_keys.check(
action.TypedKeyAction("SH123"), action.InMemoryStateHolder({"SH321", "СХ123"}, {"SH321", "СХ123"})
)

assert isinstance(decision, action.KeyDecision)
assert decision.key == "SH123"
assert decision.key_text == "SH123"
assert decision.key_type == enums.KeyType.simple
assert decision.type == action.DecisionType.LEVEL_UP
assert decision.is_level_up()
assert not decision.duplicate

0 comments on commit 622788f

Please sign in to comment.