From 86085090c32ea34216f1ce8a2b7a89e6818d6f8a Mon Sep 17 00:00:00 2001 From: bomzheg Date: Thu, 7 Nov 2024 21:43:30 +0300 Subject: [PATCH] #106 added few more tests --- shvatka/core/models/dto/scn/level.py | 7 +-- tests/unit/domain/conditions_test.py | 88 ++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/shvatka/core/models/dto/scn/level.py b/shvatka/core/models/dto/scn/level.py index 3e7bcd87..53819188 100644 --- a/shvatka/core/models/dto/scn/level.py +++ b/shvatka/core/models/dto/scn/level.py @@ -1,5 +1,4 @@ import logging -from abc import abstractmethod from collections.abc import Sequence, Iterable from dataclasses import dataclass from datetime import timedelta @@ -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") @@ -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] diff --git a/tests/unit/domain/conditions_test.py b/tests/unit/domain/conditions_test.py index 0ea91b38..c4648389 100644 --- a/tests/unit/domain/conditions_test.py +++ b/tests/unit/domain/conditions_test.py @@ -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 @@ -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)}), + ] + )