add tests for get_first_unfinished_part

This commit is contained in:
Gesche 2021-04-20 00:02:10 +02:00
parent f2bb16aeb2
commit 368653b88a
2 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,88 @@
from typing import List
from django.contrib.auth import get_user_model
from django.test import TestCase
from writingtogether.models import StoryRound, Story, StoryPart
User = get_user_model()
class TestModelFunctions(TestCase):
def create_users_and_story_round_with_stories_and_parts(self):
self.user1 = User.objects.create(username='player1')
self.user2 = User.objects.create(username='player2')
self.story_round = StoryRound.objects.create(
name='test_round',
number_of_rounds=4
)
self.story_round.participants.add(self.user1)
self.story_round.participants.add(self.user2)
self.story1 = Story.objects.create(
name='story1',
part_of_round=self.story_round,
started_by=self.user1,
)
self.story2 = Story.objects.create(
name='story2',
part_of_round=self.story_round,
started_by=self.user2,
)
self.parts1 = self.create_story_parts(self.story1, users=[self.user1, self.user2], num_rounds=4)
self.parts2 = self.create_story_parts(self.story2, users=[self.user1, self.user2], num_rounds=4,
first_user_index=1)
def create_story_parts(
self, story: Story, users: List[User],
num_rounds: int, first_user_index: int = 0
) -> List[StoryPart]:
parts = []
previous_part = None
for i in range(num_rounds):
user = users[(first_user_index + i) % len(users)]
new_part = StoryPart.objects.create(
user=user,
previous_part=previous_part,
part_of=story
)
parts.append(new_part)
previous_part = new_part
return parts
def test_get_first_unfinished_part__nothing_written(self):
self.create_users_and_story_round_with_stories_and_parts()
result = self.story1.get_first_unfinished_part()
self.assertEqual(result, self.parts1[0])
def test_get_first_unfinished_part__one_written(self):
self.create_users_and_story_round_with_stories_and_parts()
self.parts1[0].text = 'some text'
self.parts1[0].save()
result = self.story1.get_first_unfinished_part()
self.assertEqual(result, self.parts1[1])
def test_get_first_unfinished_part__finished(self):
self.create_users_and_story_round_with_stories_and_parts()
self.parts1[0].text = 'some text'
self.parts1[0].save()
self.parts1[1].text = 'some text'
self.parts1[1].save()
self.parts1[2].text = 'some text'
self.parts1[2].save()
self.parts1[3].text = 'some text'
self.parts1[3].save()
self.story1.finished = True
self.story1.save()
result = self.story1.get_first_unfinished_part()
self.assertEqual(result, None)