def run_test_profile_list(self, api_version, profile_serializer_class, profile_params={}, query_dict={}, additional_filter_options={}): profile_type = profile_params.pop('profile_type', None) if not profile_type: (profile_type, _) = ProfileType.objects.get_or_create(value='temporary-type') for _ in range(3): profile = ExtendedUserProfileFactory(profile_type=profile_type, **profile_params) profile.thumbnail = None profile.save() url = reverse('profile_list', args=[api_version + '/']) response = self.client.get('{url}?profile_type={type}&{qs}'.format( url=url, type=profile_type.value, qs=urlencode(query_dict))) response_profiles = json.loads(str(response.content, 'utf-8')) profile_list = profile_serializer_class( UserProfile.objects.filter(profile_type=profile_type, **additional_filter_options), context={ 'user': self.user }, many=True, ).data self.assertListEqual(response_profiles, profile_list)
def run_test_profile_list( self, api_version, profile_serializer_class, profile_params={}, query_dict={}, additional_filter_options={} ): profile_type = profile_params.pop('profile_type', None) if not profile_type: (profile_type, _) = ProfileType.objects.get_or_create(value='temporary-type') for _ in range(3): profile = ExtendedUserProfileFactory(profile_type=profile_type, **profile_params) profile.thumbnail = None profile.save() # Expected queryset ordering = query_dict.get('ordering', '-id').split(',') profile_list = UserProfile.objects.filter( profile_type=profile_type, **additional_filter_options ).order_by(*ordering) url = reverse('profile_list', args=[api_version + '/']) if api_version == settings.API_VERSIONS['version_1'] or api_version == settings.API_VERSIONS['version_2']: # v1 & v2 don't have pagination so we test only once response_profiles = json.loads(str( self.client.get( '{url}?profile_type={type}&{qs}'.format( url=url, type=profile_type.value, qs=urlencode(query_dict) ) ).content, 'utf-8' )) serialized_profile_list = profile_serializer_class( profile_list, context={'user': self.user}, many=True, ).data self.assertListEqual(response_profiles, serialized_profile_list) return None page_size = ProfilesPagination().get_page_size( request=Request(request=HttpRequest()) ) for page_number in range(ceil(len(profile_list) / page_size)): start_index = page_number * page_size end_index = start_index + page_size response_profiles = json.loads(str( self.client.get( '{url}?profile_type={type}&{qs}&page={page_number}'.format( url=url, type=profile_type.value, qs=urlencode(query_dict), page_number=page_number + 1 ) ).content, 'utf-8' ))['results'] serialized_profile_list = profile_serializer_class( profile_list[start_index:end_index], context={'user': self.user}, many=True, ).data self.assertListEqual(response_profiles, serialized_profile_list)