from django.contrib.auth import get_user_model from django.test import TestCase from django.urls import reverse from writingtogether.models import Story, StoryPart User = get_user_model() class TestViews(TestCase): def setUp(self) -> None: self.user1 = User.objects.create(username='player1') self.user2 = User.objects.create(username='player2') def test_create_story_round_two_rounds(self): response = self.client.post( reverse('writing:create_story_round'), { 'name': 'test round', 'participants': [self.user1.pk, self.user2.pk], 'number_of_rounds': 2 } ) self.assertEqual(response.status_code, 302) self.assertEqual(Story.objects.all().count(), 2) self.assertEqual(StoryPart.objects.all().count(), 4) story_user1 = Story.objects.get(started_by=self.user1.pk) story_user2 = Story.objects.get(started_by=self.user2.pk) story_user1_parts = StoryPart.objects.filter(part_of=story_user1) story_user2_parts = StoryPart.objects.filter(part_of=story_user2) # each story has 2 story parts self.assertEqual(story_user1_parts.count(), 2) self.assertEqual(story_user2_parts.count(), 2) def test_create_story_round_4_rounds(self): response = self.client.post( reverse('writing:create_story_round'), { 'name': 'test round', 'participants': [self.user1.pk, self.user2.pk], 'number_of_rounds': 4 } ) self.assertEqual(response.status_code, 302) self.assertEqual(Story.objects.all().count(), 2) self.assertEqual(StoryPart.objects.all().count(), 8) story_user1 = Story.objects.get(started_by=self.user1.pk) story_user2 = Story.objects.get(started_by=self.user2.pk) story_user1_parts = StoryPart.objects.filter(part_of=story_user1) story_user2_parts = StoryPart.objects.filter(part_of=story_user2) # each story has 2 story parts self.assertEqual(story_user1_parts.count(), 4) self.assertEqual(story_user2_parts.count(), 4) def test_create_story_round_three_users_two_rounds(self): user3 = User.objects.create(username='player3') response = self.client.post( reverse('writing:create_story_round'), { 'name': 'test round', 'participants': [self.user1.pk, self.user2.pk, user3.pk], 'number_of_rounds': 2 } ) self.assertEqual(response.status_code, 302) self.assertEqual(Story.objects.all().count(), 3) self.assertEqual(StoryPart.objects.all().count(), 6) story_user1 = Story.objects.get(started_by=self.user1.pk) story_user2 = Story.objects.get(started_by=self.user2.pk) story_user3 = Story.objects.get(started_by=user3.pk) story_user1_parts = StoryPart.objects.filter(part_of=story_user1) story_user2_parts = StoryPart.objects.filter(part_of=story_user2) story_user3_parts = StoryPart.objects.filter(part_of=story_user3) # each story has 2 story parts self.assertEqual(story_user1_parts.count(), 2) self.assertEqual(story_user2_parts.count(), 2) self.assertEqual(story_user3_parts.count(), 2)