def test_json_changes(self, user_data, user_data_json, chat_data,
                          chat_data_json, conversations, conversations_json):
        dict_persistence = DictPersistence(
            user_data_json=user_data_json,
            chat_data_json=chat_data_json,
            conversations_json=conversations_json)
        user_data_two = user_data.copy()
        user_data_two.update({4: {5: 6}})
        dict_persistence.update_user_data(4, {5: 6})
        assert dict_persistence.user_data == user_data_two
        assert dict_persistence.user_data_json != user_data_json
        assert dict_persistence.user_data_json == json.dumps(user_data_two)

        chat_data_two = chat_data.copy()
        chat_data_two.update({7: {8: 9}})
        dict_persistence.update_chat_data(7, {8: 9})
        assert dict_persistence.chat_data == chat_data_two
        assert dict_persistence.chat_data_json != chat_data_json
        assert dict_persistence.chat_data_json == json.dumps(chat_data_two)

        conversations_two = conversations.copy()
        conversations_two.update({'name4': {(1, 2): 3}})
        dict_persistence.update_conversation('name4', (1, 2), 3)
        assert dict_persistence.conversations == conversations_two
        assert dict_persistence.conversations_json != conversations_json
        assert dict_persistence.conversations_json == encode_conversations_to_json(
            conversations_two)
 def conversations_json(self) -> str:
     """:obj:`str`: The conversations serialized as a JSON-string."""
     if self._conversations_json:
         return self._conversations_json
     else:
         return encode_conversations_to_json(
             self.conversations)  # type: ignore[arg-type]
Example #3
0
 def _dump_into_json(self) -> Any:
     to_dump = {
         "chat_data": self._chat_data,
         "user_data": self._user_data,
         "bot_data": self.bot_data,
         "conversations": encode_conversations_to_json(self._conversations),
     }
     self.logger.info("Dumping %s", to_dump)
     return json.dumps(to_dump)
Example #4
0
    def _dump_into_json(self) -> Any:
        """Dumps data into json format for inserting in db."""

        to_dump = {
            "chat_data": self._chat_data,
            "user_data": self._user_data,
            "bot_data": self.bot_data,
            "conversations": encode_conversations_to_json(self._conversations),
        }
        self.logger.debug("Dumping %s", to_dump)
        return json.dumps(to_dump)
Example #5
0
 def conversations_json(self):
     """:obj:`str`: The conversations serialized as a JSON-string."""
     if self._conversations_json:
         return self._conversations_json
     else:
         return encode_conversations_to_json(self.conversations)
def update_conversations(conversations: dict):
    """save conversations to database"""
    db_conversations = get_persistence_row(name=consts.CONVERSATIONS)
    db_conversations.data = json.loads(
        encode_conversations_to_json(conversations))
    db.session.commit()