Skip to content

Commit

Permalink
Add tests for CloneTournamentView
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Jan 29, 2024
1 parent 72040a0 commit 6d43b87
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
5 changes: 1 addition & 4 deletions tournaments/frontend/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ def clean_definition(self):
return definition

def create_tournament(self, request):
tournament = models.Tournament.load(
return models.Tournament.load(
definition = self.cleaned_data['definition'],
name = self.cleaned_data['name'],
creator = request.user)
tournament.definition = self.data['definition']
tournament.save()
return tournament


class UpdateTournamentForm(CreateTournamentForm):
Expand Down
56 changes: 56 additions & 0 deletions tournaments/frontend/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,59 @@ def test(self):
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, views.UpdateTournamentView)
self.assertFalse(self.user1 in self.user1_tournament.participants)


class CloneTournamentViewTests(TestCase):

def setUp(self):
self.user1 = models.User.objects.create(username = 'test1')
self.user2 = models.User.objects.create(username = 'test2')
self.client.force_login(self.user1)

self.user1_tournament = models.Tournament.load(definition = test_tournament1_yml, name = 'Test1', creator = self.user1, published = True)
self.user2_tournament = models.Tournament.load(definition = test_tournament1_yml, name = 'Test2', creator = self.user2, published = True)

def test_unauthenticated(self):
self.client.logout()

response = self.client.get(reverse('clone-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('clone-tournament', kwargs = dict(pk = 0)))
self.assertEqual(response.status_code, 404)

def test_foreign(self):
response = self.client.get(reverse('clone-tournament', kwargs = dict(pk = self.user2_tournament.id)), follow = True)
clone = models.Tournament.objects.get(name = 'Test2 (Copy)')
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, views.UpdateTournamentView)
self.assertEqual(clone.definition, self.user1_tournament.definition)
self.assertEqual(clone.creator.id, self.user1.id)

def test_drafted(self):
self.user1_tournament.published = False
self.user1_tournament.save()
response = self.client.get(reverse('clone-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
clone = models.Tournament.objects.get(name = 'Test1 (Copy)')
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, views.UpdateTournamentView)
self.assertEqual(clone.definition, self.user1_tournament.definition)
self.assertEqual(clone.creator.id, self.user1.id)

def test_active(self):
start_tournament(self.user1_tournament)
response = self.client.get(reverse('clone-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
clone = models.Tournament.objects.get(name = 'Test1 (Copy)')
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, views.UpdateTournamentView)
self.assertEqual(clone.definition, self.user1_tournament.definition)
self.assertEqual(clone.creator.id, self.user1.id)

def test(self):
response = self.client.get(reverse('clone-tournament', kwargs = dict(pk = self.user1_tournament.id)), follow = True)
clone = models.Tournament.objects.get(name = 'Test1 (Copy)')
self.assertEqual(response.status_code, 200)
self.assertIs(response.resolver_match.func.view_class, views.UpdateTournamentView)
self.assertEqual(clone.definition, self.user1_tournament.definition)
2 changes: 0 additions & 2 deletions tournaments/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,5 @@ def get(self, request, *args, **kwargs):
definition = self.object.definition,
name = self.object.name + ' (Copy)',
creator = request.user)
tournament.definition = self.object.definition
tournament.save()
request.session['alert'] = dict(status = 'success', text = f'A copy of the tournament "{ self.object.name }" has been created (see below).')
return redirect('update-tournament', pk = tournament.id)
3 changes: 2 additions & 1 deletion tournaments/tournaments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Tournament(models.Model):

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

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

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

0 comments on commit 6d43b87

Please sign in to comment.