Skip to content

Commit

Permalink
Merge pull request #332 from Whist-Team/fix/card_dict_conversion
Browse files Browse the repository at this point in the history
FIX: card dictionary
  • Loading branch information
iTitus authored Jul 25, 2023
2 parents c028e6c + f97f915 commit 4cdb75e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
5 changes: 4 additions & 1 deletion tests/whist_core/cards/test_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ def test_str(self):
def test_dict(self):
self.assertEqual({'suit': 'hearts', 'rank': 'ace'}, self.card.dict())

def test_dump(self):
self.assertEqual({'suit': 'hearts', 'rank': 'ace'}, self.card.model_dump())

def test_json(self):
self.assertEqual({'suit': 'hearts', 'rank': 'ace'}, json.loads(self.card.json()))
self.assertEqual({'suit': 'hearts', 'rank': 'ace'}, json.loads(self.card.model_dump_json()))

def test_constructor_with_enum_as_str(self):
self.assertEqual(self.card, Card(suit='hearts', rank='ace'))
Expand Down
31 changes: 21 additions & 10 deletions tests/whist_core/cards/test_card_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ def setUp(self) -> None:
self.ace_hearts = Card(suit=Suit.HEARTS, rank=Rank.A)
self.seven_clubs = Card(suit=Suit.CLUBS, rank=Rank.NUM_7)
self.ten_diamonds = Card(suit=Suit.DIAMONDS, rank=Rank.NUM_10)
self.cc4 = OrderedCardContainer.with_cards(self.king_hearts, self.ace_hearts, self.seven_clubs, self.ten_diamonds)
self.cc4 = OrderedCardContainer.with_cards(self.king_hearts, self.ace_hearts,
self.seven_clubs, self.ten_diamonds)

def test_not_equal(self):
first = OrderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_2), Card(suit=Suit.HEARTS, rank=Rank.NUM_4))
second = OrderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_4), Card(suit=Suit.HEARTS, rank=Rank.NUM_2))
first = OrderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_2),
Card(suit=Suit.HEARTS, rank=Rank.NUM_4))
second = OrderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_4),
Card(suit=Suit.HEARTS, rank=Rank.NUM_2))
self.assertNotEqual(first, second)

def test_empty_manual(self):
Expand All @@ -39,11 +42,11 @@ def test_pop_random(self):

def test_json_empty(self):
cc = OrderedCardContainer.empty()
self.assertEqual({'cards': []}, json.loads(cc.json()))
self.assertEqual({'cards': []}, json.loads(cc.model_dump_json()))

def test_json_empty_load(self):
cc = OrderedCardContainer.empty()
self.assertEqual(cc, OrderedCardContainer(**json.loads(cc.json())))
self.assertEqual(cc, OrderedCardContainer(**json.loads(cc.model_dump_json())))

def test_json_some_cards(self):
cc = OrderedCardContainer.with_cards(
Expand All @@ -53,18 +56,24 @@ def test_json_some_cards(self):
self.assertEqual({'cards': [
{'suit': 'hearts', 'rank': '2'},
{'suit': 'hearts', 'rank': '4'}
]}, json.loads(cc.json()))
]}, json.loads(cc.model_dump_json()))

def test_json_some_cards_load(self):
cc = OrderedCardContainer.with_cards(
Card(suit=Suit.HEARTS, rank=Rank.NUM_2),
Card(suit=Suit.HEARTS, rank=Rank.NUM_4)
)
self.assertEqual(cc, OrderedCardContainer(**json.loads(cc.json())))
self.assertEqual(cc, OrderedCardContainer(**json.loads(cc.model_dump_json())))

def test_json_full_load(self):
cc = OrderedCardContainer.full()
self.assertEqual(cc, OrderedCardContainer(**json.loads(cc.json())))
self.assertEqual(cc, OrderedCardContainer(**json.loads(cc.model_dump_json())))

def test_dump_string_conversion(self):
cc = OrderedCardContainer.with_cards(self.spades_king).model_dump(mode='json')
card = cc['cards'][0]
self.assertEqual(str(self.spades_king.rank), card['rank'])
self.assertEqual(str(self.spades_king.suit), card['suit'])

def test_contains(self):
cc = OrderedCardContainer.with_cards(self.spades_king)
Expand Down Expand Up @@ -165,8 +174,10 @@ def setUp(self) -> None:
self.spades_king = Card(suit=Suit.SPADES, rank=Rank.K)

def test_equal(self):
first = UnorderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_2), Card(suit=Suit.HEARTS, rank=Rank.NUM_4))
second = UnorderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_4), Card(suit=Suit.HEARTS, rank=Rank.NUM_2))
first = UnorderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_2),
Card(suit=Suit.HEARTS, rank=Rank.NUM_4))
second = UnorderedCardContainer.with_cards(Card(suit=Suit.HEARTS, rank=Rank.NUM_4),
Card(suit=Suit.HEARTS, rank=Rank.NUM_2))
self.assertEqual(first, second)

def test_not_equal(self):
Expand Down
9 changes: 9 additions & 0 deletions whist_core/cards/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from functools import total_ordering
from typing import Any, Optional, Iterator

import deprecation
from pydantic import BaseModel

from whist_core.util import enforce_str_on_dict
Expand Down Expand Up @@ -135,13 +136,21 @@ def name(self) -> str:
"""
return f'{self.rank} of {self.suit}'

@deprecation.deprecated("Use model_dump instead. Will be removed in V1.")
def dict(self, *args, **kwargs):
"""
Returns the dictionary. See BaseModel for details.
"""
super_dict = super().model_dump(*args, **kwargs)
return enforce_str_on_dict(super_dict, ('suit', 'rank'))

def model_dump(self, *args, **kwargs):
"""
Returns the dictionary. See BaseModel for details.
"""
super_dict = super().model_dump(*args, **kwargs)
return enforce_str_on_dict(super_dict, ('suit', 'rank'))

def __lt__(self, other: Any) -> bool:
"""Checks if the other card is lower than this card."""
if self.__class__ is other.__class__:
Expand Down

0 comments on commit 4cdb75e

Please sign in to comment.