Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the remaining player endpoints test for 100% coverage #11

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
pip install -r setup/requirements.txt -r setup/development/requirements.txt
- name: Test with pytest
run: |
pytest --cov=api api/src
pytest --cov-report term-missing --cov=api api/src
102 changes: 95 additions & 7 deletions api/src/tests/routers/test_player_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random

from auth_token.token_model import Token
from fastapi import status
from fastapi.testclient import TestClient

random_mlb_id = random.randint(100000, 999999)
Expand All @@ -10,7 +11,7 @@
"full_name": "Test Player",
"primary_number": 5,
"current_team_id": 0,
"primary_position_code": "P",
"primary_position_code": "1",
}


Expand All @@ -23,7 +24,7 @@ def test_create_player_endpoint(
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

response_json = response.json()
assert response_json["mlb_id"] == test_player["mlb_id"]
Expand All @@ -36,10 +37,33 @@ def test_create_player_endpoint(
assert response_json["stats"] == []


def test_get_player_by_id_endpoint(test_client: TestClient) -> None:
response = test_client.get("/api/v1/players/1")

assert response.status_code == status.HTTP_200_OK
response_json = response.json()
assert response_json["mlb_id"] == test_player["mlb_id"]
assert response_json["full_name"] == test_player["full_name"]
assert response_json["primary_number"] == test_player["primary_number"]
assert response_json["current_team_id"] == test_player["current_team_id"]
assert (
response_json["primary_position_code"] == test_player["primary_position_code"]
)
assert response_json["stats"] == []


def test_get_player_by_id_endpoint_with_id_doesnt_exist(
test_client: TestClient,
) -> None:
response = test_client.get("/api/v1/players/-1")

assert response.status_code == status.HTTP_404_NOT_FOUND


def test_get_player_by_mlb_id_endpoint(test_client: TestClient) -> None:
response = test_client.get("/api/v1/players/mlb/" + str(random_mlb_id))

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

response_json = response.json()
assert response_json["mlb_id"] == test_player["mlb_id"]
Expand All @@ -52,10 +76,18 @@ def test_get_player_by_mlb_id_endpoint(test_client: TestClient) -> None:
assert response_json["stats"] == []


def test_get_player_by_mlb_id_endpoint_with_mlb_id_doesnt_exist(
test_client: TestClient,
) -> None:
response = test_client.get("/api/v1/players/mlb/-1")

assert response.status_code == status.HTTP_404_NOT_FOUND


def test_get_player_by_full_name_endpoint(test_client: TestClient) -> None:
response = test_client.get("/api/v1/players/name/" + str(test_player["full_name"]))

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

response_json = response.json()
assert response_json["mlb_id"] == test_player["mlb_id"]
Expand All @@ -68,12 +100,20 @@ def test_get_player_by_full_name_endpoint(test_client: TestClient) -> None:
assert response_json["stats"] == []


def test_get_player_by_full_name_endpoint_with_full_name_doesnt_exist(
test_client: TestClient,
) -> None:
response = test_client.get("/api/v1/players/name/Doesnt Exist")

assert response.status_code == status.HTTP_404_NOT_FOUND


def test_get_player_by_primary_number_endpoint(test_client: TestClient) -> None:
response = test_client.get(
"/api/v1/players/number/" + str(test_player["primary_number"])
)

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

response_json = response.json()
assert response_json["mlb_id"] == test_player["mlb_id"]
Expand All @@ -86,6 +126,23 @@ def test_get_player_by_primary_number_endpoint(test_client: TestClient) -> None:
assert response_json["stats"] == []


def test_get_player_by_primary_number_endpoint_with_primary_number_doesnt_exist(
test_client: TestClient,
) -> None:
response = test_client.get("/api/v1/players/number/-1")

assert response.status_code == status.HTTP_404_NOT_FOUND


def test_read_all_players_endpoint(test_client: TestClient) -> None:
response = test_client.get("/api/v1/players/?position=1&skip=0&limit=100")

assert response.status_code == status.HTTP_200_OK

response_json = response.json()
assert len(response_json) == 1


def test_update_player_endpoint(
get_test_user_token: Token, test_client: TestClient
) -> None:
Expand All @@ -98,7 +155,7 @@ def test_update_player_endpoint(
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

response_json = response.json()
assert response_json["mlb_id"] == update_test_player["mlb_id"]
Expand All @@ -112,6 +169,22 @@ def test_update_player_endpoint(
assert response_json["stats"] == []


def test_update_player_endpoint_with_id_doesnt_exist(
get_test_user_token: Token,
test_client: TestClient,
) -> None:
update_test_player = test_player.copy()
update_test_player["full_name"] = "New Test Name"

response = test_client.put(
"/api/v1/players/-1",
json=update_test_player,
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

assert response.status_code == status.HTTP_404_NOT_FOUND


def test_delete_player_endpoint(
get_test_user_token: Token, test_client: TestClient
) -> None:
Expand All @@ -120,4 +193,19 @@ def test_delete_player_endpoint(
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK


def test_delete_player_endpoint_with_id_doesnt_exist(
get_test_user_token: Token,
test_client: TestClient,
) -> None:
update_test_player = test_player.copy()
update_test_player["full_name"] = "New Test Name"

response = test_client.delete(
"/api/v1/players/1",
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

assert response.status_code == status.HTTP_404_NOT_FOUND
Loading