def test_websockets_consumers_handlers(self): class WebsocketConsumer(websockets.WebsocketConsumer): def connect(self, message, **kwargs): self.called = 'connect' self.id = kwargs['id'] def disconnect(self, message, **kwargs): self.called = 'disconnect' def receive(self, text=None, bytes=None, **kwargs): self.text = text with apply_routes([route_class(WebsocketConsumer, path='/path/(?P<id>\d+)')]): client = Client() consumer = client.send_and_consume('websocket.connect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'connect') self.assertEqual(consumer.id, '1') consumer = client.send_and_consume('websocket.receive', {'path': '/path/1', 'text': 'text'}) self.assertEqual(consumer.text, 'text') consumer = client.send_and_consume('websocket.disconnect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'disconnect')
def test_websockets_consumers_handlers(self): class WebsocketConsumer(websockets.WebsocketConsumer): def connect(self, message, **kwargs): self.called = 'connect' self.id = kwargs['id'] def disconnect(self, message, **kwargs): self.called = 'disconnect' def receive(self, text=None, bytes=None, **kwargs): self.text = text with apply_routes( [route_class(WebsocketConsumer, path='/path/(?P<id>\d+)')]): client = Client() consumer = client.send_and_consume('websocket.connect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'connect') self.assertEqual(consumer.id, '1') consumer = client.send_and_consume('websocket.receive', { 'path': '/path/1', 'text': 'text' }) self.assertEqual(consumer.text, 'text') consumer = client.send_and_consume('websocket.disconnect', {'path': '/path/1'}) self.assertEqual(consumer.called, 'disconnect')
def test_ai_play(self): room = new_room_data( room_id='test', player_number=5, ) for i in range(5): nickname = ['doge', 'bitcoin', 'ethereum', 'egger', 'ha'] create_user(username='******'.format(nickname[i]), password='******', nickname='*AI-{}'.format(nickname[i]), email='*****@*****.**') for i in range(5): room['players'].append(AI(i)) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) client = Client() client.send_and_consume('gameplay-start', {'room_id': 'test'}) room = cache.get('room:test') while room['game']['state'] is RoomState.BIDDING: client.consume('gameplay-bid', fail_on_none=False) self.flush_ai() room = cache.get('room:test') president = room['game']['president'] client.consume('gameplay-friend-select') room = cache.get('room:test') for _ in range(50): client.consume('gameplay-play', fail_on_none=False) self.flush_ai() room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.RESULT) history = GameHistory.objects.all()[0] self.assertEqual(president, history.president.username)
def flush_ai(self): client = Client() while True: try: client.consume('gameplay-ai') except Exception as e: break
def test_base_consumer(self): class Consumers(BaseConsumer): method_mapping = { 'test.create': 'create', 'test.test': 'test', } def create(self, message, **kwargs): self.called = 'create' def test(self, message, **kwargs): self.called = 'test' with apply_routes([route_class(Consumers)]): client = Client() # check that methods for certain channels routes successfully self.assertEqual( client.send_and_consume('test.create').called, 'create') self.assertEqual( client.send_and_consume('test.test').called, 'test') # send to the channels without routes client.send('test.wrong') message = self.get_next_message('test.wrong') self.assertEqual(client.channel_layer.router.match(message), None) client.send('test') message = self.get_next_message('test') self.assertEqual(client.channel_layer.router.match(message), None)
def test_websockets_consumer(self): # Create a channels test client client = Client() # Send a websocket.connect message, passing in the path so it maps to the right consumer via the routing in routing.py # Consume portion maps that message to a consumer and runs and returns an instance of the consumer connect_consumer = client.send_and_consume('websocket.connect', {'path': '/game/123/'}) connect_reply = client.receive( ) # receive() grabs the content of the next message off of the client's reply_channel self.assertEqual( connect_reply, {'accept': True }) # websocket.connect should return acceptance of connection receive_consumer = client.send_and_consume('websocket.receive', { 'path': '/game/123/', 'text': 'text' }) receive_reply = client.receive( ) # receive() grabs the content of the next message off of the client's reply_channel self.assertEqual(receive_reply, {'text': 'textreply'}) receive_consumer.group_send( '123', text='grouptext' ) # This sends a message out to a group - shortcut off of the Websocket Consumer group_reply = client.receive( ) # receive() grabs the content of the next message off of the client's reply_channel self.assertEqual(group_reply, {'text': 'grouptext'}) disconnect_consumer = client.send_and_consume('websocket.disconnect', {'path': '/game/123/'}) disconnect_consumer.close()
def test_close_session(self, mock_callback): """Check that 'close_session' is called at disconnect.""" client = Client() client.send_and_consume('websocket.connect', {}) message_content = {'text': json.dumps({"token": self.TEST_TOKEN})} client.send_and_consume('websocket.receive', message_content) client.send_and_consume('websocket.disconnect', {}) mock_callback.assert_called_with(self.TEST_TOKEN)
def test_deal_miss(self): # set up initial room state room = new_room_data( room_id='test', player_number=5, ) for i in range(5): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) self.deal( 'doge0', ['SA', 'JK', 'S10', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8']) self.deal_miss('doge0') room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.NOT_PLAYING) from websocket.consumers.gameplay_consumers import gameplay_start_consumer message = self.get_next_message('gameplay-start', require=True) gameplay_start_consumer(message) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.BIDDING)
def test_final_bid(self): # set up initial room state room = new_room_data( room_id='test', player_number=5, ) for i in range(5): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) self.bid('doge0', 0, 'N', False) self.bid('doge1', 0, 'N', False) self.bid('doge2', 0, 'N', False) self.bid('doge3', 0, 'N', False) self.bid('doge4', 13, 'S', True) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.FRIEND_SELECTING) self.assertEqual(room['game']['president'], 'doge4')
def test_base_consumer(self): class Consumers(BaseConsumer): method_mapping = { 'test.create': 'create', 'test.test': 'test', } def create(self, message, **kwargs): self.called = 'create' def test(self, message, **kwargs): self.called = 'test' with apply_routes([route_class(Consumers)]): client = Client() # check that methods for certain channels routes successfully self.assertEqual(client.send_and_consume('test.create').called, 'create') self.assertEqual(client.send_and_consume('test.test').called, 'test') # send to the channels without routes client.send('test.wrong') message = self.get_next_message('test.wrong') self.assertEqual(client.channel_layer.router.match(message), None) client.send('test') message = self.get_next_message('test') self.assertEqual(client.channel_layer.router.match(message), None)
def test_as_route_method(self): class WebsocketConsumer(BaseConsumer): trigger = 'new' def test(self, message, **kwargs): self.message.reply_channel.send({'trigger': self.trigger}) method_mapping = {'mychannel': 'test'} with apply_routes([ WebsocketConsumer.as_route( {'method_mapping': method_mapping, 'trigger': 'from_as_route'}, name='filter', ), ]): client = Client() client.send_and_consume('mychannel', {'name': 'filter'}) self.assertEqual(client.receive(), {'trigger': 'from_as_route'})
def test_start_consumer(self): # set up initial room state room = new_room_data( room_id='test', player_number=5, ) for i in range(5): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) cache.set('room:test', room) client = Client() content = { 'room_id': 'test', } client.send_and_consume('gameplay-start', content) self.flush_all() room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.BIDDING) for player in room['players']: self.assertEqual(len(player['cards']), 10) self.assertEqual(len(room['game']['floor_cards']), 3)
def test_round_friend(self): # set up initial room state room = new_room_data( room_id='test', player_number=5, ) for i in range(5): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) self.floor_card(['SJ', 'C9', 'JK']) self.deal('doge0', ['SA', 'SQ', 'S2']) self.bid('doge0', 13, 'S', True) self.bid('doge1', 0, '', False) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.BIDDING) self.bid('doge2', 0, '', False) self.bid('doge3', 0, '', False) self.bid('doge4', 0, '', False) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.FRIEND_SELECTING) self.friend_select('doge0', { 'type': 'round', 'round': 1, 'floor_cards': ['SA', 'SQ', 'S2'], }) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.PLAYING) self.deal('doge0', ['C2']) self.deal('doge1', ['C5']) self.deal('doge2', ['CK']) self.deal('doge3', ['C3']) self.deal('doge4', ['C9']) self.play('doge0', 'C2') self.play('doge1', 'C5') self.play('doge2', 'CK') self.play('doge3', 'C3') self.play('doge4', 'C9') room = cache.get('room:test') self.assertEqual(room['game']['friend'], 'doge2')
def test_six_mighty_president_kill(self): # set up initial room state room = new_room_data( room_id='test', player_number=6, ) for i in range(6): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) self.floor_card(['CK', 'JK', 'H6', 'S6', 'C3']) self.deal('doge0', ['HA', 'SQ', 'H4', 'CQ', 'H8', 'DQ', 'D8', 'S3']) self.deal('doge1', ['S2', 'S7', 'C2', 'H5', 'CA', 'S10', 'HJ', 'D5']) self.deal('doge2', ['D6', 'DK', 'S9', 'DJ', 'S4', 'C5', 'C7', 'D4']) self.deal('doge3', ['C10', 'D7', 'H9', 'C8', 'DA', 'H3', 'SK', 'S8']) self.deal('doge4', ['SJ', 'C9', 'D2', 'S5', 'C4', 'H2', 'HQ', 'H7']) self.deal('doge5', ['D3', 'CJ', 'HK', 'D10', 'SA', 'C6', 'H10', 'D9']) self.bid('doge0', 14, 'H', True) self.bid('doge1', 0, '', False) self.bid('doge2', 0, '', False) self.bid('doge3', 0, '', False) self.bid('doge4', 0, '', False) self.bid('doge5', 0, '', False) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.KILL_SELECTING) # president kill self.kill('doge0', 'JK') room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.BIDDING) self.bid('doge1', 12, 'N', True) self.bid('doge2', 0, '', False) self.bid('doge3', 0, '', False) self.bid('doge4', 0, '', False) self.bid('doge5', 0, '', False) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.FRIEND_SELECTING)
def test_websockets_decorators(self): class WebsocketConsumer(websockets.WebsocketConsumer): strict_ordering = True def connect(self, message, **kwargs): self.order = message['order'] with apply_routes([route_class(WebsocketConsumer, path='/path')]): client = Client() client.send('websocket.connect', {'path': '/path', 'order': 1}) client.send('websocket.connect', {'path': '/path', 'order': 0}) client.consume('websocket.connect') self.assertEqual(client.consume('websocket.connect').order, 0) self.assertEqual(client.consume('websocket.connect').order, 1)
def test_play_exceptions(self): # set up initial room state room = new_room_data( room_id='test', player_number=5, ) for i in range(5): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) self.floor_card(['C2', 'C3', 'C4']) self.deal( 'doge0', ['JK', 'SK', 'S10', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8']) self.bid('doge0', 13, 'S', True) self.bid('doge1', 0, '', False) self.bid('doge2', 0, '', False) self.bid('doge3', 0, '', False) self.bid('doge4', 0, '', False) self.friend_select('doge0', { 'type': 'round', 'round': 2, 'floor_cards': ['C2', 'C3', 'C4'], }) self.play('doge0', 'SK') room = cache.get('room:test') self.assertEqual(len(room['game']['table_cards']), 0) self.play('doge0', 'JK', joker_suit='S') room = cache.get('room:test') self.assertEqual(len(room['game']['table_cards']), 1) room['game']['turn'] = 0 room['game']['table_cards'] = [] cache.set('room:test', room) self.deal( 'doge0', ['SA', 'SK', 'S10', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8']) self.play('doge0', 'S2') room = cache.get('room:test') self.assertEqual(len(room['game']['table_cards']), 1) room['game']['turn'] = 0 room['game']['round'] = 9 room['game']['table_cards'] = [] cache.set('room:test', room) self.deal('doge0', ['JK', 'DA']) self.play('doge0', 'DA') room = cache.get('room:test') self.assertEqual(len(room['game']['table_cards']), 0) self.play('doge0', 'JK', joker_suit='S') room = cache.get('room:test') self.assertEqual(len(room['game']['table_cards']), 1)
def drain(self): """Drain the control channel without acting on anything.""" client = Client() while client.get_next_message( state.MANAGER_CONTROL_CHANNEL) is not None: pass
def test_record_one(self): for i in range(5): create_user(username='******'.format(i), password='******', nickname='nick{}'.format(i), email='*****@*****.**') # set up initial room state room = new_room_data( room_id='test', player_number=5, ) for i in range(5): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) self.floor_card(['SJ', 'C9', 'JK']) self.deal( 'doge0', ['SA', 'SQ', 'S2', 'HJ', 'D6', 'DK', 'C10', 'D7', 'C7', 'D4']) self.deal( 'doge1', ['SK', 'S8', 'H3', 'C4', 'H10', 'D8', 'S5', 'HK', 'C6', 'HQ']) self.deal( 'doge2', ['CQ', 'H8', 'S7', 'S3', 'HA', 'CA', 'S10', 'D9', 'DQ', 'S4']) self.deal('doge3', ['C2', 'H5', 'S9', 'DJ', 'H9', 'C8', 'D2', 'C5', 'DA', 'H4']) self.deal( 'doge4', ['H2', 'H7', 'D3', 'CJ', 'CK', 'D10', 'H6', 'S6', 'C3', 'D5']) self.bid('doge0', 13, 'S', True) self.bid('doge1', 0, '', False) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.BIDDING) self.bid('doge2', 0, '', False) self.bid('doge3', 0, '', False) self.bid('doge4', 0, '', False) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.FRIEND_SELECTING) self.friend_select('doge0', { 'type': 'card', 'card': 'DA', 'floor_cards': ['HJ', 'C10', 'C7'], }) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.PLAYING) # round 1 self.play('doge0', 'DK') self.play('doge1', 'D8') self.play('doge2', 'D9') self.play('doge3', 'DJ') self.play('doge4', 'D3') # round 2 self.play('doge0', 'SA') self.play('doge1', 'S5') self.play('doge2', 'S3') self.play('doge3', 'S9') self.play('doge4', 'S6') # round 3 self.play('doge0', 'JK', joker_suit='S') self.play('doge1', 'S8') self.play('doge2', 'S7') self.play('doge3', 'C2') self.play('doge4', 'C3') # round 4 self.play('doge0', 'S2') self.play('doge1', 'SK') self.play('doge2', 'S4') self.play('doge3', 'DA') self.play('doge4', 'H2') # round 5 self.play('doge3', 'D2') self.play('doge4', 'D10') self.play('doge0', 'D4') self.play('doge1', 'H10') self.play('doge2', 'DQ') # round 6 self.play('doge2', 'CA') self.play('doge3', 'C5') self.play('doge4', 'CJ') self.play('doge0', 'C9') self.play('doge1', 'C4') # round 7 self.play('doge2', 'HA') self.play('doge3', 'H4') self.play('doge4', 'H6') self.play('doge0', 'SJ') self.play('doge1', 'H3') # round 8 self.play('doge0', 'SQ') self.play('doge1', 'C6') self.play('doge2', 'S10') self.play('doge3', 'H5') self.play('doge4', 'H7') # round 9 self.play('doge0', 'D7') self.play('doge1', 'HQ') self.play('doge2', 'H8') self.play('doge3', 'H9') self.play('doge4', 'D5') # final round self.play('doge0', 'D6') self.play('doge1', 'HK') self.play('doge2', 'CQ') self.play('doge3', 'C8') self.play('doge4', 'CK') ### room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.RESULT) self.assertEqual(room['players'][0]['username'], 'doge3') history = GameHistory.objects.get(id=1) self.assertEqual(history.bid, 13) self.assertEqual(history.giruda, 'S') self.assertEqual(history.president.username, 'doge0') self.assertEqual(history.friend.username, 'doge3') doge2 = User.objects.get(username='******') self.assertEqual(doge2.game_histories.all()[0], history) self.game_continue('doge0', True) self.game_continue('doge0', True) self.game_continue('doge1', True) self.game_continue('doge2', True) self.game_continue('doge3', True) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.RESULT) self.game_continue('doge4', True) from websocket.consumers.gameplay_consumers import gameplay_start_consumer message = self.get_next_message('gameplay-start') gameplay_start_consumer(message) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.BIDDING)
def test_record_six_mighty(self): # set up initial room state room = new_room_data( room_id='test', player_number=6, ) for i in range(6): client = Client() username = '******'.format(i) player_data = new_player_data( username=username, reply=client.reply_channel, ready=True, ) room['players'].append(player_data) cache.set('player-room:' + username, 'test') self.clients.append(client) room['game']['state'] = RoomState.BIDDING cache.set('room:test', room) self.floor_card(['CK', 'D10', 'H6', 'S6', 'C3']) self.deal('doge0', ['HA', 'SQ', 'H4', 'CQ', 'H8', 'DQ', 'D8', 'S3']) self.deal('doge1', ['S2', 'S7', 'C2', 'H5', 'CA', 'S10', 'HJ', 'D5']) self.deal('doge2', ['D6', 'DK', 'S9', 'DJ', 'S4', 'C5', 'C7', 'D4']) self.deal('doge3', ['C10', 'D7', 'H9', 'C8', 'DA', 'H3', 'SK', 'S8']) self.deal('doge4', ['SJ', 'C9', 'D2', 'S5', 'C4', 'H2', 'HQ', 'H7']) self.deal('doge5', ['D3', 'CJ', 'HK', 'JK', 'SA', 'C6', 'H10', 'D9']) self.bid('doge0', 14, 'H', True) self.bid('doge1', 0, '', False) self.bid('doge2', 15, 'D', True) self.bid('doge3', 0, '', False) self.bid('doge4', 0, '', False) self.bid('doge5', 0, '', False) self.bid('doge0', 0, '', False) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.KILL_SELECTING) # kill doge3 self.kill('doge2', 'DA') room = cache.get('room:test') self.assertEqual(room['game']['killed_player']['username'], 'doge3') self.assertIs(room['game']['state'], RoomState.FRIEND_SELECTING) # set kill-deal to our prebuilt data self.deal('doge0', ['HA', 'SQ', 'H4', 'CQ', 'H8', 'DQ', 'D8', 'S3', 'H3', 'CK']) self.deal( 'doge1', ['S2', 'S7', 'C2', 'H5', 'CA', 'S10', 'HJ', 'D5', 'DA', 'C3']) self.deal('doge2', [ 'D6', 'DK', 'S9', 'DJ', 'S4', 'C5', 'C7', 'D4', 'D10, D7', 'C10', 'S6', 'C8' ]) self.deal('doge4', ['SJ', 'C9', 'D2', 'S5', 'C4', 'H2', 'HQ', 'H7', 'SK', 'H6']) self.deal( 'doge5', ['D3', 'CJ', 'HK', 'JK', 'SA', 'C6', 'H10', 'D9', 'S8', 'H9']) self.friend_select('doge2', { 'type': 'card', 'card': 'SA', 'floor_cards': ['S9', 'S4', 'S6'], }) room = cache.get('room:test') self.assertIs(room['game']['state'], RoomState.PLAYING) # round 1 self.play('doge2', 'C10') self.play('doge4', 'C4') self.play('doge5', 'C6') self.play('doge0', 'CQ') self.play('doge1', 'CA') # round 2 self.play('doge1', 'C3', joker_call=True) self.play('doge2', 'C8') self.play('doge4', 'C9') self.play('doge5', 'SA') self.play('doge0', 'CK')