def test_update(self): uid_1 = "uid_user_1" uid_2 = "uid_user_2" usecret_1 = "usecret_1" usecret_2 = "usecret_2" self.test_add_friend() resp, code = self.test_login(uid=uid_1, usecret=usecret_1, result=True) assert code == 200, resp resp_dict = json.loads(resp) headers = dict() headers['token'] = resp_dict['token'] success, dialog_id = Dialog.did_from_users_list([uid_1, uid_2]) assert success, dialog_id msg1 = Message(dialog_id=dialog_id, text="test text", from_id=uid_1, time_stamp=-1) msg2 = Message(dialog_id=dialog_id, text="text test", from_id=uid_2, time_stamp=-2) valid_params = msg1.to_dict() resp, code = api.on_new_msg(headers=headers, params=valid_params) assert code == 200, str(code) + resp resp, code = api.on_new_msg(headers=headers, params=valid_params) assert code == 200, str(code) + resp resp, code = api.on_update_request(headers=headers) assert code == 200, str(code) + resp print "test[test_update] finished"
def test_message_from_dict(self): from_uid = "Fyu3" text = "this test is awesome" dialog_id = "d3oP" time_stamp = 3 params_invalid_0 = None params_invalid_1 = dict() params_invalid_1['from_id'] = from_uid params_invalid_1['text'] = text params_invalid_1['dialog_id'] = dialog_id params_valid = dict(params_invalid_1) params_valid['time_stamp'] = time_stamp success_invalid_0, info = Message.from_dict(params=params_invalid_0) assert not success_invalid_0, info print "[test_message_from_dict] test case 0 finished" success_invalid_1, info = Message.from_dict(params=params_invalid_1) assert not success_invalid_1, info print "[test_message_from_dict] test case 1 finished" params_valid, info = Message.from_dict(params=params_valid) assert params_valid, info print "[test_message_from_dict] test case 2 finished"
def test_user_update_holder(self): # 1. create users user1 = User(uid="test0", name="test0", secret="1111") user2 = User(uid="test1", name="test1", secret="1111") # 2. imitate dialog # dialog_holders = DialogsHolders.get_instance() # # list_of_users = list() # list_of_users.append(user1.uid) # list_of_users.append(user2.uid) # # success, dialog = dialog_holders.create_dialog(list_of_users=list_of_users) # 3. prepare messages msg1 = Message(dialog_id="test", text="test text", from_id=user1.uid, time_stamp=-1) msg2 = Message(dialog_id="test", text="text test", from_id=user2.uid, time_stamp=-2) # 4. prepare update holders update_holder_u1 = UserUpdateHolder(user_id=user1.uid) update_holder_u2 = UserUpdateHolder(user_id=user2.uid) update_holder_u1.add(message=msg1, dialog_id="test") update_holder_u1.add(message=msg2, dialog_id="test") update_holder_u1_dict = update_holder_u1.get_as_dict() update_holder_u2_dict = update_holder_u2.get_as_dict() j_result = update_holder_u1.get_as_json() print "[test_user_update_holder] test cases finished" pass # do not know how to automate validation -> seems to be OK
def send_message_with_save(bot: Bot, message_id: int, chat_id: int, text: str, custom_keyboard: List[List[InlineKeyboardButton]], edit_mode: bool, is_available: bool = True): buttons_texts, buttons_callbacks = parse_inline_keyboard(custom_keyboard) print( Message(message_id, 0, chat_id, text, buttons_texts, buttons_callbacks, is_available)) put_message( Message(message_id, 0, chat_id, text, buttons_texts, buttons_callbacks, is_available)) if edit_mode is False: bot.send_message(text=text, message_id=message_id, chat_id=chat_id, parse_mode='HTML', disable_web_page_preview=True, disable_notification=True, reply_markup=InlineKeyboardMarkup(custom_keyboard)) else: bot.edit_message_text( text=text, message_id=message_id, chat_id=chat_id, parse_mode='HTML', reply_markup=InlineKeyboardMarkup(custom_keyboard))
def test_user_history_holder(self): ##### # from previous test # 1. create users user1 = User(uid="uid_test0", name="name test0", secret="secr1111") user2 = User(uid="uid_test1", name="name test1", secret="secr1111") # 2. imitate dialog # dialog_holders = DialogsHolders.get_instance() # # list_of_users = list() # list_of_users.append(user1.uid) # list_of_users.append(user2.uid) # # success, dialog = dialog_holders.create_dialog(list_of_users=list_of_users) # 3. prepare messages msg1 = Message(dialog_id="did_test", text="test from_id=user1.uid", from_id=user1.uid, time_stamp=-1) msg2 = Message(dialog_id="did_test", text="text from_id=user2.uid", from_id=user2.uid, time_stamp=-2) msg3 = Message(dialog_id="did_test", text="text from_id=user2.uid 2", from_id=user2.uid, time_stamp=-3) # 4. prepare update holders update_holder_u1 = UserUpdateHolder(user_id=user1.uid) update_holder_u2 = UserUpdateHolder(user_id=user2.uid) update_holder_u1.add(message=msg1, dialog_id="did_test") update_holder_u1.add(message=msg2, dialog_id="did_test") update_holder_u1.add(message=msg3, dialog_id="did_test") update_holder_u1_dict = update_holder_u1.get_as_dict() update_holder_u2_dict = update_holder_u2.get_as_dict() j_result = update_holder_u1.get_as_json() ##### # this test history_holder_u1 = UserHistoryHolder(user_id=user1.uid) history_holder_u2 = UserHistoryHolder(user_id=user2.uid) for did, dialog_update in update_holder_u1.storage.iteritems(): history_holder_u1.on_add(dialog_update_list=dialog_update) for did, dialog_update in update_holder_u1.storage.iteritems(): history_holder_u2.on_add(dialog_update_list=dialog_update) history_holder_u1_dict = history_holder_u1.to_dict() history_holder_u2_dict = history_holder_u2.to_dict() print "[test_user_history_holder] test cases finished" pass # todo: automate validation
def get_previous_message(chat_id: int, message_id: int) -> Message: session = Session(engine) rs = session.execute( "SELECT message_id, message_version, chat_id, text, buttons_names, buttons_callbacks, is_available " "FROM messages WHERE chat_id = :chat_id AND is_available = TRUE " "AND date_time < " "(SELECT date_time FROM messages WHERE message_id = :message_id AND chat_id = :chat_id AND is_available = TRUE ORDER BY date_time DESC LIMIT 1) " "ORDER BY date_time " "DESC LIMIT 1", { "message_id": message_id, "chat_id": chat_id }) for row in rs: session.execute( "DELETE FROM messages WHERE message_id = :message_id AND chat_id = :chat_id AND is_available = TRUE " "AND date_time = " "(SELECT date_time FROM messages WHERE message_id = :message_id AND chat_id = :chat_id AND is_available = TRUE ORDER BY date_time DESC LIMIT 1)", { "message_id": message_id, "chat_id": chat_id }) session.commit() session.close() return Message(int(row[0]), int(row[1]), int(row[2]), row[3], from_str(row[4]), from_str(row[5]), True)
def route_create_message(room_id: str, message: str): uid = request.headers.get("uid", None) user = User.objects.get_or_404(uid=uid) message = Message(user_id=str(user.id), message=message, created_at=pendulum.now().int_timestamp) chat_room = ChatRoom.objects.get_or_404(id=room_id, members=user) chat_room.messages.append(message) chat_room.save() user_from = user user_to_list = [ member for member in chat_room.members if member.id != user.id ] for user_to in user_to_list: push = AlertRecord(push_type="MESSAGE", user_id=user_from.id, created_at=pendulum.now().int_timestamp, chat_room_id=chat_room.id, message_id=message.id, message=message.message) data = alerts_blueprint.dictify_push_item(push) message_service.push(data, user_to.r_token) return Response(message.to_json(), mimetype="application/json")
def test_update_history_manager(self): # 1. create users user1 = User(uid="uid_test0", name="name test0", secret="secr1111") user2 = User(uid="uid_test1", name="name test1", secret="secr1111") dialog_holders = DialogsHolders.get_instance() list_of_users = list() list_of_users.append(user1.uid) list_of_users.append(user2.uid) success, dialog = dialog_holders.create_dialog(list_of_users=list_of_users) assert success assert len(dialog.list_of_users) == len(list_of_users) update_history_manager = UpdateHistoryManager(dialogs_holder=dialog_holders) # 3. prepare messages msg1 = Message(dialog_id=dialog.did, text="test from_id=user1.uid", from_id=user1.uid, time_stamp=-1) msg2 = Message(dialog_id=dialog.did, text="text from_id=user2.uid", from_id=user2.uid, time_stamp=-2) msg3 = Message(dialog_id=dialog.did, text="text from_id=user2.uid 2", from_id=user2.uid, time_stamp=-3) success_msg1, info_msg1 = update_history_manager.on_new_msg(msg=msg1) success_msg2, info_msg2 = update_history_manager.on_new_msg(msg=msg2) success_msg3, info_msg3 = update_history_manager.on_new_msg(msg=msg3) assert success_msg1, info_msg1 assert success_msg2, info_msg2 assert success_msg3, info_msg3 history_before_u1 = update_history_manager.on_get_history_json(user1.uid, dialog_id=dialog.did) history_before_u2 = update_history_manager.on_get_history_json(user2.uid, dialog_id=dialog.did) updates_u1 = update_history_manager.on_get_update_json(user_id=user1.uid) updates_u2 = update_history_manager.on_get_update_json(user_id=user2.uid) history_after_u1 = update_history_manager.on_get_history_json(user1.uid, dialog_id=dialog.did) history_after_u2 = update_history_manager.on_get_history_json(user2.uid, dialog_id=dialog.did) print "[test_update_history_manager] test cases finished" pass
def get_last_message(chat_id: int) -> Message: session = Session(engine) rs = session.execute( "SELECT message_id, message_version, chat_id, text, buttons_names, buttons_callbacks, is_available " "FROM messages WHERE chat_id = :chat_id " "ORDER BY date_time DESC " "LIMIT 1", {"chat_id": chat_id}) for row in rs: session.commit() session.close() return Message(int(row[0]), int(row[1]), int(row[2]), row[3], from_str(row[4]), from_str(row[5]), row[6])
def success(user_from): msg_correct, msg = Message.from_dict(params=params) if not msg_correct: response = json.dumps({'error': msg}) return response, 400 else: dialog_id = msg.dialog_id found_dialog, dialog = dialogs_storage.get_dialog(did=dialog_id) if found_dialog: update_history_manager.on_new_msg(msg=msg) # response = update_history_manager.on_get_update_json(user_id=user_id) response = "{}" return response, 200 else: response = json.dumps({'error': "dialog object with specified not found"}) return response, 500