Skip to content

Commit

Permalink
#106 added few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Nov 15, 2024
1 parent f6cd0f6 commit 4b138c3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
7 changes: 2 additions & 5 deletions shvatka/core/models/dto/scn/level.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from abc import abstractmethod
from collections.abc import Sequence, Iterable
from dataclasses import dataclass
from datetime import timedelta
Expand Down Expand Up @@ -150,13 +149,13 @@ def validate(conditions: Sequence[WinCondition]) -> None:
raise exceptions.LevelError(
text=f"keys already exists {keys.intersection(c.keys)}"
)
keys.union(c.keys)
keys = keys.union(c.keys)
elif isinstance(c, KeyBonusCondition):
if keys.intersection({k.text for k in c.keys}):
raise exceptions.LevelError(
text=f"keys already exists {keys.intersection(c.keys)}"
)
keys.union({k.text for k in c.keys})
keys = keys.union({k.text for k in c.keys})
if not win_conditions:
raise exceptions.LevelError(text="There is no win condition")

Expand All @@ -175,12 +174,10 @@ def get_bonus_keys(self) -> set[BonusKey]:
return result

@overload
@abstractmethod
def __getitem__(self, index: int) -> WinCondition:
return self.conditions[index]

@overload
@abstractmethod
def __getitem__(self, index: slice) -> Sequence[WinCondition]:
return self.conditions[index]

Expand Down
88 changes: 84 additions & 4 deletions tests/unit/domain/conditions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
from shvatka.core.utils import exceptions


@pytest.fixture
def complex_conditions() -> scn.Conditions:
return scn.Conditions(
[
action.KeyWinCondition({keys.SHKey("SH123"), keys.SHKey("SH321")}),
action.KeyBonusCondition(
{
keys.BonusKey(text="SHB1", bonus_minutes=1),
keys.BonusKey(text="SHB2", bonus_minutes=-1),
}
),
action.KeyBonusCondition({keys.BonusKey(text="SHB3", bonus_minutes=0)}),
action.KeyWinCondition({keys.SHKey("СХ123")}),
]
)


def test_create_one_key():
c = scn.Conditions([action.KeyWinCondition({keys.SHKey("SH321")})])
assert len(c) == 1
Expand All @@ -26,9 +43,72 @@ def test_create_only_bonus_condition():
with pytest.raises(exceptions.LevelError):
scn.Conditions([action.KeyBonusCondition({keys.BonusKey(text="SH123", bonus_minutes=1)})])


def test_conditions_get_keys():
c = scn.Conditions([
action.KeyWinCondition({keys.SHKey("SH123"), keys.SHKey("SH321")}),
action.KeyWinCondition({keys.SHKey("СХ123")})
])
c = scn.Conditions(
[
action.KeyWinCondition({keys.SHKey("SH123"), keys.SHKey("SH321")}),
action.KeyWinCondition({keys.SHKey("СХ123")}),
]
)
assert c.get_keys() == {keys.SHKey("SH123"), keys.SHKey("SH321"), keys.SHKey("СХ123")}


def test_conditions_get_keys_with_bonus(complex_conditions: scn.Conditions):
assert complex_conditions.get_keys() == {
keys.SHKey("SH123"),
keys.SHKey("SH321"),
keys.SHKey("СХ123"),
}


def test_conditions_get_bonus_keys(complex_conditions: scn.Conditions):
assert complex_conditions.get_bonus_keys() == {
keys.BonusKey(text="SHB1", bonus_minutes=1),
keys.BonusKey(text="SHB2", bonus_minutes=-1),
keys.BonusKey(text="SHB3", bonus_minutes=0),
}


def test_conditions_duplicate_keys():
with pytest.raises(exceptions.LevelError):
scn.Conditions(
[
action.KeyWinCondition({keys.SHKey("SH123"), keys.SHKey("SH321")}),
action.KeyWinCondition({keys.SHKey("СХ123")}),
action.KeyWinCondition({keys.SHKey("SH321")}),
]
)


def test_conditions_duplicate_bonus_keys():
with pytest.raises(exceptions.LevelError):
scn.Conditions(
[
action.KeyWinCondition({keys.SHKey("SH123")}),
action.KeyBonusCondition(
{
keys.BonusKey(text="SHB1", bonus_minutes=1),
keys.BonusKey(text="SH123", bonus_minutes=-1),
}
),
action.KeyBonusCondition({keys.BonusKey(text="SHB3", bonus_minutes=0)}),
]
)


def test_conditions_duplicate_both_keys():
with pytest.raises(exceptions.LevelError):
scn.Conditions(
[
action.KeyWinCondition({keys.SHKey("SH123"), keys.SHKey("SH321")}),
action.KeyWinCondition({keys.SHKey("СХ123")}),
action.KeyBonusCondition(
{
keys.BonusKey(text="SHB1", bonus_minutes=1),
keys.BonusKey(text="СХ123", bonus_minutes=-1),
}
),
action.KeyBonusCondition({keys.BonusKey(text="SHB3", bonus_minutes=0)}),
]
)

0 comments on commit 4b138c3

Please sign in to comment.