add integration test for create + next part and fix bugs

- story part of a story needs to start with current user
- turn number needs to be increased
This commit is contained in:
Gesche 2021-04-21 00:23:20 +02:00
parent 82e26a812c
commit cc93abc752
2 changed files with 63 additions and 5 deletions

View File

@ -2,7 +2,7 @@ from django.contrib.auth import get_user_model
from django.test import TestCase from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from writingtogether.models import Story, StoryPart from writingtogether.models import Story, StoryPart, StoryRound
User = get_user_model() User = get_user_model()
@ -89,4 +89,62 @@ class TestViews(TestCase):
# each story has 2 story parts # each story has 2 story parts
self.assertEqual(story_user1_parts.count(), 2) self.assertEqual(story_user1_parts.count(), 2)
self.assertEqual(story_user2_parts.count(), 2) self.assertEqual(story_user2_parts.count(), 2)
self.assertEqual(story_user3_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)

View File

@ -34,7 +34,7 @@ class StoryRoundCreate(CreateView):
sorted_participants = sorted([user.pk for user in form.cleaned_data['participants']]) sorted_participants = sorted([user.pk for user in form.cleaned_data['participants']])
number_of_participants = len(sorted_participants) number_of_participants = len(sorted_participants)
for user_id in sorted_participants: for user_index, user_id in enumerate(sorted_participants):
story = Story.objects.create( story = Story.objects.create(
part_of_round=self.object, part_of_round=self.object,
started_by_id=user_id started_by_id=user_id
@ -43,10 +43,10 @@ class StoryRoundCreate(CreateView):
previous_part=None previous_part=None
for i in range(form.cleaned_data['number_of_rounds']): for i in range(form.cleaned_data['number_of_rounds']):
current_part = StoryPart.objects.create( current_part = StoryPart.objects.create(
user_id=sorted_participants[i % number_of_participants], user_id=sorted_participants[(user_index + i) % number_of_participants],
previous_part=previous_part, previous_part=previous_part,
part_of=story, part_of=story,
turn_number=0 turn_number=i,
) )
previous_part = current_part previous_part = current_part