Example #1
0
    def test_get_card_success(self):
        state_obj = main.State()
        user_obj = main.User("myusername", "mypassword", main.School.query.get(1))
        main.db.session.add_all((
            state_obj,
            user_obj,
            main.Card(user_obj, 'q1', 'a1'),
            main.Card(user_obj, 'q2', 'a2'),
        ))
        card_obj_1 = main.Card(user_obj, 'q3', 'a3')
        card_obj_2 = main.Card(user_obj, 'q4', 'a4')
        main.db.session.add_all((
            card_obj_1,
            card_obj_2,
            main.Card(user_obj, 'q5', 'a5')
        ))
        state_obj.user = user_obj
        state_obj._State__current_card_seed = 608731  # [3, 4, 5, 2, 1]
        main.db.session.commit()
        print(card_obj_1.id, card_obj_2.id)

        with self.subTest('get first card'):
            with self.client as c:
                c.set_cookie('localhost', 'game-state', state_obj.id)
                response = c.get(flask.url_for('get_card_api', n=1))
                self.assert200(response)
                self.assertEqual(response.json['id'], card_obj_1.id)

        with self.subTest('get current card'):
            with self.client as c:
                c.set_cookie('localhost', 'game-state', state_obj.id)
                response = c.get(flask.url_for('get_card_api', n=0))
                self.assert200(response)
                self.assertEqual(response.json['id'], card_obj_1.id)

        with self.subTest('get next card'):
            with self.client as c:
                c.set_cookie('localhost', 'game-state', state_obj.id)
                response = c.get(flask.url_for('get_card_api', n=1))
                self.assert200(response)
                self.assertEqual(response.json['id'], card_obj_2.id)

        with self.subTest('get current card (next)'):
            with self.client as c:
                c.set_cookie('localhost', 'game-state', state_obj.id)
                response = c.get(flask.url_for('get_card_api', n=0))
                self.assert200(response)
                self.assertEqual(response.json['id'], card_obj_2.id)

        with self.subTest('overflow iterator'):
            state_obj._State__current_card_iter = 5
            main.db.session.commit()
            with self.client as c:
                c.set_cookie('localhost', 'game-state', state_obj.id)
                response = c.get(flask.url_for('get_card_api', n=1))
                self.assert200(response)
                self.assertIn(response.json['id'], [i.id for i in main.Card.query.all()])
Example #2
0
    def test_play_success(self):
        state_obj = main.State()
        user_obj = main.User("myusername", "mypassword", main.School.query.get(1))
        main.db.session.add_all((state_obj, user_obj,
                                 main.Card(user_obj, "q1", "a1"),
                                 main.Card(user_obj, "q2", "a2")))
        state_obj.user = user_obj
        main.db.session.commit()

        with self.client as c:
            c.set_cookie('localhost', 'game-state', state_obj.id)
            response = c.get(flask.url_for('play'))
        self.assert200(response)
Example #3
0
    def test_state_card(self):
        state_obj = main.State()
        user_obj = main.User("myusername", "mypassword", main.School.query.get(1))
        main.db.session.add_all((state_obj, user_obj))

        state_obj.user = user_obj
        state_obj._State__current_card_seed = 259720  # [2, 3, 4, 1]
        main.db.session.add(main.Card(state_obj.user, "card 1", "1"))
        card_2 = main.Card(state_obj.user, "card 2", "2")
        main.db.session.add(card_2)
        main.db.session.add(main.Card(state_obj.user, "card 3", "3"))
        main.db.session.add(main.Card(state_obj.user, "card 4", "4"))

        main.db.session.commit()

        self.assertIs(state_obj.next_card(), card_2)
        self.assertIsNotNone(state_obj._State__current_card_iter)
        self.assertIs(state_obj.card, card_2)
Example #4
0
class TestCard(unittest.TestCase):

    card = main.Card('6', '♠')

    def test_str(self):
        self.assertEqual(str(self.card), '6♠')

    def test_get_int_value(self):

        self.assertEqual(self.card.get_int_value(), 6)
Example #5
0
    def test_answer_missing_arg(self):
        state_obj = main.State()
        user_obj = main.User("myusername", "mypassword", main.School.query.get(1))
        card_obj = main.Card(user_obj, 'q1', 'a1')
        main.db.session.add_all((state_obj, user_obj, card_obj))
        state_obj.user = user_obj
        state_obj._State__current_card_iter = 0
        main.db.session.commit()

        with self.client as c:
            c.set_cookie('localhost', 'game-state', state_obj.id)
            response = c.post(flask.url_for('answer_card_api'))
            self.assert400(response)
Example #6
0
    def test_remove_card_get_fail(self):
        state_obj = main.State()
        user_obj = main.User("myusername", "mypassword", main.School.query.get(1))
        card_obj = main.Card(user_obj, 'q1', 'a1')
        main.db.session.add_all((state_obj, user_obj, card_obj))
        state_obj.user = user_obj
        main.db.session.commit()

        with self.client as c:
            c.set_cookie('localhost', 'game-state', state_obj.id)
            response = c.get(flask.url_for('remove_card_api'), data={'id': card_obj.id})
        self.assert405(response)
        self.assertEqual(main.Card.query.filter_by(user=user_obj).count(), 1)
Example #7
0
def test_sort_all_card(sorted_ranks):
    sorted_deck = main.Deck()
    sorted_cards = [
        main.Card(suit, rank) for suit in main.Suit for rank in sorted_ranks
    ]
    sorted_deck.cards = sorted_cards

    deck = main.Deck()
    deck.sort([
        main.Suit.Spades, main.Suit.Diamonds, main.Suit.Hearts, main.Suit.Clubs
    ])

    deck.print()
    assert sorted_deck == deck, "test sort all cards failed"
Example #8
0
def test_sort_example_failed():
    deck = main.Deck()
    result_deck = main.Deck()
    deck.cards = [
        main.Card(main.Suit.Spades, main.Rank._2),
        main.Card(main.Suit.Diamonds, main.Rank._5),
        main.Card(main.Suit.Spades, main.Rank.King),
        main.Card(main.Suit.Hearts, main.Rank._3),
        main.Card(main.Suit.Clubs, main.Rank.Ace)
    ]

    deck.sort([
        main.Suit.Spades, main.Suit.Diamonds, main.Suit.Hearts, main.Suit.Clubs
    ])

    result_deck.cards = [
        main.Card(main.Suit.Spades, main.Rank._2),
        main.Card(main.Suit.Diamonds, main.Rank._5),
        main.Card(main.Suit.Spades, main.Rank.King),
        main.Card(main.Suit.Hearts, main.Rank._3),
        main.Card(main.Suit.Clubs, main.Rank.Ace)
    ]

    assert result_deck != deck, "test sort example failed"
Example #9
0
    def test_answer_card_success(self):
        state_obj = main.State()
        user_obj = main.User("myusername", "mypassword", main.School.query.get(1))
        card_obj = main.Card(user_obj, 'q1', 'a1')
        main.db.session.add_all((state_obj, user_obj, card_obj))
        state_obj.user = user_obj
        state_obj._State__current_card_iter = 0
        main.db.session.commit()

        with self.client as c:
            c.set_cookie('localhost', 'game-state', state_obj.id)
            response = c.post(flask.url_for('answer_card_api'), data={'a': 'a1'})
            self.assert200(response)
            self.assertTrue(response.json['correct'])
            self.assertIn('score', response.json)
Example #10
0
def test_second_player_wins():
    player1 = main.Player("Alice")
    player2 = main.Player("Bob")

    player1.take_card(main.Card(main.Suit.Spades, main.Rank._2))
    player1.take_card(main.Card(main.Suit.Diamonds, main.Rank._5))
    player1.take_card(main.Card(main.Suit.Spades, main.Rank.King))

    player2.take_card(main.Card(main.Suit.Diamonds, main.Rank._2))
    player2.take_card(main.Card(main.Suit.Diamonds, main.Rank._4))
    player2.take_card(main.Card(main.Suit.Spades, main.Rank.King))

    game = main.Game()
    winner = game.how_wins(player1, player2)

    winner.print()

    assert winner == player2, "test second player wins"
Example #11
0
 def test_hand_total_with_ace_as_one(self):
     self.hand.hand =[main.Card('10', '♥'), main.Card('2', '♥'), main.Card('A', '♥')]
     self.assertEqual(self.hand.hand_total(), 13)
Example #12
0
 def test_hand_total_with_ace_as_eleven(self):
     self.hand.hand = [main.Card('A', '♥')]
     self.assertEqual(self.hand.hand_total(), 11)
Example #13
0
 def test_hand_total_with_picture_card(self):
     self.hand.hand = [main.Card('J', '♦')]
     self.assertEqual(self.hand.hand_total(), 10)
Example #14
0
 def setUp(self):
     self.hand = main.Hand()
     self.hand.hand = [main.Card('2', '♠'), main.Card('3', '♠'), main.Card('4', '♠')]
     self.deck = main.Deck()