Example #1
0
 def test_responder_hands_what_it_got_from_receive_to_handler(self):
     self.transmitter.receive = mock.MagicMock(return_value="for the handler")
     handler = ResponseHandler(MoveGenerator())
     handler.response = mock.Mock()
     responder = Responder(self.transmitter,handler)
     responder.respond()
     handler.response.assert_called_with("for the handler")
 def test_returns_board_with_obvious_move(self):
     transmitter = mock.Mock()
     transmitter.receive = mock.MagicMock(return_value=self.easy_win_possible)
     generator = MoveGenerator()
     handler = ResponseHandler(generator)          
     responder = Responder(transmitter,handler)
     responder.respond()
     transmitter.send.called_with( {"winner_after_ai_move":"o","ai_move":3} )
Example #3
0
 def test_responder_hands_what_it_got_from_receive_to_handler(self):
     self.transmitter.receive = mock.MagicMock(
         return_value="for the handler")
     handler = ResponseHandler(MoveGenerator())
     handler.response = mock.Mock()
     responder = Responder(self.transmitter, handler)
     responder.respond()
     handler.response.assert_called_with("for the handler")
 def test_returns_board_with_obvious_move(self):
     transmitter = mock.Mock()
     transmitter.receive = mock.MagicMock(
         return_value=self.easy_win_possible)
     generator = MoveGenerator()
     handler = ResponseHandler(generator)
     responder = Responder(transmitter, handler)
     responder.respond()
     transmitter.send.called_with({
         "winner_after_ai_move": "o",
         "ai_move": 3
     })
Example #5
0
 def test_integration_of_parts(self):
     DUMB_AI_DEPTH = 1
     data_from_transmitter_receive = {"board":{1:"x", 2:"x"},
                                      "depth":DUMB_AI_DEPTH}
     SMART_MOVE = 3
     transmitter = mock.Mock()
     transmitter.receive = mock.MagicMock(return_value=data_from_transmitter_receive)
     handler = ResponseHandler(MoveGenerator())
     responder = Responder(transmitter,handler)
     responder.respond()
     self.assertNotEqual(SMART_MOVE, transmitter.send.call_args[0][0]["ai_move"])
     self.assertEqual(None, transmitter.send.call_args[0][0]["winner_after_ai_move"])
    def test_it_hooks_into_socket_sends_and_receives(self):
        transmitter = JsonTransmitter(self.connection_socket)
        handler = ResponseHandler(MoveGenerator())
        responder = Responder(transmitter, handler)

        game_info = json.dumps({"board": {1: "o", 3: "o"}, "depth": 20})
        self.sock.send(game_info)
        responder.respond()

        data_received = self.sock.recv(1024)
        updated_game = json.loads(data_received)

        WIN_MOVE = 2
        self.assertEqual(WIN_MOVE, updated_game["ai_move"])
        self.assertEqual("o", updated_game["winner_after_ai_move"])
        self.assertEqual(None, updated_game["winner_on_board"])
Example #7
0
 def test_redirect_with_headers(self):
     responder = Responder(statusCode=302,
                           body='http://test.com',
                           headers={'test': 'val'})
     response = responder.respond()
     self.assertEqual(response['statusCode'], 302)
     self.assertEqual(response['body'], 'http://test.com')
     self.assertEqual(response['headers'], {'test': 'val'})
      def test_it_hooks_into_socket_sends_and_receives(self):
          transmitter = JsonTransmitter(self.connection_socket)
          handler = ResponseHandler(MoveGenerator())
          responder = Responder(transmitter,handler)

          game_info = json.dumps({ "board": {1:"o",3:"o"},
                                   "depth": 20 })
          self.sock.send(game_info)
          responder.respond()

          data_received = self.sock.recv(1024)
          updated_game = json.loads(data_received)
           
          WIN_MOVE = 2
          self.assertEqual(WIN_MOVE, updated_game["ai_move"])
          self.assertEqual("o" ,updated_game["winner_after_ai_move"])
          self.assertEqual(None, updated_game["winner_on_board"])
Example #9
0
 def test_integration_of_parts(self):
     DUMB_AI_DEPTH = 1
     data_from_transmitter_receive = {
         "board": {
             1: "x",
             2: "x"
         },
         "depth": DUMB_AI_DEPTH
     }
     SMART_MOVE = 3
     transmitter = mock.Mock()
     transmitter.receive = mock.MagicMock(
         return_value=data_from_transmitter_receive)
     handler = ResponseHandler(MoveGenerator())
     responder = Responder(transmitter, handler)
     responder.respond()
     self.assertNotEqual(SMART_MOVE,
                         transmitter.send.call_args[0][0]["ai_move"])
     self.assertEqual(
         None, transmitter.send.call_args[0][0]["winner_after_ai_move"])
Example #10
0
    def test_response_no_headers(self):
        responder = Responder(statusCode=200, body={'url': 'http://test.com'})
        response = responder.respond()
        self.assertEqual(200, response['statusCode'])
        self.assertEqual('{"url": "http://test.com"}', response['body'])

        # expect 'headers' not in response dict
        error = False
        try:
            response['headers']
        except:
            error = True
        self.assertEqual(error, True)
Example #11
0
 def test_responder_sends_what_it_got_from_handler(self):
     handler = ResponseHandler(mock.Mock())
     handler.response = mock.MagicMock(return_value="some stuff")
     responder = Responder(self.transmitter, handler)
     responder.respond()
     self.transmitter.send.assert_called_with("some stuff")
Example #12
0
 def test_responder_sends_what_it_got_from_handler(self):
     handler = ResponseHandler(mock.Mock())
     handler.response = mock.MagicMock(return_value="some stuff")
     responder = Responder(self.transmitter, handler)
     responder.respond()
     self.transmitter.send.assert_called_with("some stuff")