def test_users_save_load(self):
        mock_users_url = 'http://localhost:{port}/'.format(
            port=self.mock_server_port)
        with mock.patch('сlasses.vk_api_client.VkApiClient.API_BASE_URL',
                        new_callable=mock.PropertyMock) as mock_f:
            mock_f.return_value = mock_users_url
            self.api = VkApiClient(token='',
                                   app_id='',
                                   user_id='1',
                                   debug_mode=True)
            assert self.api.is_initialized
            users = self.api.get_users(['1', '5', 1])
            client_1 = VKinderClient(users[0])
            assert client_1
            client_2 = VKinderClient(users[1])
            assert client_2

            self.db.save_client(client_1)
            self.db.save_client(client_2)

            for i in range(self.db.search_history_limit + 1):
                client_1.reset_search()
                client_2.reset_search()

                client_1.search.sex_id = randrange(0, 2, 1)
                client_1.search.status_id = randrange(1, 8, 1)
                client_1.search.city_id = 1
                client_1.search.city_name = 'Москва'
                client_1.search.min_age = randrange(0, 60, 1)
                client_1.search.max_age = randrange(client_1.search.min_age,
                                                    127, 1)
                client_1.rating_filter = 0

                client_2.search.sex_id = randrange(0, 2, 1)
                client_2.search.status_id = randrange(1, 8, 1)
                client_2.search.city_id = 2
                client_2.search.city_name = 'Санкт-Петербург'
                client_2.search.min_age = randrange(0, 60, 1)
                client_2.search.max_age = randrange(client_2.search.min_age,
                                                    127, 1)
                client_2.rating_filter = 0

                self.db.save_search(client_1)
                self.db.save_search(client_2)

                client_1.found_users = self.api.search_users()
                client_2.found_users = self.api.search_users(q='babych')
                assert len(client_1.found_users) > 0
                assert len(client_2.found_users) > 0

                self.db.save_users(client_1)
                self.db.save_users(client_2)
Пример #2
0
 def do_users_search(self, client: VKinderClient):
     client.status = STATUSES['loading_users']
     params = PHRASES['city_x_sex_x_status_x_age_xx'].format(
         client.search.city_name, SEXES[client.search.sex_id],
         LOVE_STATUSES[client.search.status_id], client.search.min_age,
         client.search.max_age)
     self.send_msg(client,
                   f'{PHRASES["started_search_peoples"]}\n({params})')
     self.send_typing_activity(client)
     self.db.save_search(client)
     client.found_users = self.vk_personal.search_users(
         city_id=client.search.city_id,
         sex_id=client.search.sex_id,
         love_status_id=client.search.status_id,
         age_from=client.search.min_age,
         age_to=client.search.max_age)
     client.found_users = [
         user for user in client.found_users
         if not user.is_closed and user.last_seen_time
     ]
     client.found_users.sort(key=lambda x: x.last_seen_time, reverse=True)
     if client.found_users:
         self.db.load_users_ratings_from_db(client)
         ratings_sum = get_users_ratings_counts(client.found_users)
         self.send_msg(
             client,
             PHRASES['found_x_peoples_x_new_x_liked_x_disliked_x_banned'].
             format(len(client.found_users), ratings_sum['new'],
                    ratings_sum['liked'], ratings_sum['disliked'],
                    ratings_sum['banned']))
         if ratings_sum['new'] > 0:
             self.db.save_users(client)
             self.do_show_next_user(client)
         else:
             self.send_msg(client, PHRASES['no_new_peoples_found'])
             self.do_propose_start_search(client)
     else:
         self.send_msg(client, PHRASES['no_peoples_found'])
         self.do_propose_start_search(client)
 def load_users_from_db(self, client: VKinderClient):
     """
     Gets all rated users by client, using rating as filter
     """
     log(f'[{client.fname} {client.lname}] Loading users from DB with rating {client.rating_filter}',
         is_debug_msg=self.debug_mode)
     users = self.__session.query(Users).join(ClientsUsers).filter(
         ClientsUsers.client_id == client.db_id).filter(
             ClientsUsers.rating_id == client.rating_filter).all()
     client.found_users = []
     for user in users:
         client.found_users.append(
             user.convert_to_ApiUser(client.rating_filter))
     log(f'[{client.fname} {client.lname}] Loaded {len(client.found_users)} users from DB',
         is_debug_msg=self.debug_mode)