def list(self, request): """List the user's achivements and requests.""" self.queryset = AchievementSerializer.setup_eager_loading( self.queryset) self.queryset = self.queryset.filter(user=request.user.profile) serializer = AchievementSerializer(self.queryset, many=True, context={'request': request}) return Response(serializer.data)
def test_serializer_achievement_claimant_sorting(self): # Sorted by date, descending serializer = AchievementSerializer(self.achievement, context={ 'obtain_claimants': True, }) claimants = serializer.data.get("claimants") self.assertEqual(len(claimants), 2) self.assertEqual(claimants[0]['name'], 'test_user') self.assertEqual(claimants[1]['name'], 'test_admin') # Sorted by extra_data_2, descending self.achievement.claimants_sort_field = "extra_data_2" serializer = AchievementSerializer(self.achievement, context={ 'obtain_claimants': True, }) claimants = serializer.data.get("claimants") self.assertEqual(len(claimants), 2) self.assertEqual(claimants[0]['name'], 'test_admin') self.assertEqual(claimants[1]['name'], 'test_user') # Sorted by extra_data_1, ascending self.achievement.claimants_sort_field = "extra_data_1" self.achievement.claimants_sort_ascending = True serializer = AchievementSerializer(self.achievement, context={ 'obtain_claimants': True, }) claimants = serializer.data.get("claimants") self.assertEqual(len(claimants), 2) self.assertEqual(claimants[0]['name'], 'test_admin') self.assertEqual(claimants[1]['name'], 'test_user') # Sorted by extra_data_3, ascending (which are equal), so fallback to date, descending self.achievement.claimants_sort_field = "extra_data_3" serializer = AchievementSerializer(self.achievement, context={ 'obtain_claimants': True, }) claimants = serializer.data.get("claimants") self.assertEqual(len(claimants), 2) self.assertEqual(claimants[0]['name'], 'test_user') self.assertEqual(claimants[1]['name'], 'test_admin')
def test_serializer_achievement_claimants_all(self): serializer = AchievementSerializer(self.achievement, context={ 'obtain_claimants': True, }) self.assertDictEqual( serializer.data, { 'id': 1, 'name': "Test", 'description': "Be a test user", 'unlocked_text': "Unlocked by {0} on {1}", 'image': f"{settings.MEDIA_URL}rubber-duck.jpg", 'claimants': ClaimantSerializer(Claimant.objects.filter( achievement__id=self.achievement.id).order_by( '-date_unlocked'), many=True).data, 'claimants_sort_field': "date_unlocked", 'claimants_sort_ascending': False, 'claimant_count': 2, })
def test_serializer_category_sort_default(self): serializer = CategorySerializer( self.category, context={ 'user_id': 1, 'sort_type': AchievementSortType.ACHIEVEMENTSORT_DEFAULT, 'obtain_claimants': False, }) self.assertDictEqual( serializer.data, { "id": 1, "name": "Testing", "description": "A Category used for testing!", "achievements": AchievementSerializer( Achievement.objects.filter(category__id=self.category.id), many=True, context={ 'user_id': 1, 'obtain_claimants': False }).data, })
def test_serializer_achievement_claimants_none(self): serializer = AchievementSerializer(self.achievement) self.assertDictEqual( serializer.data, { 'id': 1, 'name': "Test", 'description': "Be a test user", 'unlocked_text': "Unlocked by {0} on {1}", 'image': f"{settings.MEDIA_URL}rubber-duck.jpg", 'claimants': [], 'claimants_sort_field': "date_unlocked", 'claimants_sort_ascending': False, 'claimant_count': 2, })