145 lines
5.0 KiB
Python
145 lines
5.0 KiB
Python
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,
|
|
turn_number=i
|
|
)
|
|
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)
|
|
|
|
def test_next_round_ready__game_start(self):
|
|
self.create_users_and_story_round_with_stories_and_parts()
|
|
self.assertFalse(self.story_round.next_round_ready())
|
|
|
|
def test_next_round_ready__half_done(self):
|
|
self.create_users_and_story_round_with_stories_and_parts()
|
|
self.parts1[0].text = 'some text'
|
|
self.parts1[0].save()
|
|
self.assertFalse(self.story_round.next_round_ready())
|
|
|
|
def test_next_round_ready__first_round_done(self):
|
|
self.create_users_and_story_round_with_stories_and_parts()
|
|
|
|
self.parts1[0].text = 'some text'
|
|
self.parts1[0].save()
|
|
self.parts2[0].text = 'some text'
|
|
self.parts2[0].save()
|
|
self.assertTrue(self.story_round.next_round_ready())
|
|
|
|
def test_next_round_ready__second_round_done(self):
|
|
self.create_users_and_story_round_with_stories_and_parts()
|
|
self.story_round.current_turn = 1
|
|
self.story_round.save()
|
|
|
|
self.parts1[1].text = 'some text'
|
|
self.parts1[1].save()
|
|
self.parts2[1].text = 'some text'
|
|
self.parts2[1].save()
|
|
self.assertTrue(self.story_round.next_round_ready())
|
|
|
|
def test_get_next_story_part__each_user_starts_with_own_story(self):
|
|
self.create_users_and_story_round_with_stories_and_parts()
|
|
|
|
result = self.story_round.get_next_story_part(self.user1)
|
|
self.assertEqual(result, self.parts1[0])
|
|
|
|
result = self.story_round.get_next_story_part(self.user2)
|
|
self.assertEqual(result, self.parts2[0])
|
|
|
|
def test_get_next_story_part__second_round_story_switched(self):
|
|
self.create_users_and_story_round_with_stories_and_parts()
|
|
self.parts1[0].text = 'some text'
|
|
self.parts1[0].save()
|
|
|
|
self.parts2[0].text = 'some text'
|
|
self.parts2[0].save()
|
|
|
|
self.story_round.current_turn = 1
|
|
self.story_round.save()
|
|
|
|
result = self.story_round.get_next_story_part(self.user1)
|
|
self.assertEqual(result, self.parts2[1])
|
|
|
|
result = self.story_round.get_next_story_part(self.user2)
|
|
self.assertEqual(result, self.parts1[1]) |