from django.contrib.auth import get_user_model from django.test import TestCase from django.urls import reverse from writingtogether.models import Story, StoryPart, StoryRound User = get_user_model() class TestViews(TestCase): def setUp(self) -> None: self.user1 = User.objects.create_user(username='player1', password='12345') self.user2 = User.objects.create_user(username='player2') self.client.login(username='player1', password='12345') 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) def test_integration_create_and_next_part(self): response = self.client.post( reverse('writing:create_story_round'), { 'name': 'test round', 'participants': [self.user1.pk, self.user2.pk], 'number_of_rounds': 2 } ) story_round = StoryRound.objects.get() result = story_round.get_next_story_part(self.user1) story_user1 = Story.objects.get(started_by=self.user1.pk) self.assertEqual(result.part_of, story_user1) self.assertEqual(result.turn_number, 0) self.assertEqual(result.user_id, self.user1.pk) def test_integration_create_and_next_part__more_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 } ) story_round = StoryRound.objects.get() result = story_round.get_next_story_part(self.user1) story_user1 = Story.objects.get(started_by=self.user1.pk) self.assertEqual(result.part_of, story_user1) self.assertEqual(result.turn_number, 0) self.assertEqual(result.user_id, self.user1.pk) def test_integration_create_and_next_part__second_round(self): response = self.client.post( reverse('writing:create_story_round'), { 'name': 'test round', 'participants': [self.user1.pk, self.user2.pk], 'number_of_rounds': 4 } ) story_round = StoryRound.objects.get() story_round.current_turn = 1 story_round.save() result = story_round.get_next_story_part(self.user1) story_user2 = Story.objects.get(started_by=self.user2.pk) self.assertEqual(result.part_of, story_user2) self.assertEqual(result.turn_number, 1) self.assertEqual(result.user_id, self.user1.pk)