test_data.py 7.98 KB
Newer Older
1 2
from unittest import TestCase

3 4 5
from server.model.card import Card, lowest_value_and_rest
from server.model.color import Color
from server.model.value import Value
6 7 8 9
from server.model.deck import Deck
from server.model.hand import Hand
from server.model.hands import full, brelan, pair, single, double_pair, carre
from server.model.known import ACE_OF_HEARTS, PAIR_ACE, SINGLE_ACE, DOUBLE_PAIR_ACE, BRELAN_ACE, FULL_ACE
10
from server.model.players import RandomPlayer
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34


class TestDeck(TestCase):

    def setUp(self) -> None:
        super().setUp()
        self.deck = Deck()

    def testInitDeck(self):
        self.assertEqual(52, len(self.deck), "52 cards")
        self.assertEqual(52, len(set(self.deck.cards)), "unique cards")

    def testRandomCard(self):
        cards = []
        for i in range(len(self.deck)):
            cards.append(self.deck.random_card())

        self.assertEqual(52, len(set(cards)), "draw each card once")


class TestPlayer(TestCase):

    def setUp(self) -> None:
        super().setUp()
35
        self.player = RandomPlayer()
36 37 38 39 40

    def testHand(self):
        self.assertEqual(0, len(self.player.hand), "Begin no cards")
        self.assertTrue(len(self.player.name), "Has a name")

41
        self.player.give(ACE_OF_HEARTS)
42 43 44 45 46

        self.assertEqual(1, len(self.player.hand), "Gave one card")
        self.assertEqual(Value.Ace, self.player.hand[0].value, "Is Ace")
        self.assertEqual(Color.Hearts, self.player.hand[0].color, "of Hearts")

47 48 49 50 51
    def testTricheur(self):
        with self.assertRaises(ValueError, msg="Giving someone a card they already have is an error"):
            self.player.give(ACE_OF_HEARTS)
            self.player.give(ACE_OF_HEARTS)

52 53
    def testDefeats(self):
        pass
54 55 56 57 58 59 60


class TestHand(TestCase):
    def setUp(self) -> None:
        self.hand = Hand()

    def testSimple(self):
61
        low_value, other_values = lowest_value_and_rest()
PLN (Algolia) committed
62 63 64 65 66 67

        for value in other_values:
            high_hand = single(value)
            low_hand = single(low_value)

            self.assertGreater(high_hand.value(), low_hand.value())
68 69

    def testPair(self):
70
        low_value, other_values = lowest_value_and_rest()
PLN (Algolia) committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84

        for value in other_values:
            high_hand: Hand = pair(value)
            low_hand: Hand = pair(low_value)
            single_hand: Hand = single(Value.Ace)

            high_score = high_hand.value()
            low_score = low_hand.value()
            single_score = single_hand.value()

            self.assertGreater(high_score, low_score, f"Pair[{high_hand}] > Pair[{low_hand}]]")
            self.assertGreater(high_score, single_score, f"Pair[{high_hand}] > Ace")

    def testDoublePair(self):
85
        low_value, other_values = lowest_value_and_rest()
PLN (Algolia) committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

        for value in other_values:
            high_hand: Hand = double_pair(value)
            low_hand: Hand = double_pair(low_value)
            pair_hand: Hand = PAIR_ACE
            single_hand: Hand = SINGLE_ACE

            high_score = high_hand.value()
            low_score = low_hand.value()
            pair_score = pair_hand.value()
            single_score = single_hand.value()

            # Consider case when we compare equal double pairs
            low_cards = [c.value for c in low_hand.cards]
            if all([card.value in low_cards for card in high_hand.cards]):
                self.assertEqual(high_score, low_score, f"DoublePair[{high_hand}] == DoublePair[{low_hand}]")
            else:
                self.assertGreater(high_score, low_score, f"DoublePair[{high_hand}] > DoublePair[{low_hand}]")
            self.assertGreater(high_score, pair_score, f"DoublePair[{high_hand}] > Pair[Ace]")
            self.assertGreater(high_score, single_score, f"DoublePair[{high_hand}] > Ace")

    def testBrelan(self):
108
        low_value, other_values = lowest_value_and_rest()
PLN (Algolia) committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

        for value in other_values:
            high_hand: Hand = brelan(value)
            low_hand: Hand = brelan(low_value)
            double_pair_hand: Hand = DOUBLE_PAIR_ACE
            pair_hand: Hand = PAIR_ACE
            single_hand: Hand = SINGLE_ACE

            high_score = high_hand.value()
            low_score = low_hand.value()
            double_pair_score = double_pair_hand.value()
            pair_score = pair_hand.value()
            single_score = single_hand.value()

            self.assertGreater(high_score, low_score, f"Brelan[{high_hand}] > Brelan[{low_hand}]]")
            self.assertGreater(high_score, double_pair_score, f"Brelan[{high_hand}] > Pair[Ace]")
            self.assertGreater(high_score, pair_score, f"Brelan[{high_hand}] > Pair[Ace]")
            self.assertGreater(high_score, single_score, f"Brelan[{high_hand}] > Ace")
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    def testFull(self):
        low_value, other_values = lowest_value_and_rest()

        for full_aux in other_values:  # Full_aux: brelan
            full_options = other_values.copy()
            full_options.remove(full_aux)
            full_options.append(low_value)

            for full_par in full_options:  # Full_des: paire
                high_hand: Hand = full(full_aux, full_par)
                low_hand: Hand = full(low_value, full_par)
                brelan_hand: Hand = BRELAN_ACE
                double_pair_hand: Hand = DOUBLE_PAIR_ACE
                pair_hand: Hand = PAIR_ACE
                single_hand: Hand = SINGLE_ACE

                high_score = high_hand.value()
                low_score = low_hand.value()
                brelan_score = brelan_hand.value()
                double_pair_score = double_pair_hand.value()
                pair_score = pair_hand.value()
                single_score = single_hand.value()

                self.assertGreater(high_score, low_score, f"Full[{high_hand}] > Full[{low_hand}]]")
                self.assertGreater(high_score, brelan_score, f"Full[{high_hand}] > Brelan[Ace]")
                self.assertGreater(high_score, double_pair_score, f"Full[{high_hand}] > Pair[Ace]")
                self.assertGreater(high_score, pair_score, f"Full[{high_hand}] > Pair[Ace]")
                self.assertGreater(high_score, single_score, f"Full[{high_hand}] > Ace")

    def testCarre(self):
        low_value, other_values = lowest_value_and_rest()

        for value in other_values:
            high_hand: Hand = carre(value)
            low_hand: Hand = carre(low_value)
            full_hand: Hand = FULL_ACE
            brelan_hand: Hand = BRELAN_ACE
            double_pair_hand: Hand = DOUBLE_PAIR_ACE
            pair_hand: Hand = PAIR_ACE
            single_hand: Hand = SINGLE_ACE

            high_score = high_hand.value()
            low_score = low_hand.value()
            full_score = full_hand.value()
            brelan_score = brelan_hand.value()
            double_pair_score = double_pair_hand.value()
            pair_score = pair_hand.value()
            single_score = single_hand.value()

            self.assertGreater(high_score, low_score, f"Carre[{high_hand}] > Carre[{low_hand}]]")
            self.assertGreater(high_score, full_score, f"Carre[{high_hand}] > Full[Ace]]")
            self.assertGreater(high_score, brelan_score, f"Carre[{high_hand}] > Brelan[Ace]")
            self.assertGreater(high_score, double_pair_score, f"Carre[{high_hand}] > Pair[Ace]")
            self.assertGreater(high_score, pair_score, f"Carre[{high_hand}] > Pair[Ace]")
            self.assertGreater(high_score, single_score, f"Carre[{high_hand}] > Ace")
183 184 185 186 187 188 189

    # Specifics
    def testFulls(self):
        # AssertionError: 24060 not greater than 24060 :
        # Full[Three of ♥|Three of ♣|Three of ♠|Four of ♥|Four of ♣]
        # > Full[Two of ♥|Two of ♣|Two of ♠|Four of ♥|Four of ♣]]

190
        full1 = Hand(cards=[Card(value=v) for v in [
191 192 193 194 195 196
            Value.Three,
            Value.Three,
            Value.Three,
            Value.Four,
            Value.Four,
        ]])
197
        full2 = Hand(cards=[Card(value=v) for v in [
198 199 200 201 202 203 204
            Value.Two,
            Value.Two,
            Value.Two,
            Value.Four,
            Value.Four,
        ]])
        self.assertGreater(full1.value(), full2.value(), "Full1 > full2 (threes >> twos)")