chore: Introduce card factory
This commit is contained in:
parent
6be02c447d
commit
68bfee17f1
|
@ -0,0 +1,24 @@
|
||||||
|
from typing import Any, Callable, TypeVar
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from mypy_extensions import KwArg
|
||||||
|
|
||||||
|
from poker.constants import Suit, Value
|
||||||
|
from poker.models import Card
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
Factory = Callable[[KwArg(Any)], T]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def card_factory() -> Factory[Card]:
|
||||||
|
"""Get Card object factory."""
|
||||||
|
default = {
|
||||||
|
"suit": Suit.CLUBS,
|
||||||
|
"value": Value.ACE,
|
||||||
|
}
|
||||||
|
|
||||||
|
def factory(**kwargs: Any) -> Card:
|
||||||
|
return Card(**(default | kwargs))
|
||||||
|
|
||||||
|
return factory
|
|
@ -1,28 +1,21 @@
|
||||||
import pytest
|
import pytest
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
|
|
||||||
from poker.constants import Suit, Value
|
from poker.constants import Value
|
||||||
from poker.models import Card, Hand
|
from poker.models import Card, Hand
|
||||||
|
from tests.conftest import Factory
|
||||||
|
|
||||||
|
|
||||||
def test_hand_should_contain_unique_cards() -> None:
|
def test_hand_should_contain_unique_cards(card_factory: Factory[Card]) -> None:
|
||||||
|
cards = [
|
||||||
|
Value.ACE,
|
||||||
|
Value.TWO,
|
||||||
|
Value.THREE,
|
||||||
|
Value.FOUR,
|
||||||
|
]
|
||||||
with pytest.raises(ValidationError):
|
with pytest.raises(ValidationError):
|
||||||
_ = Hand(
|
_ = Hand(
|
||||||
cards=(
|
cards=tuple(card_factory(value=value) for value in cards + [Value.FOUR])
|
||||||
Card(suit=Suit.CLUBS, value=Value.ACE),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.TWO),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.THREE),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.FOUR),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.FOUR),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
_ = Hand(
|
_ = Hand(cards=tuple(card_factory(value=value) for value in cards + [Value.FIVE]))
|
||||||
cards=(
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.ACE),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.TWO),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.THREE),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.FOUR),
|
|
||||||
Card(suit=Suit.CLUBS, value=Value.FIVE),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue