def _load_user_and_chat_id_80661419(self, session):
        '''
        helper function that loads the db_model.User and
        db_model.Chat for user / chat id `80661419` which we need if we want to
        test messages

        '''

        # load user
        user_json_path =  u.get_fake_tdlib_messages_path("user/user_id_80661419.json")
        user_obj = u.load_tdlib_generated_obj_from_file(user_json_path, self.converter)
        self.assertIsInstance(user_obj, tdg.user)
        user_dbmodel_obj = UserAide.new_user_from_tdlib_user(session, user_obj)
        self.assertEqual(user_dbmodel_obj.tg_user_id, 80661419)

        session.add(user_dbmodel_obj)
        session.commit()

        # load chat
        chat_json_path = u.get_fake_tdlib_messages_path("chat/chat_private_id_80661419_has_photo.json")
        chat_tdlib_obj = u.load_tdlib_generated_obj_from_file(chat_json_path, self.converter)
        self.assertIsInstance(chat_tdlib_obj, tdg.chat)
        chat_dbmodel_obj = ChatAide.new_chat_from_tdlib_chat(session, chat_tdlib_obj)
        self.assertEqual(chat_dbmodel_obj.tg_chat_id, 80661419)

        session.add(chat_dbmodel_obj)
        session.commit()
    def test_get_chat_by_tg_chat_id(self):
        '''
        `ChatAide.get_chat_by_tg_chat_id`, insert chat into database and assert we can get it out
        '''

        with u.get_testing_sqla_session_contextmanager() as session:

            p3 = u.get_fake_tdlib_messages_path(
                "chat/chat_basic_group_id_-423872019_no_photo.json")
            tdlib_obj3 = u.load_tdlib_generated_obj_from_file(
                p3, self.converter)
            self.assertIsInstance(tdlib_obj3, tdg.chat)
            chat_three = chat_aide.ChatAide.new_chat_from_tdlib_chat(
                session, tdlib_obj3)
            session.add(chat_three)

            session.commit()

            result_chat_three = chat_aide.ChatAide.get_chat_by_tg_chat_id(
                session, -423872019)
            self.assertEqual(result_chat_three.tg_chat_id, -423872019)
            self.assertEqual(len(result_chat_three.versions), 1)
            self.assertIsInstance(result_chat_three, db_model.BasicGroupChat)
            self.assertEqual(result_chat_three.versions[0].title,
                             "telegram_dl development")
    def test_get_text_entities_single_entity_per_message(self):
        '''
        `TextEntityAide.get_text_entity_from_tdlib_text_entity` with the various types of TextEntities
        '''

        for iter_param in self.test_params:

            if not iter_param.should_skip:
                with self.subTest(textEntityType=iter_param.te_type):

                    json_path = self.fake_message_prefix / "message_id_18874368_chat_id_-1001446368458_text_entity_bold.json"
                    message_json = u.get_fake_tdlib_messages_path(
                        iter_param.json_path)
                    message_obj = u.load_tdlib_generated_obj_from_file(
                        message_json, self.converter)

                    # assert that the loaded type is what we expect
                    self.assertIsInstance(message_obj, tdg.message)

                    with u.get_testing_sqla_session_contextmanager(
                    ) as session:

                        self._add_testing_data_to_session(session)

                        # Message related assertions, just to be safe
                        loaded_message = MessageAide.new_message_from_tdlib_message(
                            session, message_obj)
                        self.assertEqual(
                            len(loaded_message.versions), 1,
                            "make sure we only have 1 loaded version")
                        message_version = loaded_message.versions[-1]
                        self.assertIsInstance(
                            message_version, db_model.MessageVersionText,
                            "verify db_model.MessageVersion subclass")
                        self.assertEqual(iter_param.msg_text,
                                         message_version.text,
                                         "verify message text")

                        # Text Entity related assertions

                        loaded_text_entities = TextEntityAide.get_text_entities_from_tdlib_text_entity_sequence(
                            message_version, message_obj.content.text.entities)

                        self.assertEqual(
                            len(loaded_text_entities), 1,
                            "make sure we only have 1 text entity")
                        te = loaded_text_entities[0]
                        self.assertIsInstance(
                            te, db_model.TextEntity,
                            "verify type is db_model.TextEntity")
                        self.assertEqual(te.offset, iter_param.te_offset,
                                         "verify TextEntity offset")
                        self.assertEqual(te.length, iter_param.te_length,
                                         "verify TextEntity length")
                        self.assertEqual(te.text_entity_type,
                                         iter_param.te_type,
                                         "verify type of TextEntity")
    def test_get_chat_by_tg_chat_id(self):
        '''
        `ChatAide.get_chat_by_tg_chat_id`, insert chat into database and assert we can get it out
        '''

        with u.get_testing_sqla_session_contextmanager() as session:

            # add the db_model.User that this private chat is with, or else it
            # will have a integrity error when we add the chat
            user_json = u.get_fake_tdlib_messages_path(
                "user/user_id_80661419.json")
            user_obj = u.load_tdlib_generated_obj_from_file(
                user_json, self.converter)
            self.assertIsInstance(user_obj, tdg.user)
            user_tdlib_obj = UserAide.new_user_from_tdlib_user(
                session, user_obj)
            session.add(user_tdlib_obj)

            # add the db_model.PrivateChat
            p4 = u.get_fake_tdlib_messages_path(
                "chat/chat_private_id_80661419_has_photo.json")
            tdlib_obj4 = u.load_tdlib_generated_obj_from_file(
                p4, self.converter)
            self.assertIsInstance(tdlib_obj4, tdg.chat)
            chat_four = chat_aide.ChatAide.new_chat_from_tdlib_chat(
                session, tdlib_obj4)
            session.add(chat_four)

            # commit it
            session.commit()

            # do assertions
            result_chat_four = chat_aide.ChatAide.get_chat_by_tg_chat_id(
                session, 80661419)
            self.assertEqual(result_chat_four.tg_chat_id, 80661419)
            self.assertEqual(len(result_chat_four.versions), 1)
            self.assertIsInstance(result_chat_four, db_model.PrivateChat)
            self.assertEqual(result_chat_four.versions[0].title, "John Smith")
    def _add_testing_data_to_session(self, session):
        '''
        Adds testing data that all of the unit tests in this file will use, since all
        of the examples are messages that were sent in a channel (so therefore no user)
        and were all sent within the same chat (a supergroup channel)
        '''

        # load the chat
        path_chat = u.get_fake_tdlib_messages_path(
            "chat/chat_supergroup_id_-1001446368458_channel_no_photo.json")
        tdlib_obj_chat = u.load_tdlib_generated_obj_from_file(
            path_chat, self.converter)
        self.assertIsInstance(tdlib_obj_chat, tdg.chat)
        chat = ChatAide.new_chat_from_tdlib_chat(session, tdlib_obj_chat)
        session.add(chat)

        session.commit()
    def _load_and_return_message_id_599515987968(self, session):
        '''
        helper function that loads the db_model.Message for message id `599515987968`
        and returns the resulting db_model.Message object

        '''

        # load message from JSON
        message_json =  u.get_fake_tdlib_messages_path("message/messageText/message_id_599515987968_chat_id_80661419_message_text.json")
        message_tdlib_obj = u.load_tdlib_generated_obj_from_file(message_json, self.converter)
        self.assertIsInstance(message_tdlib_obj, tdg.message)
        self.assertIsInstance(message_tdlib_obj.content, tdg.messageText)
        message_dbmodel_obj = MessageAide.new_message_from_tdlib_message(session, message_tdlib_obj)

        session.add(message_dbmodel_obj)
        session.commit()

        return message_dbmodel_obj
Esempio n. 7
0
    def test_compare_tdlib_and_dbmodel_chat_equal(self):
        '''
        `ChatAide.test_compare_tdlib_and_dbmodel_chat`, should be equal
        '''

        p = u.get_fake_tdlib_messages_path("chat/chat_supergroup_id_-1001446368458_channel_has_photo.json")
        tdlib_obj = u.load_tdlib_generated_obj_from_file(p, self.converter)

        # create the db_model objects from JSON

        asof_time = arrow.utcnow()
        chat_dbmodel_obj = self._get_supergroup_channel_chat_photo(asof_time)

        # assert that we correctly compare these as true

        compare_result = chat_aide.ChatAide.compare_tdlib_and_dbmodel_chat(
            dbmodel_chat=chat_dbmodel_obj,
            tdlib_chat=tdlib_obj)

        self.assertTrue(compare_result)
Esempio n. 8
0
    def test_get_chat_by_tg_chat_id(self):
        '''
        `ChatAide.get_chat_by_tg_chat_id`, insert chat into database and assert we can get it out
        '''

        with u.get_testing_sqla_session_contextmanager() as session:

            p1 = u.get_fake_tdlib_messages_path("chat/chat_supergroup_id_-1001446368458_channel_has_photo.json")
            tdlib_obj1 = u.load_tdlib_generated_obj_from_file(p1, self.converter)
            self.assertIsInstance(tdlib_obj1, tdg.chat)
            chat_one = chat_aide.ChatAide.new_chat_from_tdlib_chat(session, tdlib_obj1)
            session.add(chat_one)

            session.commit()

            result_chat_one = chat_aide.ChatAide.get_chat_by_tg_chat_id(session, -1001446368458)
            self.assertEqual(result_chat_one.tg_chat_id, -1001446368458)
            self.assertEqual(len(result_chat_one.versions), 1)
            self.assertIsInstance(result_chat_one, db_model.SuperGroupChat)
            self.assertEqual(result_chat_one.versions[0].title, "TestingChannel")
Esempio n. 9
0
    def test_get_chat_by_tg_chat_id(self):
        '''
        `ChatAide.get_chat_by_tg_chat_id`, insert chat into database and assert we can get it out
        '''

        with u.get_testing_sqla_session_contextmanager() as session:

            p2 = u.get_fake_tdlib_messages_path(
                "chat/chat_supergroup_id_-1001266163180_no_photo.json")
            tdlib_obj2 = u.load_tdlib_generated_obj_from_file(
                p2, self.converter)
            self.assertIsInstance(tdlib_obj2, tdg.chat)
            chat_two = chat_aide.ChatAide.new_chat_from_tdlib_chat(
                session, tdlib_obj2)
            session.add(chat_two)

            session.commit()

            result_chat_two = chat_aide.ChatAide.get_chat_by_tg_chat_id(
                session, -1001266163180)
            self.assertEqual(result_chat_two.tg_chat_id, -1001266163180)
            self.assertEqual(len(result_chat_two.versions), 1)
            self.assertIsInstance(result_chat_two, db_model.SuperGroupChat)
            self.assertEqual(result_chat_two.versions[0].title, "TGS stuff")
Esempio n. 10
0
    def test_compare_tdlib_and_dbmodel_chat_multiple_chat_versions(self):
        '''
        `ChatAide.test_compare_tdlib_and_dbmodel_chat`, with multiple `db_model.ChatVersion` objects

        NOTE: this is not fully testing the `relationship` of the Chat -> ChatVersion
        (the `versions` field), since when loading from the database it will sort by the
        `ChatVersion.as_of` field

        so theoretically we could do `Chat.versions.insert(0, chat_ver_obj)`
        (instead of `append()`) and that will insert it at index 0, even if the
        `chat_ver_obj` is not the earliest ChatVersion (by the `as_of` field),
        but since we aren't loading it from the database, sqlalchemy doesn't
        re-sort it for us or anything

        '''

        asof_time = arrow.utcnow()

        chat_dbmodel_obj = self._get_supergroup_channel_chat_photo(asof_time)


        p = u.get_fake_tdlib_messages_path("chat/chat_supergroup_id_-1001446368458_channel_has_photo.json")
        tdlib_obj = u.load_tdlib_generated_obj_from_file(p, self.converter)

        # so we have a Chat (Supergroup) with a photo, it should equal the message that we loaded
        # from the JSON

        compare_result_one = chat_aide.ChatAide.compare_tdlib_and_dbmodel_chat(
            dbmodel_chat=chat_dbmodel_obj,
            tdlib_chat=tdlib_obj)

        self.assertEqual(len(chat_dbmodel_obj.versions), 1)
        # has a photo to start out with
        self.assertNotEqual(chat_dbmodel_obj.versions[-1].photo_set, None)
        self.assertTrue(compare_result_one)

        # now add a chat version where we 'remove' the photo
        # and add it as a version
        chat_ver_no_photo = self._get_chat_version(arrow.now(), "TestingChannel", None, False)

        chat_dbmodel_obj.versions.append(chat_ver_no_photo)

        # now the compare method should return as not equal
        compare_result_two = chat_aide.ChatAide.compare_tdlib_and_dbmodel_chat(
            dbmodel_chat=chat_dbmodel_obj,
            tdlib_chat=tdlib_obj)

        self.assertEqual(len(chat_dbmodel_obj.versions), 2)
        # does not have a photo for this one
        self.assertEqual(chat_dbmodel_obj.versions[-1].photo_set, None)
        self.assertFalse(compare_result_two)

        # if we add another version that should be the same as the "first" version
        # (has the same photo), then it should compare as equal again
        chat_ver_photo_two = self._get_chat_version(arrow.now(), "TestingChannel", self._get_photo_set(), False)

        chat_dbmodel_obj.versions.append(chat_ver_photo_two)

        compare_result_three = chat_aide.ChatAide.compare_tdlib_and_dbmodel_chat(
            dbmodel_chat=chat_dbmodel_obj,
            tdlib_chat=tdlib_obj)

        self.assertEqual(len(chat_dbmodel_obj.versions), 3)
        # has a photo
        self.assertNotEqual(chat_dbmodel_obj.versions[-1].photo_set, None)
        self.assertTrue(compare_result_three)
    def test_new_message_from_tdlib_message(self):

        with u.get_testing_sqla_session_contextmanager() as session:

            # add the db_model.User that this private chat is with, or else it
            # will have a integrity error when we add the chat
            message_json =  u.get_fake_tdlib_messages_path("message/messageText/message_id_599515987968_chat_id_80661419_message_text.json")
            message_tdlib_obj = u.load_tdlib_generated_obj_from_file(message_json, self.converter)
            self.assertIsInstance(message_tdlib_obj, tdg.message)
            self.assertIsInstance(message_tdlib_obj.content, tdg.messageText)

            # load user
            user_json_path =  u.get_fake_tdlib_messages_path("user/user_id_80661419.json")
            user_obj = u.load_tdlib_generated_obj_from_file(user_json_path, self.converter)
            self.assertIsInstance(user_obj, tdg.user)
            user_dbmodel_obj = UserAide.new_user_from_tdlib_user(session, user_obj)
            session.add(user_dbmodel_obj)
            session.commit()
            self.assertEqual(user_dbmodel_obj.tg_user_id, 80661419)

            # load chat

            chat_json_path = u.get_fake_tdlib_messages_path("chat/chat_private_id_80661419_has_photo.json")
            chat_tdlib_obj = u.load_tdlib_generated_obj_from_file(chat_json_path, self.converter)
            self.assertIsInstance(chat_tdlib_obj, tdg.chat)
            chat_dbmodel_obj = ChatAide.new_chat_from_tdlib_chat(session, chat_tdlib_obj)
            session.add(chat_dbmodel_obj)
            session.commit()

            # save and load message

            message_dbmodel_obj = MessageAide.new_message_from_tdlib_message(session, message_tdlib_obj)

            session.add(message_dbmodel_obj)
            session.commit()

            retrieved_message_dbmodel_obj = MessageAide.get_message_by_tg_message_and_tg_chat_id(
                session, 599515987968, 80661419)

            # message assertions
            self.assertEqual(retrieved_message_dbmodel_obj.tg_message_id, 599515987968)
            self.assertTrue(retrieved_message_dbmodel_obj.is_outgoing)
            self.assertFalse(retrieved_message_dbmodel_obj.is_channel_post)
            self.assertTrue(retrieved_message_dbmodel_obj.can_edit)
            self.assertTrue(retrieved_message_dbmodel_obj.can_forward)
            self.assertTrue(retrieved_message_dbmodel_obj.can_be_deleted_only_for_self)
            self.assertFalse(retrieved_message_dbmodel_obj.can_be_deleted_for_all_users)
            self.assertEqual(retrieved_message_dbmodel_obj.restriction_reason, "")

            self.assertIsNone(retrieved_message_dbmodel_obj.via_bot_user)
            self.assertIsNone(retrieved_message_dbmodel_obj.reply_to_message)

            self.assertEqual(retrieved_message_dbmodel_obj.sender_user.user_id, user_dbmodel_obj.user_id)
            self.assertEqual(retrieved_message_dbmodel_obj.chat.chat_id, chat_dbmodel_obj.chat_id)

            # message version assertions
            self.assertEqual(len(retrieved_message_dbmodel_obj.versions), 1)

            latest_ver = retrieved_message_dbmodel_obj.versions[0]

            self.assertIsInstance(latest_ver, db_model.MessageVersionText)
            self.assertEqual(latest_ver.date, arrow.get(1589260319))
            self.assertIsNone(latest_ver.edit_date)
            self.assertEqual(latest_ver.author_signature, "")
            self.assertEqual(latest_ver.ttl, 0)

            # MessageVersionText assertions

            self.assertEqual(latest_ver.text, "https://twitter.com/dril/status/107911000199671808")

            # TODO: ASSERT WEB PAGE

            # MessageVersionText TextEntities assertions
            self.assertEqual(len(latest_ver.text_entities), 1)

            text_entity = latest_ver.text_entities[0]

            self.assertEqual(text_entity.offset, 0)
            self.assertEqual(text_entity.length, 50)
            self.assertEqual(text_entity.text_entity_type, dbme.TextEntityTypeEnum.TEXT_ENTITY_TYPE_URL)
    def test_new_chat_from_tdlib_chat(self):
        '''
        `ChatAide.new_chat_from_tdlib_chat`, get new `db_model.Chat` from `tdlib_generated.chat`
        '''

        with u.get_testing_sqla_session_contextmanager() as session:

            # add the db_model.User that this private chat is with, or else it
            # will have a integrity error when we add the chat
            user_json = u.get_fake_tdlib_messages_path(
                "user/user_id_80661419.json")
            user_obj = u.load_tdlib_generated_obj_from_file(
                user_json, self.converter)
            self.assertIsInstance(user_obj, tdg.user)
            user_tdlib_obj = UserAide.new_user_from_tdlib_user(
                session, user_obj)
            session.add(user_tdlib_obj)

            # add the db_model.PrivateChat
            p = u.get_fake_tdlib_messages_path(
                "chat/chat_private_id_80661419_has_photo.json")
            tdlib_obj = u.load_tdlib_generated_obj_from_file(p, self.converter)

            self.assertIsInstance(tdlib_obj, tdg.chat)

            db_model_chat_obj = chat_aide.ChatAide.new_chat_from_tdlib_chat(
                session, tdlib_obj)

            # assertions
            self.assertIsInstance(db_model_chat_obj, db_model.PrivateChat)

            self.assertEqual(db_model_chat_obj.tg_chat_id, 80661419)

            self.assertEqual(len(db_model_chat_obj.versions), 1)

            latest_ver = db_model_chat_obj.versions[-1]

            self.assertEqual(latest_ver.title, "John Smith")
            self.assertFalse(latest_ver.is_sponsored)

            ##################################
            # test the photo set
            ##################################

            latest_ver_photo_set = latest_ver.photo_set

            self.assertEqual(len(latest_ver_photo_set.photos), 2)

            # big chat photo
            big_list = latest_ver_photo_set \
                .get_photos_by_thumnail_type(dbe.PhotoSizeThumbnailType.PHOTO_BIG)
            self.assertEqual(len(big_list), 1)
            big_photo = big_list[0]

            self.assertEqual(big_photo.thumbnail_type,
                             dbe.PhotoSizeThumbnailType.PHOTO_BIG)
            self.assertEqual(big_photo.width, -1)
            self.assertEqual(big_photo.height, -1)
            self.assertFalse(big_photo.has_stickers)

            big_file = big_photo.file

            # NOTE: so this seems weird, since if you look at the JSON, the `file.id` for this
            # is clearly 30 (and 29 for the user), but since a Private Chat is between you
            # and another user, the photo is the other user's profile photo.
            # so since we have to add the user that this chat is with to the database session
            # (or else we get an integrity error), the `file.id` is actually of the file of the
            # user's photo , and therefore in the user json rather than the chat json
            # however the remote_file_id and remote_unique_id should be the same!
            self.assertEqual(big_file.tg_file_id, 15)
            self.assertEqual(big_file.size, 0)
            self.assertEqual(big_file.expected_size, 0)
            self.assertEqual(
                big_file.remote_file_id,
                "AQADAQADXKkxG6vLzgQACLJsEjAABAMAA6vLzgQABHHpJIC1z601EmwAAhgE")
            self.assertEqual(big_file.remote_unique_id, "AQADsmwSMAAEEmwAAg")

            # small chat photo
            small_list = latest_ver_photo_set \
                .get_photos_by_thumnail_type(dbe.PhotoSizeThumbnailType.PHOTO_SMALL)
            self.assertEqual(len(small_list), 1)
            small_photo = small_list[0]

            self.assertEqual(small_photo.thumbnail_type,
                             dbe.PhotoSizeThumbnailType.PHOTO_SMALL)
            self.assertEqual(small_photo.width, -1)
            self.assertEqual(small_photo.height, -1)
            self.assertFalse(small_photo.has_stickers)

            small_file = small_photo.file

            # see note above
            self.assertEqual(small_file.tg_file_id, 14)
            self.assertEqual(small_file.size, 0)
            self.assertEqual(small_file.expected_size, 0)
            self.assertEqual(
                small_file.remote_file_id,
                "AQADAQADXKkxG6vLzgQACLJsEjAABAIAA6vLzgQABHHpJIC1z601EGwAAhgE")
            self.assertEqual(small_file.remote_unique_id, "AQADsmwSMAAEEGwAAg")
    def test_compare_dbmodel_and_tdlib_message(self):
        '''
        test `MessageAide.compare_dbmodel_and_tdlib_message` for general Message stuff
        not for anything in db_model.MessageVersion / tdlib_generated.MessageContent
        '''
        message_json =  u.get_fake_tdlib_messages_path("message/messageText/message_id_599515987968_chat_id_80661419_message_text.json")
        message_tdlib_obj = u.load_tdlib_generated_obj_from_file(message_json, self.converter)

        with u.get_testing_sqla_session_contextmanager() as session:

            # add the db_model.User and db_model.Chat that this private chat is with, or else it
            # will have a integrity error when we add the chat
            self._load_user_and_chat_id_80661419(session)


            # do the initial load of the first message
            initial_dbmodel_message_obj = self._load_and_return_message_id_599515987968(session)

            # assert that the db_model.Message and tdlib_generated.message are equal

            is_equal_unmodified_message = MessageAide.compare_dbmodel_and_tdlib_message(
                initial_dbmodel_message_obj, message_tdlib_obj)

            self.assertTrue(is_equal_unmodified_message, "unmodified message should be equal")

            # get a copy of the message from the sqlalchemy session so we can 'modify it' for testing
            # note: i was originally using `_load_and_return_message_id_599515987968` here but then
            # it was not proving equal because it was adding another (duplicate) TextEntity to the message
            # probably because the message already existed , but i would think it would be a brand new
            # message / MessageVersion, rather than using the existing one, so maybe this is a bug? i'm not
            # sure, should probably look into this at some point
            # TODO
            modified_dbmodel_message_obj = session.query(db_model.Message) \
                .filter(db_model.Message.message_id == initial_dbmodel_message_obj.message_id) \
                .first()


            attributes_to_modify = [
                AttributeToModify(attr_name="is_outgoing", attr_value=False),
                AttributeToModify(attr_name="is_channel_post", attr_value=True),
                AttributeToModify(attr_name="can_edit", attr_value=False),
                AttributeToModify(attr_name="can_forward", attr_value=False),
                AttributeToModify(attr_name="can_be_deleted_only_for_self", attr_value=False),
                AttributeToModify(attr_name="can_be_deleted_for_all_users", attr_value=True),
                AttributeToModify(attr_name="restriction_reason", attr_value='''{"ios":"contains a sonic meme"'''),

            ]

            for iter_attr_to_mod in attributes_to_modify:

                # make sure the db_model and tdlib message should be equal before modification
                self.assertTrue(
                    MessageAide.compare_dbmodel_and_tdlib_message(
                    modified_dbmodel_message_obj, message_tdlib_obj),
                "tdlib and dbmodel message should be equal before modification")

                # modify the db_model message and assert it is no longer equal
                old_value = getattr(modified_dbmodel_message_obj, iter_attr_to_mod.attr_name)

                setattr(modified_dbmodel_message_obj, iter_attr_to_mod.attr_name, iter_attr_to_mod.attr_value)

                is_equal_with_modified_message = MessageAide.compare_dbmodel_and_tdlib_message(
                    modified_dbmodel_message_obj, message_tdlib_obj)

                self.assertFalse(is_equal_with_modified_message,
                    "tdlib and dbmodel message after modification should not be equal")


                # modify the value back to its original value and make sure it is equal

                setattr(modified_dbmodel_message_obj, iter_attr_to_mod.attr_name, old_value)

                self.assertTrue(
                    MessageAide.compare_dbmodel_and_tdlib_message(
                    modified_dbmodel_message_obj, message_tdlib_obj),
                "tdlib and dbmodel message should be equal after restoring original value")
Esempio n. 14
0
    def test_new_user_from_tdlib_user_equal(self):
        '''
        `UserAide.new_user_from_tdlib_user`, should be equal
        '''

        with u.get_testing_sqla_session_contextmanager() as session:

            # add the db_model.User that this private chat is with, or else it
            # will have a integrity error when we add the chat
            user_json = u.get_fake_tdlib_messages_path(
                "user/user_id_80661419.json")
            user_obj = u.load_tdlib_generated_obj_from_file(
                user_json, self.converter)
            self.assertIsInstance(user_obj, tdg.user)
            user_dbmodel_obj = UserAide.new_user_from_tdlib_user(
                session, user_obj)
            session.add(user_dbmodel_obj)

            # assertions

            self.assertEqual(user_dbmodel_obj.tg_user_id, 80661419)

            self.assertEqual(len(user_dbmodel_obj.versions), 1)

            latest_ver = user_dbmodel_obj.versions[-1]

            self.assertEqual(latest_ver.first_name, "John")
            self.assertEqual(latest_ver.last_name, "Doe")
            self.assertEqual(latest_ver.user_name, "TestingJohnDoe")

            # NOTE: it seems that the `sqlalchemy_utils.PhoneNumberType` converts
            # a string phone number into a `phonenumbers` object only when its been
            # loaded from the database
            # so that means we should expect the number to be a STRING here
            # but in other tests (like in a `get_user_by_tg_user_id` test) the phone number
            # would be a `phonenumber` object
            expected_phone_number = phonenumbers.parse(
                "+15555555", region=constants.PHONE_NUMBER_DEFAULT_REGION)
            phone_number_from_user_parsed_from_str = PhoneNumberAide \
                .parse_phone_number_from_string(latest_ver.phone_number)
            self.assertIsInstance(latest_ver.phone_number, str)
            self.assertEqual(phone_number_from_user_parsed_from_str,
                             expected_phone_number)

            self.assertTrue(latest_ver.is_contact)
            self.assertTrue(latest_ver.is_mutual_contact)
            self.assertFalse(latest_ver.is_verified)
            self.assertFalse(latest_ver.is_support)
            self.assertEqual(latest_ver.restriction_reason, "")
            self.assertFalse(latest_ver.is_scam)
            self.assertTrue(latest_ver.have_access)
            self.assertEqual(latest_ver.language_code, "")
            self.assertEqual(latest_ver.user_type,
                             dbe.UserTypeEnum.USER_TYPE_REGULAR)

            #########################
            # test profile photo set
            #########################

            latest_ver_profile_photo_set = latest_ver.profile_photo_set

            self.assertEqual(len(latest_ver_profile_photo_set.photos), 2)

            self.assertEqual(latest_ver_profile_photo_set.tg_id,
                             346438157110192450)

            # test big

            # NOTE: we _cannot_ use the `big` and `small` relationship properties here since we
            # created this object manually and have not loaded it from the database, and the relationship
            # is a `primaryjoin` so even though the relationship should exist, they are None at the moment
            # unless we fetch the object fresh from the database again
            big_list = latest_ver_profile_photo_set \
                .get_photos_by_thumnail_type(dbe.PhotoSizeThumbnailType.PHOTO_BIG)
            self.assertEqual(len(big_list), 1)
            big_photo = big_list[0]

            self.assertEqual(big_photo.thumbnail_type,
                             dbe.PhotoSizeThumbnailType.PHOTO_BIG)
            self.assertEqual(big_photo.width, -1)
            self.assertEqual(big_photo.height, -1)
            self.assertFalse(big_photo.has_stickers)

            big_file = big_photo.file

            self.assertEqual(big_file.tg_file_id, 15)
            self.assertEqual(big_file.size, 0)
            self.assertEqual(big_file.expected_size, 0)
            self.assertEqual(
                big_file.remote_file_id,
                "AQADAQADXKkxG6vLzgQACLJsEjAABAMAA6vLzgQABHHpJIC1z601EmwAAhgE")
            self.assertEqual(big_file.remote_unique_id, "AQADsmwSMAAEEmwAAg")

            # test small

            # see note above
            small_list = latest_ver_profile_photo_set \
                .get_photos_by_thumnail_type(dbe.PhotoSizeThumbnailType.PHOTO_SMALL)
            self.assertEqual(len(small_list), 1)
            small_photo = small_list[0]

            self.assertEqual(small_photo.thumbnail_type,
                             dbe.PhotoSizeThumbnailType.PHOTO_SMALL)
            self.assertEqual(small_photo.width, -1)
            self.assertEqual(small_photo.height, -1)
            self.assertFalse(small_photo.has_stickers)

            small_file = small_photo.file

            # see note above
            self.assertEqual(small_file.tg_file_id, 14)
            self.assertEqual(small_file.size, 0)
            self.assertEqual(small_file.expected_size, 0)
            self.assertEqual(
                small_file.remote_file_id,
                "AQADAQADXKkxG6vLzgQACLJsEjAABAIAA6vLzgQABHHpJIC1z601EGwAAhgE")
            self.assertEqual(small_file.remote_unique_id, "AQADsmwSMAAEEGwAAg")