Nouvelle API :
Avec * API sur les consoles en utilisant Django Rest Framework * Tri par nom de console * Nouveaux tests sur l'API * Documentation pour l'API * Ajout d'une description au champ "name" pour Console et sa traduction * Nouvelles dépendances à coreapi et djangorestframework
This commit is contained in:
45
collection/games/tests/test_api.py
Normal file
45
collection/games/tests/test_api.py
Normal file
@ -0,0 +1,45 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
from games.models import Console
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase, force_authenticate
|
||||
import json
|
||||
|
||||
|
||||
class ConsoleTest(APITestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.superuser = User.objects.create_superuser(
|
||||
'admin',
|
||||
'admin@localhost',
|
||||
'admin')
|
||||
|
||||
def test_create_console(self):
|
||||
"""
|
||||
Check we can create a console object.
|
||||
"""
|
||||
url = reverse('console-list')
|
||||
data = {'name': 'GP2X'}
|
||||
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(Console.objects.count(), 1)
|
||||
self.assertEqual(Console.objects.get().name, 'GP2X')
|
||||
|
||||
def test_sorted_console(self):
|
||||
"""
|
||||
Check that console list is sorted.
|
||||
"""
|
||||
Console.objects.create(name='GP2X')
|
||||
Console.objects.create(name='3DS')
|
||||
Console.objects.create(name='Game Boy')
|
||||
Console.objects.create(name='Amiga')
|
||||
url = reverse('console-list')
|
||||
self.client.force_authenticate(user=self.superuser)
|
||||
response = self.client.get(url, format='json')
|
||||
sorted_consoles = list(
|
||||
Console.objects.all().order_by('name').values_list(
|
||||
'name', flat=True))
|
||||
consoles = [x.get('name') for x in json.loads(response.content)]
|
||||
self.assertEqual(consoles, sorted_consoles)
|
Reference in New Issue
Block a user