2017-08-17 20:42:48 +00:00
|
|
|
from django.test import TestCase
|
2017-09-16 15:42:04 +00:00
|
|
|
from games.models import Game, Platform
|
2017-08-17 20:42:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GameTest(TestCase):
|
|
|
|
"""
|
|
|
|
Game Model
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-09-16 15:42:04 +00:00
|
|
|
self.platform = Platform.objects.create(name='BestPlatform4Ever')
|
2017-08-17 20:42:48 +00:00
|
|
|
Game.objects.create(
|
2017-09-03 08:53:17 +00:00
|
|
|
name='Deponia',
|
|
|
|
playing=True,
|
2017-09-16 15:42:04 +00:00
|
|
|
collection=self.platform,
|
2017-09-03 08:53:17 +00:00
|
|
|
status=Game.EXCLUDED)
|
2017-08-17 20:42:48 +00:00
|
|
|
Game.objects.create(
|
2017-09-16 15:42:04 +00:00
|
|
|
name='Aladdin', playing=True, collection=self.platform)
|
2017-08-17 20:42:48 +00:00
|
|
|
Game.objects.create(
|
2017-09-16 15:42:04 +00:00
|
|
|
name='Persona 5', playing=True, collection=self.platform)
|
2017-09-03 08:53:17 +00:00
|
|
|
Game.objects.create(
|
|
|
|
name='The Witcher III',
|
|
|
|
playing=False,
|
2017-09-16 15:42:04 +00:00
|
|
|
collection=self.platform,
|
2017-09-03 08:53:17 +00:00
|
|
|
wish=True)
|
|
|
|
self.index_url = '/games/'
|
2017-08-17 20:42:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_game_are_sorted_by_playing_and_name(self):
|
|
|
|
"""
|
|
|
|
Games should be sorted by playing state (playing should be True first)
|
|
|
|
then by name in alphabetical order.
|
|
|
|
"""
|
|
|
|
games = list(Game.objects.all().values_list('name', flat=True))
|
|
|
|
sorted_games = list(Game.objects.all().order_by(
|
|
|
|
'-playing', 'name').values_list('name', flat=True))
|
|
|
|
self.assertEqual(games, sorted_games)
|
2017-09-03 08:53:17 +00:00
|
|
|
|
|
|
|
def test_index(self):
|
|
|
|
"""
|
|
|
|
Context gives 'playing_games'.
|
|
|
|
Context gives 'last_timelines'.
|
|
|
|
"""
|
|
|
|
res = self.client.get(self.index_url)
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertTrue('playing_games' in res.context)
|
|
|
|
self.assertTrue('last_timelines' in res.context)
|
|
|
|
self.assertTrue('object_list' in res.context)
|
|
|
|
|
|
|
|
def test_index_queryset(self):
|
|
|
|
"""
|
|
|
|
Queryset excludes wishlist games.
|
|
|
|
Queryset excludes games that have status EXCLUDED.
|
|
|
|
Queryset is sorted by name.
|
|
|
|
"""
|
|
|
|
res = self.client.get(self.index_url)
|
|
|
|
games = res.context.get('object_list')
|
|
|
|
for game in games:
|
|
|
|
self.assertFalse(game.wish)
|
|
|
|
self.assertTrue(game.status != Game.EXCLUDED)
|
|
|
|
sorted_games = list(games.order_by('name'))
|
|
|
|
self.assertEqual([x.name for x in games], [s.name for s in sorted_games])
|
|
|
|
|
|
|
|
def test_index_playing_games(self):
|
|
|
|
"""
|
|
|
|
'playing_games' contains games that have playing=True.
|
|
|
|
"""
|
|
|
|
res = self.client.get(self.index_url)
|
|
|
|
playing_games = res.context.get('playing_games')
|
|
|
|
for game in playing_games:
|
|
|
|
self.assertTrue(game.playing)
|
|
|
|
|
|
|
|
def test_index_last_timelines(self):
|
|
|
|
"""
|
|
|
|
'last_timelines' only have 5 items.
|
|
|
|
'last_timelines' have NO games with status EXCLUDED.
|
|
|
|
"""
|
|
|
|
res = self.client.get(self.index_url)
|
|
|
|
games = res.context.get('last_timelines')
|
|
|
|
self.assertEqual(len(games), 5)
|
|
|
|
for game in games:
|
|
|
|
self.assertTrue(game.status != Game.EXCLUDED)
|