bots: Set JSON as default serializer for StateHandler.

With this change, StateHandler.put() does only accept JSON-able
objects by default. The incrementor test tried to store the return
value of send_reply(), a non-JSON-able MockObject, in the state.
Therefore, this commits also sets functional default test return
values for send_message() and send_reply().
Finally, it fixes the tictactoe bot which relied on directly
modifying the state_ attribute.
This commit is contained in:
derAnfaenger 2017-11-01 13:04:54 +01:00 committed by showell
parent 66da09324e
commit daad7f24e4
3 changed files with 5 additions and 2 deletions

View file

@ -299,6 +299,7 @@ class ticTacToeHandler(object):
return_content = TicTacToeGame.detailed_help_message return_content = TicTacToeGame.detailed_help_message
elif (user_game) and TicTacToeGame.check_validity(user_game, TicTacToeGame.sanitize_move(user_game, command)): elif (user_game) and TicTacToeGame.check_validity(user_game, TicTacToeGame.sanitize_move(user_game, command)):
return_content = TicTacToeGame.tictactoe(user_game, user_board, command) return_content = TicTacToeGame.tictactoe(user_game, user_board, command)
storage.put(original_sender, user_board)
elif (user_game) and command == 'quit': elif (user_game) and command == 'quit':
storage.put(original_sender, None) storage.put(original_sender, None)
return_content = "You've successfully quit the game." return_content = "You've successfully quit the game."

View file

@ -56,8 +56,8 @@ class StateHandler(object):
def __init__(self): def __init__(self):
# type: () -> None # type: () -> None
self.state_ = {} # type: Dict[Text, Text] self.state_ = {} # type: Dict[Text, Text]
self.marshal = lambda obj: obj self.marshal = lambda obj: json.dumps(obj)
self.demarshal = lambda obj: obj self.demarshal = lambda obj: json.loads(obj)
def put(self, key, value): def put(self, key, value):
# type: (Text, Text) -> None # type: (Text, Text) -> None

View file

@ -46,6 +46,8 @@ class BotTestCaseBase(TestCase):
self.MockClass = self.patcher.start() self.MockClass = self.patcher.start()
self.mock_bot_handler = self.MockClass(None, None) self.mock_bot_handler = self.MockClass(None, None)
self.mock_bot_handler.storage = StateHandler() self.mock_bot_handler.storage = StateHandler()
self.mock_bot_handler.send_message.return_value = {'id': 42}
self.mock_bot_handler.send_reply.return_value = {'id': 42}
self.message_handler = self.get_bot_message_handler() self.message_handler = self.get_bot_message_handler()
def tearDown(self): def tearDown(self):