from typing import List

import pytest
from django.test.client import Client
from django.urls import reverse

from core.models import Game, GamePlayer, Round
from core.tests.factories import GameFactory, GamePlayerFactory, RoundFactory


@pytest.fixture
def one_round() -> Round:
    return RoundFactory(is_current=True)


@pytest.fixture
def two_games(one_round) -> List[Game]:
    return [
        GameFactory(round=one_round),
        GameFactory(round=one_round),
    ]


@pytest.fixture
def games_with_scores(two_games) -> List[GamePlayer]:
    return [GamePlayerFactory(game=game) for game in two_games]


def test_players_list(client: Client, games_with_scores: List[GamePlayer]):
    url_path = reverse("players-list")
    response = client.get(url_path)
    assert response.status_code == 200
    response_table_headers = response.context["table_header"]
    assert len(response_table_headers) == len({"player", "sum_of_victory_points", "1st_round"})
    response_table_rows = response.context["table_rows"]
    assert len(response_table_rows) == len({"player1", "player2", "player3", "player4"})
    for row in response_table_rows:
        assert row[1] == row[2]


def test_games_list(client: Client, two_games: List[Game]):
    url_path = reverse("games-list")
    response = client.get(url_path)
    assert response.status_code == 200
    response_objects = response.context["object_list"]
    assert len(response_objects) == 2

from typing import List

import pytest
from django.test.client import Client
from django.urls import reverse

from core.models import Game, GamePlayer, Round
from core.tests.factories import GameFactory, GamePlayerFactory, RoundFactory


@pytest.fixture
def one_round() -> Round:
    return RoundFactory(is_current=True)


@pytest.fixture
def two_games(one_round) -> List[Game]:
    return [
        GameFactory(round=one_round),
        GameFactory(round=one_round),
    ]


@pytest.fixture
def games_with_scores(two_games) -> List[GamePlayer]:
    return [GamePlayerFactory(game=game) for game in two_games]


def test_players_list(client: Client, games_with_scores: List[GamePlayer]):
    url_path = reverse("players-list")
    response = client.get(url_path)
    assert response.status_code == 200
    response_table_headers = response.context["table_header"]
    assert len(response_table_headers) == len({"player", "sum_of_victory_points", "1st_round"})
    response_table_rows = response.context["table_rows"]
    assert len(response_table_rows) == len({"player1", "player2", "player3", "player4"})
    for row in response_table_rows:
        assert row[1] == row[2]


def test_games_list(client: Client, two_games: List[Game]):
    url_path = reverse("games-list")
    response = client.get(url_path)
    assert response.status_code == 200
    response_objects = response.context["object_list"]
    assert len(response_objects) == 2
