Beispiel #1
0
 def test_it_raises_invalid_when_no_playing_sets(self):
     action = SubPointsAction()
     match = MatchFactory()
     request = get_fake_request(get_params={'token': match.token})
     with pytest.raises(BadRequestError) as e:
         action.validate(request, match_id=match.id, team='team_one')
     assert e.value.error_code == INVALID_SET_STATUS
Beispiel #2
0
 def test_it_raises_bad_request_when_no_token_provided(self):
     action = SubPointsAction()
     request = get_fake_request()
     match = MatchFactory()
     with pytest.raises(BadRequestError) as e:
         action.validate(request, match_id=match.id, team='team_one')
     assert e.value.error_code == INVALID_TOKEN
Beispiel #3
0
 def test_it_raises_invalid_when_invalid_game_status(self):
     action = SubPointsAction()
     match = MatchFactory(game_status=Match.GameStatus.FINISHED.value)
     request = get_fake_request(get_params={'token': match.token})
     with pytest.raises(BadRequestError) as e:
         action.validate(request, match_id=match.id, team='team_one')
     assert e.value.error_code == INVALID_MATCH_STATUS
Beispiel #4
0
 def test_it_does_not_raise_error_when_valid_id_and_write_match_in_common(
         self):
     action = GetMatchByIdAction()
     match = MatchFactory()
     request = get_fake_request()
     action.validate(request, match_id=match.id)
     assert action.common.get('match').id == match.id
Beispiel #5
0
 def test_some(self):
     action = PaginatedBaseAction()
     request = get_fake_request(get_params={'page': 0})
     with pytest.raises(BadRequestError) as e:
         action.validate(request)
         assert e.error_message == 'Page number must be positive'
         assert e.error_code == INVALID_PAGE
Beispiel #6
0
 def test_it_raises_bad_request_when_not_odd_sets_number(self):
     action = CreateMatchAction()
     schema = copy.deepcopy(valid_schema)
     schema['sets_number'] = 4
     request = get_fake_request(body=json.dumps(schema))
     with pytest.raises(BadRequestError) as e:
         action.validate(request)
     assert e.value.error_code == REQUIRED_TO_BE_ODD
Beispiel #7
0
 def test_it_raises_invalid_when_invalid_team(self):
     action = SubPointsAction()
     match = MatchFactory()
     request = get_fake_request(get_params={'token': match.token})
     with pytest.raises(BadRequestError) as e:
         action.validate(request,
                         match_id=match.id,
                         team='no_existing_team')
     assert e.value.error_code == INVALID_TEAM_SELECTION
Beispiel #8
0
    def test_atomic_transaction_not_create_partial_match(self, generate_access_code_mock):
        generate_access_code_mock.side_effect = Exception

        action = CreateMatchAction()
        action.common['body'] = copy.deepcopy(valid_schema)
        request = get_fake_request()
        with pytest.raises(Exception):
            action.run(request)

        created_matches = Match.objects.all()
        created_teams = Team.objects.all()

        assert created_teams.count() == 0
        assert created_matches.count() == 0
Beispiel #9
0
    def test_it_substract_one_to_the_correct_team_response_and_db(self):
        action = SubPointsAction()
        request = get_fake_request()
        match = MatchFactory()
        match.teams.add(*[TeamFactory() for i in range(2)])
        SetFactory(match=match, team_one_points=1, set_number=1)
        action.common['match'] = match
        response = json.loads(
            action.run(request=request, match_id=match.id,
                       team='team_one').content)
        sets = Set.objects.filter(match_id=match.id)

        assert len(response['match']['sets']) == 1
        assert response['match']['sets'][0][
            'game_status'] == Set.GameStatus.PLAYING.value
        assert response['match']['sets'][0]['team_one_points'] == 0
        assert response['match']['sets'][0]['team_two_points'] == 0
        assert response['match']['sets'][0]['match_id'] == match.id
        assert response['match']['sets'][0]['set_number'] == 1
        assert response['match']['sets'][0]['is_tie_break'] is False

        assert response['match']['sets_number'] == match.sets_number
        assert response['match'][
            'set_points_number'] == match.set_points_number
        assert response['match'][
            'points_difference'] == match.points_difference
        assert response['match']['tie_break_points'] == match.tie_break_points
        assert response['match']['teams'][0]['name'] == match.teams.all(
        )[0].name
        assert response['match']['teams'][0]['color'] == match.teams.all(
        )[0].color
        assert response['match']['teams'][1]['name'] == match.teams.all(
        )[1].name
        assert response['match']['teams'][1]['color'] == match.teams.all(
        )[1].color

        match_db = Match.objects.get(id=match.id)

        assert sets.count() == 1
        assert match_db.sets_number == match.sets_number
        assert match_db.set_points_number == match.set_points_number
        assert match_db.points_difference == match.points_difference
        assert match_db.tie_break_points == match.tie_break_points
        assert match_db.game_status == Match.GameStatus.PLAYING.value
        assert len(sets) == 1
        assert sets[0].game_status == Set.GameStatus.PLAYING.value
        assert sets[0].team_one_points == 0
        assert sets[0].team_two_points == 0
Beispiel #10
0
 def test_it_returns_match(self):
     action = GetMatchByIdAction()
     match = MatchFactory.build()
     request = get_fake_request()
     action.common = {'match': match}
     result = json.loads(action.run(request, match_id=match.id).content)
     expected = {
         'match': {
             'id': match.id,
             'sets_number': match.sets_number,
             'access_code': match.access_code,
             'status': match.status,
             'game_status': match.game_status,
             'set_points_number': match.set_points_number,
             'points_difference': match.points_difference,
             'tie_break_points': match.tie_break_points,
             'teams': [],
             'sets': [],
             'winner_team': match.winner_team,
         }
     }
     assert expected == result
Beispiel #11
0
    def test_it_create_requested_match_and_teams(self):
        action = CreateMatchAction()
        action.common['body'] = copy.deepcopy(valid_schema)
        request = get_fake_request()
        response = json.loads(action.run(request).content)
        created_matches = Match.objects.all()
        created_teams = Team.objects.filter(match=created_matches[0]).order_by('id')
        created_sets = Set.objects.filter(match_id=created_matches[0].id)

        assert len(response['match']['sets']) == 1
        assert response['match']['sets'][0]['game_status'] == 0
        assert response['match']['sets'][0]['team_one_points'] == 0
        assert response['match']['sets'][0]['team_two_points'] == 0
        assert response['match']['sets'][0]['match_id'] == created_matches[0].id
        assert response['match']['sets'][0]['set_number'] == 1
        assert response['match']['sets'][0]['is_tie_break'] is False
        assert response['match']['sets_number'] == valid_schema['sets_number']
        assert response['match']['set_points_number'] == valid_schema['set_points_number']
        assert response['match']['points_difference'] == valid_schema['points_difference']
        assert response['match']['tie_break_points'] == valid_schema['tie_break_points']
        assert response['match']['token'] == created_matches[0].token
        assert response['match']['teams'][0]['name'] == valid_schema['teams'][0]['name']
        assert response['match']['teams'][0]['color'] == valid_schema['teams'][0]['color']
        assert response['match']['teams'][1]['name'] == valid_schema['teams'][1]['name']
        assert response['match']['teams'][1]['color'] == valid_schema['teams'][1]['color']

        assert created_sets.count() == 1
        assert created_teams.count() == 2
        assert created_matches.count() == 1
        assert created_matches[0].sets_number == valid_schema['sets_number']
        assert created_matches[0].set_points_number == valid_schema['set_points_number']
        assert created_matches[0].points_difference == valid_schema['points_difference']
        assert created_matches[0].tie_break_points == valid_schema['tie_break_points']
        assert created_teams[0].name == valid_schema['teams'][0]['name']
        assert created_teams[0].color == valid_schema['teams'][0]['color']
        assert created_teams[1].name == valid_schema['teams'][1]['name']
        assert created_teams[1].color == valid_schema['teams'][1]['color']
Beispiel #12
0
 def test_it_does_not_raise_error_when_valid_request(self):
     action = SubPointsAction()
     match = MatchFactory()
     request = get_fake_request(get_params={'token': match.token})
     SetFactory(match=match, team_one_points=1)
     action.validate(request, match_id=match.id, team='team_one')
Beispiel #13
0
 def test_it_raises_not_found_when_invalid_id(self):
     action = SubPointsAction()
     request = get_fake_request()
     with pytest.raises(NotFoundError) as e:
         action.validate(request, match_id=1, team='team_one')
     assert e.value.error_code == INVALID_MATCH_ID
Beispiel #14
0
 def test_it_raises_not_found_when_invalid_id(self):
     action = GetMatchByIdAction()
     request = get_fake_request()
     with pytest.raises(NotFoundError) as e:
         action.validate(request, match_id=1)
     assert e.value.error_code == INVALID_MATCH_ID
Beispiel #15
0
 def test_it_does_not_raise_invalid_when_valid_request(self):
     action = AddPointsAction()
     request = get_fake_request()
     match = MatchFactory()
     request = get_fake_request(get_params={'token': match.token})
     action.validate(request, match_id=match.id, team='team_one')
Beispiel #16
0
 def test_it_does_not_raise_bad_request_when_odd_sets_number(self):
     action = CreateMatchAction()
     request = get_fake_request(body=json.dumps(valid_schema))
     action.validate(request)