openbackloggery/collection/games/tests/test_api.py

166 lines
6.4 KiB
Python
Raw Permalink Normal View History

2018-01-15 18:06:26 +00:00
import json
from datetime import date
from datetime import datetime
from django.contrib.auth.models import User
from django.urls import reverse
2018-01-15 18:06:26 +00:00
from games.models import Game
from games.models import Platform
from games.models import Timeline
from rest_framework import status
2018-01-15 18:06:26 +00:00
from rest_framework.test import APITestCase
2017-09-16 15:42:04 +00:00
class PlatformTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.superuser = User.objects.create_superuser(
2018-01-15 18:06:26 +00:00
'admin', 'admin@localhost', 'admin')
2017-09-16 15:42:04 +00:00
def test_create_platform(self):
"""
2017-09-16 15:42:04 +00:00
Check we can create a platform object.
"""
2017-09-16 15:42:04 +00:00
url = reverse('platform-list')
2018-01-15 18:06:26 +00:00
data = {'name': 'GP2X', 'shortname': '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)
2017-09-16 15:42:04 +00:00
self.assertEqual(Platform.objects.count(), 1)
self.assertEqual(Platform.objects.get().name, 'GP2X')
2017-09-16 15:42:04 +00:00
def test_sorted_platform(self):
"""
2017-09-16 15:42:04 +00:00
Check that platform list is sorted.
"""
2017-09-16 15:42:04 +00:00
Platform.objects.create(name='GP2X')
Platform.objects.create(name='3DS')
Platform.objects.create(name='Game Boy')
Platform.objects.create(name='Amiga')
url = reverse('platform-list')
self.client.force_authenticate(user=self.superuser)
response = self.client.get(url, format='json')
2018-01-15 18:06:26 +00:00
sorted_platforms = list(Platform.objects.all().order_by('name')
.values_list('name', flat=True))
2017-09-16 15:42:04 +00:00
platforms = [x.get('name') for x in json.loads(response.content)]
self.assertEqual(platforms, sorted_platforms)
class GameTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.superuser = User.objects.create_superuser(
2018-01-15 18:06:26 +00:00
'admin', 'admin@localhost', 'admin')
def test_create_game(self):
"""
Check we can create a game object.
"""
# Game works on specific platform
console = Platform.objects.create(name='Game Boy')
console_url = reverse('platform-detail', kwargs={'pk': console.id})
url = reverse('game-list')
data = {
'name': 'Tetris',
'collection': console_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(Game.objects.count(), 1)
self.assertEqual(Game.objects.get().name, 'Tetris')
self.assertEqual(Game.objects.get().collection_id, console.id)
def test_sorted_games(self):
"""
Check that game list is sorted.
"""
console = Platform.objects.create(name='BestPlatform4Ever')
2018-01-15 18:06:26 +00:00
Game.objects.create(name='Pomperman', collection=console)
Game.objects.create(name='Vektoria', collection=console)
Game.objects.create(name='Qrackovitchya', collection=console)
url = reverse('game-list')
self.client.force_authenticate(user=self.superuser)
response = self.client.get(url, format='json')
sorted_games = list(
2018-01-15 18:06:26 +00:00
Game.objects.all().order_by('name').values_list('name', flat=True))
games = [x.get('name') for x in json.loads(response.content)]
self.assertEqual(games, sorted_games)
def test_not_allowed_status(self):
"""
Check that we cannot insert a not allowed status in a Game
"""
2018-01-15 18:06:26 +00:00
Platform.objects.create(name='BestPlatform4Ever')
url = reverse('game-list')
data = {
'name': 'Vilebrequin',
'status': 'wrecked',
}
self.client.force_authenticate(user=self.superuser)
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Game.objects.count(), 0)
2017-09-16 22:04:26 +00:00
class TimelineTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.superuser = User.objects.create_superuser(
2018-01-15 18:06:26 +00:00
'admin', 'admin@localhost', 'admin')
2017-09-16 22:04:26 +00:00
def test_create_timeline(self):
"""
Check we can create a timeline object.
"""
console = Platform.objects.create(name='BestPlatform4Ever')
game = Game.objects.create(
2018-01-15 18:06:26 +00:00
name='Cherubin', collection=console, status='created')
2017-09-16 22:04:26 +00:00
# By default a timeline is generated with current datetime.
# We need to change this date.
timeline = Timeline.objects.first()
timeline.date = date(2017, 9, 15)
timeline.save()
url = reverse('game_timeline-list')
data = {
'item': reverse('game-detail', kwargs={'pk': game.id}),
'date': date(2017, 9, 16),
'status': 'beaten',
}
self.client.force_authenticate(user=self.superuser)
response = self.client.post(url, data, format='json')
2018-01-15 18:06:26 +00:00
self.assertEqual(response.status_code, status.HTTP_201_CREATED,
response.content)
2017-09-16 22:04:26 +00:00
self.assertEqual(Timeline.objects.count(), 2)
def test_sorted_game_timeline(self):
"""
Check that timelines are sorted by date in descending order.
"""
# Prepare timeline with some different dates
console = Platform.objects.create(name='BestPlatform4Ever')
game = Game.objects.create(
2018-01-15 18:06:26 +00:00
name='Symptomatic', collection=console, status='created')
game.status = 'beaten'
game.save()
game.status = 'completed'
game.save()
# Change date from Timelines
2018-01-15 18:06:26 +00:00
Timeline.objects.filter(status='created').update(date='2017-03-01')
Timeline.objects.filter(status='completed').update(date='2017-03-05')
Timeline.objects.filter(status='beaten').update(date='2017-03-07')
# Check dates
url = reverse('game_timeline-list')
self.client.force_authenticate(user=self.superuser)
response = self.client.get(url, format='json')
# sorted_timelines are datetime.date
2018-01-15 18:06:26 +00:00
sorted_timelines = list(Timeline.objects.all().order_by('-date')
.values_list('date', flat=True))
# dates are string date, format %Y-%m-%d
dates = [x.get('date') for x in json.loads(response.content)]
da = [datetime.date(datetime.strptime(x, '%Y-%m-%d')) for x in dates]
self.assertEqual(da, sorted_timelines)