feat: Ensure hand comprises unique cards
This commit is contained in:
parent
c337984965
commit
6be02c447d
|
@ -1,4 +1,4 @@
|
|||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, validator
|
||||
|
||||
from poker.constants import Suit, Value
|
||||
|
||||
|
@ -9,8 +9,22 @@ class Card(BaseModel):
|
|||
suit: Suit
|
||||
value: Value
|
||||
|
||||
def __hash__(self) -> int:
|
||||
"""Hash function."""
|
||||
return hash(f"{self.value} {self.suit}")
|
||||
|
||||
|
||||
HandType = tuple[Card, Card, Card, Card, Card]
|
||||
|
||||
|
||||
class Hand(BaseModel):
|
||||
"""Hand domain model class."""
|
||||
|
||||
cards: list[Card] = Field(..., min_length=5, max_length=5)
|
||||
cards: HandType
|
||||
|
||||
@validator("cards")
|
||||
def validate_unique(cls, cards: HandType) -> HandType:
|
||||
"""Validate hand comprises unique cards."""
|
||||
if len(cards) != len(set(cards)):
|
||||
raise ValueError("Hand contains duplicate cards.")
|
||||
return cards
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from poker.constants import Suit, Value
|
||||
from poker.models import Card, Hand
|
||||
|
||||
|
||||
def test_hand_should_contain_unique_cards() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
_ = Hand(
|
||||
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.FOUR),
|
||||
)
|
||||
)
|
||||
|
||||
_ = Hand(
|
||||
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