def setUp(self):
     players = [
         gen_player(
             first_name="Fryderyk",
             last_name="Chopin",
             email="*****@*****.**",
         ),
         gen_player(
             first_name="Immanuel",
             last_name="Kant",
             email="*****@*****.**",
         )
     ]
     tournament = Tournament(name='tournament_test_1',
                             date_beginning='2019-01-02 10:00:00',
                             date_ending='2019-01-03 10:00:00')
     # TEMP: set players
     tournament.save()
     tournament.players.set(players)
     self.match = Match(player_1_id=players[0].id,
                        player_2_id=players[1].id,
                        tournament_id=tournament.id,
                        player_1_score=1,
                        player_2_score=2,
                        draws=0,
                        round=1)
     self.match.save()
 def setUp(self):
     players = [
         gen_player(
             first_name="Fryderyk",
             last_name="Chopin",
             email="*****@*****.**",
         ),
         gen_player(
             first_name="Immanuel",
             last_name="Kant",
             email="*****@*****.**",
         )
     ]
     self.tournaments = [
         Tournament(name='tournament_test_1',
                    date_beginning='2019-01-02 10:00:00',
                    date_ending='2019-01-03 10:00:00'),
         Tournament(name='tournament_test_2',
                    date_beginning='2019-02-02 10:00:00',
                    date_ending='2019-02-03 10:00:00')
     ]
     for tournament in self.tournaments:
         tournament.save()
         tournament.players.set(players)
         tournament.save()
    def test_match_delete_404_tournament_missing(self):
        tournament = Tournament(name='tournament_delete_test',
                                date_beginning='2019-01-02 10:00:00',
                                date_ending='2019-01-03 10:00:00')
        tournament.save()

        response = client.delete(
            f'{self.base_url}/{tournament.id}/matches/{self.match.id}/')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
    def setUp(self):
        players = [
            gen_player(
                first_name="Fryderyk",
                last_name="Chopin",
                email="*****@*****.**",
            ),
            gen_player(
                first_name="Immanuel",
                last_name="Kant",
                email="*****@*****.**",
            ),
            gen_player(
                first_name="Muniek",
                last_name="Kot",
                email="*****@*****.**",
            )
        ]
        tournament = Tournament(name='tournament_test_1',
                                date_beginning='2019-01-02 10:00:00',
                                date_ending='2019-01-03 10:00:00')
        # TEMP: set players
        tournament.save()
        tournament.players.set(players)
        self.matches = [
            Match(player_1_id=players[0].id,
                  player_2_id=players[1].id,
                  tournament_id=tournament.id,
                  player_1_score=1,
                  player_2_score=2,
                  draws=0,
                  round=1),
            Match(player_1_id=players[1].id,
                  player_2_id=players[2].id,
                  tournament_id=tournament.id,
                  player_1_score=2,
                  player_2_score=0,
                  draws=0,
                  round=2)
        ]
        for match in self.matches:
            match.save()

        # tournament_2 made to check if matches from another tournament
        # don't show up in match_list
        tournament_2 = Tournament(name='tournament_test_2',
                                  date_beginning='2019-01-02 10:00:00',
                                  date_ending='2019-01-03 10:00:00')
        tournament_2.save()
        tournament_2.players.set(players)
        Match(player_1_id=players[2].id,
              player_2_id=players[1].id,
              tournament_id=tournament_2.id,
              player_1_score=2,
              player_2_score=1,
              draws=0,
              round=1).save()
 def setUp(self):
     self.players = [
         Player(
             first_name="Fryderyk",
             last_name="Chopin",
             email="*****@*****.**",
         ),
         Player(
             first_name="Immanuel",
             last_name="Kant",
             email="*****@*****.**",
         )
     ]
     self.tournament = Tournament(name='tournament_test_1',
                                  date_beginning='2019-01-02 10:00:00',
                                  date_ending='2019-01-03 10:00:00')
     self.tournament.save()
     for player in self.players:
         player.save()
def tournament_list(request):
    if request.method == 'GET':
        tournaments = Tournament.objects.all()
        serializer = TournamentListSerializer(tournaments, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = TournamentCreateSerializer(data=data)
        if not serializer.is_valid():
            return JsonResponse(serializer.errors, status=400)
        else:
            new_tournament = Tournament(name=data['name'],
                                        date_beginning=data['date_beginning'],
                                        date_ending=data['date_ending'])
            new_tournament.save()

            serializer_return = TournamentListSerializer(new_tournament)
            return JsonResponse(serializer_return.data, safe=False, status=201)
def gen_tournament(
    name='test tournament 1',
    date_beginning='2018-12-17 10:00:00',
    date_ending='2018-12-17 10:00:00',
    player_dicts=None
    # [
    # {"first_name": "John"},
    # {"last_name": "Fryc"},
    # {"email": "*****@*****.**"},
    # {"password": "******"}
    # ]
):
    tournament = Tournament(
        name=name,
        date_beginning=date_beginning,
        date_ending=date_ending,
    )
    if player_dicts:
        tournament.save()
        players = [gen_player(**player_dict) for player_dict in player_dicts]
        tournament.players.set(players)
    tournament.save()

    return tournament
class TestAddPlayersToTournament(TestCase):
    def setUp(self):
        self.players = [
            Player(
                first_name="Fryderyk",
                last_name="Chopin",
                email="*****@*****.**",
            ),
            Player(
                first_name="Immanuel",
                last_name="Kant",
                email="*****@*****.**",
            )
        ]
        self.tournament = Tournament(name='tournament_test_1',
                                     date_beginning='2019-01-02 10:00:00',
                                     date_ending='2019-01-03 10:00:00')
        self.tournament.save()
        for player in self.players:
            player.save()

    def test_add_player_to_tournament_ok(self):
        payload = {"player_ids": [self.players[0].id]}
        response = client.post(
            f'{BASE_URL}/events/tournaments/{self.tournament.id}/add_players/',
            data=json.dumps(payload),
            content_type='application/json')
        errors = response.json()['errors']
        data = response.json()['data']

        self.assertEqual(len(errors['player_id_missing']), 0)
        self.assertEqual(len(data['players']), 1)
        self.assertEqual(data['players'][0]["id"], payload["player_ids"][0])
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_add_player_to_tournament_wrong_player_id(self):
        payload = {"player_ids": [999]}
        response = client.post(
            f'{BASE_URL}/events/tournaments/{self.tournament.id}/add_players/',
            data=json.dumps(payload),
            content_type='application/json')
        errors = response.json()['errors']
        data = response.json()['data']

        self.assertEqual(len(errors['player_id_missing']), 1)
        self.assertEqual(len(data['players']), 0)
        self.assertEqual(errors['player_id_missing'][0],
                         payload["player_ids"][0])
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_add_player_to_tournament_wrong_tournament_id(self):
        payload = {"player_ids": [[self.players[0].id]]}
        response = client.post(
            f'{BASE_URL}/events/tournaments/999/add_players/',
            data=json.dumps(payload),
            content_type='application/json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_add_two_players_to_tournament_ok(self):
        payload = {"player_ids": [self.players[0].id, self.players[1].id]}
        response = client.post(
            f'{BASE_URL}/events/tournaments/{self.tournament.id}/add_players/',
            data=json.dumps(payload),
            content_type='application/json')
        errors = response.json()['errors']
        data = response.json()['data']

        self.assertEqual(len(errors['player_id_missing']), 0)
        self.assertEqual(len(data['players']), 2)
        self.assertEqual(data['players'][0]["id"], payload["player_ids"][0])
        self.assertEqual(data['players'][1]["id"], payload["player_ids"][1])
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_add_two_the_same_players_to_tournament_ok(self):
        payload = {"player_ids": [self.players[0].id, self.players[0].id]}
        response = client.post(
            f'{BASE_URL}/events/tournaments/{self.tournament.id}/add_players/',
            data=json.dumps(payload),
            content_type='application/json')
        errors = response.json()['errors']
        data = response.json()['data']

        self.assertEqual(len(errors['player_id_missing']), 0)
        self.assertEqual(len(data['players']), 1)
        self.assertEqual(data['players'][0]["id"], payload["player_ids"][0])
        self.assertEqual(response.status_code, status.HTTP_200_OK)
示例#9
0
 def score(self, Tournament):
     return Tournament.score_by_player_id(self.id)