fix #21 - API : ajout des figurines et leurs colllections

This commit is contained in:
Olivier DOSSMANN 2018-02-04 22:10:56 +01:00
parent 286c075ea8
commit a5bcbf0c7e
4 changed files with 152 additions and 0 deletions

View File

@ -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')

View File

@ -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', )

View File

@ -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)

View File

@ -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