From a5bcbf0c7ef222559a3b878ef5fb68dbe52bba1d Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Sun, 4 Feb 2018 22:10:56 +0100 Subject: [PATCH] fix #21 - API : ajout des figurines et leurs colllections --- collection/collection/urls.py | 4 ++ collection/figurines/serializers.py | 22 +++++++ collection/figurines/tests/test_api.py | 86 ++++++++++++++++++++++++++ collection/figurines/views.py | 40 ++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 collection/figurines/serializers.py create mode 100644 collection/figurines/tests/test_api.py create mode 100644 collection/figurines/views.py diff --git a/collection/collection/urls.py b/collection/collection/urls.py index c0ee7d7..1257f33 100644 --- a/collection/collection/urls.py +++ b/collection/collection/urls.py @@ -17,6 +17,8 @@ from django.conf import settings from django.conf.urls import include from django.conf.urls import url from django.contrib import admin +from figurines.views import FigurineViewSet +from figurines.views import SetViewSet from games.views import GameList from games.views import GameTimelineViewSet from games.views import GameViewSet @@ -33,7 +35,9 @@ admin.site.site_header = '%s %s' % (admin.site.site_title, app_version) # Django Rest Framework router router = routers.DefaultRouter() router.register(r'games', GameViewSet) +router.register(r'figurines', FigurineViewSet) router.register(r'platforms', PlatformViewSet) +router.register(r'sets', SetViewSet) router.register( r'game_timelines', GameTimelineViewSet, base_name='game_timeline') diff --git a/collection/figurines/serializers.py b/collection/figurines/serializers.py new file mode 100644 index 0000000..cc3a0ec --- /dev/null +++ b/collection/figurines/serializers.py @@ -0,0 +1,22 @@ +from figurines.models import Figurine +from figurines.models import Set +from rest_framework import serializers + + +class SetSerializer(serializers.ModelSerializer): + class Meta: + model = Set + fields = ('name', 'shortname') + + +class FigurineSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Figurine + fields = ( + 'collection', + 'name', + 'kind', + 'achievement', + 'achievement_max', + 'coins', + 'wish', ) diff --git a/collection/figurines/tests/test_api.py b/collection/figurines/tests/test_api.py new file mode 100644 index 0000000..21c505b --- /dev/null +++ b/collection/figurines/tests/test_api.py @@ -0,0 +1,86 @@ +import json + +from django.contrib.auth.models import User +from django.urls import reverse +from figurines.models import Figurine +from figurines.models import Set +from rest_framework import status +from rest_framework.test import APITestCase + + +class SetTest(APITestCase): + @classmethod + def setUpTestData(cls): + cls.superuser = User.objects.create_superuser( + 'admin', 'admin@localhost', 'admin') + + def test_create_set(self): + """ + Check we can create a set object. + """ + url = reverse('set-list') + data = {'name': 'Skypanzers', 'shortname': 'SP'} + self.client.force_authenticate(user=self.superuser) + response = self.client.post(url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(Set.objects.count(), 1) + self.assertEqual(Set.objects.get().name, 'Skypanzers') + + def test_sorted_set(self): + """ + Check that set list is sorted. + """ + Set.objects.create(name='Skypanzers') + Set.objects.create(name='Liibo') + Set.objects.create(name='Skypanzers2') + Set.objects.create(name='Amimoche') + url = reverse('set-list') + self.client.force_authenticate(user=self.superuser) + response = self.client.get(url, format='json') + sorted_sets = list(Set.objects.all().order_by('name') + .values_list('name', flat=True)) + sets = [x.get('name') for x in json.loads(response.content)] + self.assertEqual(sets, sorted_sets) + + +class FigurineTest(APITestCase): + @classmethod + def setUpTestData(cls): + cls.superuser = User.objects.create_superuser( + 'admin', 'admin@localhost', 'admin') + + def test_create_figurine(self): + """ + Check we can create a figurine object. + """ + # Figurine comes from a specific Set + collection = Set.objects.create(name='Skypanzers') + collection_url = reverse('set-detail', kwargs={'pk': collection.id}) + + url = reverse('figurine-list') + data = { + 'name': 'Posséïdon', + 'collection': collection_url, + } + self.client.force_authenticate(user=self.superuser) + response = self.client.post(url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(Figurine.objects.count(), 1) + self.assertEqual(Figurine.objects.get().name, 'Posséïdon') + self.assertEqual(Figurine.objects.get().collection_id, collection.id) + + def test_sorted_figurines(self): + """ + Check that figurine list is sorted. + """ + collection = Set.objects.create(name='Amimoche') + Figurine.objects.create(name='Posséïdon', collection=collection) + Figurine.objects.create(name='Veronica', collection=collection) + Figurine.objects.create(name='Quintrépide', collection=collection) + url = reverse('figurine-list') + self.client.force_authenticate(user=self.superuser) + response = self.client.get(url, format='json') + sorted_figurines = list(Figurine.objects.all().order_by('name') + .values_list('name', flat=True)) + figurines = [x.get('name') for x in json.loads(response.content)] + self.assertEqual(figurines, sorted_figurines) diff --git a/collection/figurines/views.py b/collection/figurines/views.py new file mode 100644 index 0000000..412f8b2 --- /dev/null +++ b/collection/figurines/views.py @@ -0,0 +1,40 @@ +from figurines.models import Figurine +from figurines.models import Set +from rest_framework import viewsets + +from .serializers import FigurineSerializer +from .serializers import SetSerializer + + +class FigurineViewSet(viewsets.ModelViewSet): + """ + API endpoints that allows figurines to be edited or viewed. + + retrieve: + Return the given figurine + + list: + Return a list of all existing figurines ordered by name. + + create: + Create a new figurine instance. + """ + queryset = Figurine.objects.all().order_by('name') + serializer_class = FigurineSerializer + + +class SetViewSet(viewsets.ModelViewSet): + """ + API endpoints that allows to edit or view sets. + + retrieve: + Return the given set + + list: + Return a list of all existing sets ordered by name. + + create: + Create a new set instance. + """ + queryset = Set.objects.all().order_by('name') + serializer_class = SetSerializer