示例#1
0
def get_user_by_id(id_string):
    """

    (string) -> (User or None, string, int)

    Function gets object User by users's id string from VK

    """

    vk_common.dprint(2, vk_common.func_name(),
                     f"Входные данные: id_string = {id_string}")
    try:
        int(id_string)
        id_string = f"id{id_string}"
    except ValueError as error:
        vk_common.dprint(2, vk_common.func_name(), error)

    if vk_common.test_id(id_string):
        user = vk_user.User()
        user.assign_by_nickname(id_string)
        if user.error["error"] != 0:
            if user.error["error"] == 113:
                print(
                    f"Аккаунт ВК с id = \"{id_string}\" не определен в системе."
                )
            else:
                print(
                    f"Возникла ошибка {user.error['error']}: {user.error['msg']}."
                )
            return None, user.error['msg'], user.error["error"]
        else:
            return user, "", 0
    else:
        print("Введенная строка не может быть идентификатором пользователя.")
        return None, "", -1
示例#2
0
    def test_get_common_friends(self):
        other = vk_user.User()
        other.assign_by_nickname('boytsovph')
        self.assertIsInstance(other, vk_user.User)
        err, msg, common_friends = self.user.get_common_friends(other)
        self.assertEqual(len(common_friends), 1)

        other = vk_user.User()
        other.assign_by_nickname('chepel_vn')
        self.assertIsInstance(other, vk_user.User)
        err, msg, common_friends = self.user.get_common_friends(other)
        self.assertEqual(len(common_friends), 718)

        other = vk_user.User()
        other.assign_by_nickname('*')
        self.assertIsInstance(other, vk_user.User)
        err, msg, common_friends = self.user.get_common_friends(other)
        self.assertEqual(len(common_friends), 0)
示例#3
0
 def setUp(self):
     filename_token = "data\\token.txt"
     vk_common.get_token(filename_token)
     self.user = vk_user.User()
     self.user.assign_by_nickname('chepel_vn')
     vk_db.add_user(self.user)
     self.search = vk_search.Search()
     self.user_id_old = list()
     self.user_id_banned = list()
     set_search_param(20, 40, 0)
示例#4
0
 def test_get_additional_info(self):
     other = vk_user.User()
     other.assign_by_nickname('boytsovph')
     self.assertIsInstance(other, vk_user.User)
     self.user.get_additional_info(other)
     self.assertEqual(self.user.cnt_common_friends, 1)
     self.assertEqual(self.user.cnt_common_groups, 2)
     self.assertEqual(self.user.cnt_common_interests, 0)
     self.assertEqual(self.user.cnt_common_books, 0)
     self.assertEqual(self.user.cnt_common_music, 0)
示例#5
0
    def execute_search(self, info_for_search, user_id_old, user_id_banned):
        error, msg, response = search_by(info_for_search)
        if error != 0:
            return error, msg, None

        error, msg, count, items = vk_common.get_count_items_by_response(
            response)
        vk_common.dprint(2, "Парсим результат запроса: ", error, msg, count,
                         items)
        if error != 0:
            return error, msg, None

        if count == 0:
            return 0, "", None

        # Sort out all records of search
        for item in items:
            vk_common.dprint(2, item['id'], user_id_old)
            if item['id'] in user_id_old or item['id'] in user_id_banned:
                # print(f"id = {item['id']} пропустили.")
                continue

            vk_common.dprint(2, "item = ", item)
            user_add = vk_user.User()
            user_add.assign_by_user_info(item)
            vk_common.dprint(
                2, f"Смотрим результат работы user_add.assign_by_user_info: "
                f"{user_add.error['error']}: {user_add.error['msg']}")
            if user_add.error["error"] != 0:
                vk_common.dprint(
                    1, vk_common.func_name(),
                    f"Возникла ошибка {user_add.error['error']}: "
                    f"{user_add.error['msg']}.")
                return -1, f"{user_add.error['error']}: {user_add.error['msg']}", None

            vk_common.dprint(
                2,
                f"user_add.uid = {user_add.uid}, user_add.age = {user_add.age}"
            )

            if user_add.uid is None:
                vk_common.dprint(1, vk_common.func_name(),
                                 f"Параметр user_add.uid = {user_add.uid}")
                return -1, f"{vk_common.func_name()}. Параметр user_add.uid = {user_add.uid}.", None

            if user_add.age is not None:
                self.results.append(user_add)
                vk_common.dprint(2, f"id = {user_add.uid} добавили в список.")
            else:
                vk_common.dprint(
                    2,
                    f"id = {user_add.uid} пропустили потому как возраст не определился."
                )
        return 0, "", self.results
示例#6
0
    def test_add_user(self):
        with patch('vk_db.TABLES_NAMES', TABLES_NAMES1):
            vk_db.clear_db()
            vk_db.add_city({'id': 119, 'title': 'Гуково'})
            data, err = vk_db.get_city_name(119)
            self.assertEqual(data[0][0], "Гуково")

            user = vk_user.User()
            user.assign_by_nickname('chepel_vn')
            user_id, err = vk_db.add_user(user)
            self.assertEqual(err, 0)
            self.assertIsNotNone(user_id)
示例#7
0
    def test_assign_by_nickname(self):
        self.assertEqual(self.user.error['error'], 0)
        self.assertIsNotNone(self.user.uid)

        # Negative test case
        self.user.assign_by_nickname('*')
        self.assertEqual(self.user.error['error'], 113)

        user = vk_user.User()
        user.assign_by_nickname('')
        self.assertEqual(user.error['error'], -1)

        self.user.assign_by_nickname(214191820)
        self.assertEqual(self.user.error['error'], 0)
        self.assertEqual(self.user.uid, 214191820)
示例#8
0
    def test_clear_db(self):
        with patch('vk_db.TABLES_NAMES', TABLES_NAMES1):
            vk_db.add_city({'id': 119, 'title': 'dfghzdgh'})
            rows, err = vk_db.get_count_rows_of_table('vk_cities')
            self.assertGreater(rows[0][0], 0)

            user = vk_user.User()
            user.assign_by_nickname('chepel_vn')
            user_id, err = vk_db.add_user(user)
            self.assertEqual(err, 0)
            self.assertIsNotNone(user_id)
            rows, err = vk_db.get_count_rows_of_table('vk_users')
            self.assertGreater(rows[0][0], 0)
            rows, err = vk_db.clear_db()
            self.assertEqual(err, 0)
            self.assertIsNone(rows)

            for table_name in TABLES_NAMES1:
                rows, err = vk_db.get_count_rows_of_table(table_name)
                self.assertEqual(rows[0][0], 0)
示例#9
0
 def setUp(self):
     filename_token = "data\\token.txt"
     vk_common.get_token(filename_token)
     self.user = vk_user.User()
     self.user.assign_by_nickname('chepel_vn')