Skip to content

Commit

Permalink
Add tests for DraftTournamentView
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Jan 29, 2024
1 parent 963414a commit 90b074e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
22 changes: 13 additions & 9 deletions tournaments/frontend/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,11 @@ def test(self):
response = self.client.get(reverse('publish-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, views.UpdateTournamentView)
self.user1_tournament.refresh_from_db()
self.assertTrue(self.user1_tournament.published)


class PublishTournamentViewTests(TestCase):
class DraftTournamentViewTests(TestCase):

def setUp(self):
user1 = models.User.objects.create(username = 'test1')
Expand All @@ -332,31 +334,33 @@ def setUp(self):
def test_unauthenticated(self):
self.client.logout()

response = self.client.get(reverse('publish-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
response = self.client.get(reverse('draft-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, LoginView)

def test_not_found(self):
response = self.client.get(reverse('publish-tournament', kwargs = dict(pk = 0)))
response = self.client.get(reverse('draft-tournament', kwargs = dict(pk = 0)))
self.assertEqual(response.status_code, 404)

def test_foreign(self):
response = self.client.get(reverse('publish-tournament', kwargs = dict(pk = self.user2_tournament.id)))
response = self.client.get(reverse('draft-tournament', kwargs = dict(pk = self.user2_tournament.id)))
self.assertEqual(response.status_code, 403)

self.user2_tournament.creator = None
self.user2_tournament.save()

response = self.client.get(reverse('publish-tournament', kwargs = dict(pk = self.user2_tournament.id)))
response = self.client.get(reverse('draft-tournament', kwargs = dict(pk = self.user2_tournament.id)))
self.assertEqual(response.status_code, 403)

def test_published(self):
self.user1_tournament.published = True
def test_drafted(self):
self.user1_tournament.published = False
self.user1_tournament.save()
response = self.client.get(reverse('publish-tournament', kwargs = dict(pk = self.user1_tournament.id)))
response = self.client.get(reverse('draft-tournament', kwargs = dict(pk = self.user1_tournament.id)))
self.assertEqual(response.status_code, 412)

def test(self):
response = self.client.get(reverse('publish-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
response = self.client.get(reverse('draft-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, views.UpdateTournamentView)
self.user1_tournament.refresh_from_db()
self.assertFalse(self.user1_tournament.published)
4 changes: 2 additions & 2 deletions tournaments/tournaments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Tournament(models.Model):
creator = models.ForeignKey('auth.User', on_delete = models.SET_NULL, related_name = 'tournaments', null = True, blank = True)

@staticmethod
def load(definition, name, creator = None):
def load(definition, name, **kwargs):
if isinstance(definition, str):
import yaml
definition = yaml.safe_load(definition)
Expand All @@ -31,7 +31,7 @@ def load(definition, name, creator = None):
if len(definition['podium']) == 0:
raise ValidationError('No podium definition given.')

tournament = Tournament.objects.create(name = name, podium_spec = definition['podium'], creator = creator)
tournament = Tournament.objects.create(name = name, podium_spec = definition['podium'], **kwargs)

for stage in definition['stages']:
stage = {key.replace('-', '_'): value for key, value in stage.items()}
Expand Down

0 comments on commit 90b074e

Please sign in to comment.