From 6133fb1891ecd3605cd2d92866849be7fb65593d Mon Sep 17 00:00:00 2001 From: Gesche Gierse Date: Tue, 20 Apr 2021 00:25:58 +0200 Subject: [PATCH] add function to determine if all players are ready for the next round; fix tests --- stories/writingtogether/models.py | 6 ++++ .../tests/test_model_functions.py | 32 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/stories/writingtogether/models.py b/stories/writingtogether/models.py index 27baa61..01f65ce 100644 --- a/stories/writingtogether/models.py +++ b/stories/writingtogether/models.py @@ -20,6 +20,12 @@ class StoryRound(models.Model): # TODO: implement, for each story get unfinished part and check user pass + def next_round_ready(self) -> bool: + for story in self.story_set.all(): + if not story.storypart_set.get(turn_number=self.current_turn).finished: + return False + return True + #class Participant(models.Model): # user = models.ForeignKey(User, on_delete=CASCADE) diff --git a/stories/writingtogether/tests/test_model_functions.py b/stories/writingtogether/tests/test_model_functions.py index db0a5f0..1f5d1dc 100644 --- a/stories/writingtogether/tests/test_model_functions.py +++ b/stories/writingtogether/tests/test_model_functions.py @@ -48,7 +48,7 @@ class TestModelFunctions(TestCase): user=user, previous_part=previous_part, part_of=story, - round_number=i + turn_number=i ) parts.append(new_part) previous_part = new_part @@ -87,3 +87,33 @@ class TestModelFunctions(TestCase): 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())