def test_calculate_score_for_completed_game_5(self): scores = ['X', '7/', '7-2', '9/', 'X', 'X', 'X', '2-3', '6/', 'X-4/'] for score in scores: services.set_frame_score(self.queryset, self.game_registration.game_id, score) game_object = services.get_frame_score(self.queryset, self.game_registration.game_id) assert game_object == game_models.Game(self.game_registration.game_id, 178)
def test_calculate_score_for_all_strikes(self): scores = ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X-X-X'] for score in scores: services.set_frame_score(self.queryset, self.game_registration.game_id, score) game_object = services.get_frame_score(self.queryset, self.game_registration.game_id) assert game_object == game_models.Game(self.game_registration.game_id, 300)
def test_calculate_score_for_active_game(self): scores = ['X', '7/', '7-2', '9/', 'X', 'X', 'X'] for score in scores: services.set_frame_score(self.queryset, self.game_registration.game_id, score) game_object = services.get_frame_score(self.queryset, self.game_registration.game_id) assert game_object == game_models.Game(self.game_registration.game_id, 96)
def test_set_frame_score__multiple_frames(self): services.set_frame_score(self.queryset, self.game_registration.game_id, 'X') score_object = services.set_frame_score(self.queryset, self.game_registration.game_id, '7/') assert score_object.errors == [] assert score_object.first_attempt_score == '7' assert score_object.second_attempt_score == 3 assert score_object.third_attempt_score == 0 assert score_object.frame == 2 assert score_object.frame_score is None assert score_object.frame_version == 1
def test_set_frame_score__game_has_been_played(self): scores = ['X', '7/', '7-2', '9/', 'X', 'X', 'X', '2-3', '6/', '7/3'] for score in scores: services.set_frame_score(self.queryset, self.game_registration.game_id, score) score_object = services.set_frame_score(self.queryset, self.game_registration.game_id, 'X') assert score_object.errors == [ game_models.Error( error_code=400, error_message='Game:\'{}\' has already been played.'.format( self.game_registration.game_id)) ] assert score_object.frame_score is None assert score_object.first_attempt_score is None assert score_object.second_attempt_score is None assert score_object.third_attempt_score is None
def test_set_frame_score__race_condition_version_raises_error(self): services.set_frame_score(self.queryset, self.game_registration.game_id, 'X') with mock.patch('django.db.models.query.QuerySet.values_list', return_value=mock_query.MockSet((0,))), \ mock.patch('django.db.models.query.QuerySet.aggregate', return_value={'frame_max': 0}): score_object = services.set_frame_score( self.queryset, self.game_registration.game_id, '2-3') assert score_object.errors == [ game_models.Error( error_code=500, error_message=('Unable to save score: \'2-3\' for ' 'game: \'{game}\'.'.format( game=self.game_registration.game_id))) ] assert score_object.frame_score is None assert score_object.first_attempt_score is None assert score_object.second_attempt_score is None assert score_object.third_attempt_score is None
def test_set_frame_score__create_first_frame_spare(self): score_object = services.set_frame_score(self.queryset, self.game_registration.game_id, '3/') assert score_object.errors == [] assert score_object.first_attempt_score == '3' assert score_object.second_attempt_score == 7 assert score_object.third_attempt_score == 0 assert score_object.frame == 1 assert score_object.frame_score is None assert score_object.total_score_for_frame is None assert score_object.frame_version == 1
def test_set_frame_score__invalid_score_format(self): score_object = services.set_frame_score(self.queryset, self.game_registration.game_id, 'XX') assert score_object.errors == [ game_models.Error(error_code=400, error_message='Score format: XX is invalid.') ] assert score_object.frame_score is None assert score_object.first_attempt_score is None assert score_object.second_attempt_score is None assert score_object.third_attempt_score is None
def test_set_frame_score__game_object_not_found(self): score_object = services.set_frame_score(self.queryset, 'abcde12345', 'X') assert score_object.errors == [ game_models.Error( error_code=404, error_message='No game was found for the game id: abcde12345.') ] assert score_object.frame_score is None assert score_object.first_attempt_score is None assert score_object.second_attempt_score is None assert score_object.third_attempt_score is None
def test_set_frame_score__last_attempt_strike_spare(self): scores = ['X', '7/', '7-2', '9/', 'X', 'X', 'X', '2-3', '6/'] for score in scores: services.set_frame_score(self.queryset, self.game_registration.game_id, score) score_object = services.set_frame_score(self.queryset, self.game_registration.game_id, 'X-7/') assert score_object.errors == [] assert score_object.frame_score == 20 assert score_object.first_attempt_score == 'X' assert score_object.second_attempt_score == '7' assert score_object.third_attempt_score == 3 # Asserting total frames total_scores = [ score for score in game_models.ScorePerFrame.objects.select_related( 'game').filter(game=self.game_registration.game_id). values_list('total_score_for_frame', flat=True) ] assert total_scores == [20, 37, 46, 66, 96, 118, 133, 138, 158, 178]
def set_score(self, request, game_id, score): """Sets the score of the given frame.""" response = bowling_services.set_frame_score(self.get_queryset(), game_id, score) return serialized_object(self.get_serializer_class(), response, status.HTTP_200_OK)