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 unit test for pitches #14

Merged
merged 1 commit into from
Apr 20, 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
22 changes: 16 additions & 6 deletions api/src/player/crud/pitches_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def get_player_pitches(
.all()
)

if len(pitches) == 0:
if (
len(pitches) == 0
): # pragma: no cover (This will call MLB api, so for now I'm not testing this)
season_start = statsapi.latest_season()["regularSeasonStartDate"]
season_start_date = datetime.strptime(season_start, "%Y-%m-%d").date()
today_date = datetime.now().date()
Expand Down Expand Up @@ -82,15 +84,15 @@ def update_pitches(
db_pitches.mlb_id = pitches_in.mlb_id
db_pitches.season = pitches_in.season
db_pitches.team_id = pitches_in.team_id
db.db_pitches.stats = pitches_in.pitches
db_pitches.stats = pitches_in.pitches

db.add(db_pitches)
db.commit()
db.refresh(db_pitches)
return db_pitches # type: ignore[no-any-return]


def create_pitch_type(
def create_pitch_type( # pragma: no cover (This is only called when we call get_player_pitches)
db: Session, pitch_type: PitchTypeCreate, pitches_id: int
) -> PitchType:
db_pitch_type = PitchType(
Expand All @@ -103,11 +105,15 @@ def create_pitch_type(
return db_pitch_type


def get_pitch_type(db: Session, id: int) -> Optional[PitchType]:
def get_pitch_type(
db: Session, id: int
) -> Optional[
PitchType
]: # pragma: no cover (This is only called when we call get_player_pitches)
return db.query(PitchType).filter(PitchType.id == id).first() # type: ignore[no-any-return]


def update_pitch_type(
def update_pitch_type( # pragma: no cover (This is only called when we call get_player_pitches)
db: Session, id: int, pitch_type_in: PitchesUpdate
) -> Optional[PitchType]:
db_pitch_type = db.query(PitchType).filter(PitchType.id == id).first()
Expand All @@ -121,7 +127,11 @@ def update_pitch_type(
return db_pitch_type # type: ignore[no-any-return]


def remove_pitch_type(db: Session, id: int) -> Optional[PitchType]:
def remove_pitch_type(
db: Session, id: int
) -> Optional[
PitchType
]: # pragma: no cover (This is only called when we call get_player_pitches)
db_pitch_type = db.query(PitchType).filter(PitchType.id == id).first()
db.delete(db_pitch_type)
db.commit()
Expand Down
8 changes: 6 additions & 2 deletions api/src/player/routers/pitches_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ async def create_pitch_type(
db: Session = Depends(get_db),
id: int,
pitch_type: pitches_schemas.PitchTypeCreate,
) -> PitchType:
) -> (
PitchType
): # pragma: no cover (This is only called when we call read_all_pitches_by_mlb_id)
"""
Create a pitch type.
"""
Expand All @@ -115,7 +117,9 @@ async def read_pitch_types(
*,
db: Session = Depends(get_db),
id: int,
) -> Pitches:
) -> (
Pitches
): # pragma: no cover (This is only called when we call read_all_pitches_by_mlb_id)
"""
Get pitch types by ID.
"""
Expand Down
4 changes: 2 additions & 2 deletions api/src/player/schemas/pitches_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class PitchType(PitchTypeBase):
class PitchesBase(BaseModel): # type:ignore[misc]
season: int
team_id: int
mlb_id: int
pitches: list[PitchType] = []


class PitchesCreate(PitchesBase):
Expand All @@ -38,5 +40,3 @@ class Pitches(PitchesBase):
model_config = ConfigDict(from_attributes=True)

id: int
mlb_id: int
pitches: list[PitchType] = []
2 changes: 2 additions & 0 deletions api/src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def get_test_user(test_client: TestClient) -> User:
@pytest.fixture # type: ignore[misc]
def get_test_player(get_test_user_token: Token, test_client: TestClient) -> Player:
response = test_client.get("/api/v1/players/mlb/" + str(test_player["mlb_id"]))
import logging

logging.getLogger(__name__).info(response)
if response.status_code == 404:
response = test_client.post(
"/api/v1/players/",
Expand Down
Empty file removed api/src/tests/routers/__init__.py
Empty file.
150 changes: 150 additions & 0 deletions api/src/tests/test_pitches_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from auth_token.token_model import Token
from fastapi import status
from fastapi.testclient import TestClient
from player.models.player import Player


def test_create_pitches_endpoint(
get_test_player: Player, get_test_user_token: Token, test_client: TestClient
) -> None:
test_pitches = {
"mlb_id": get_test_player.mlb_id,
"team_id": get_test_player.current_team_id,
"season": 1830,
}

response = test_client.post(
"/api/v1/player/pitches/" + str(get_test_player.mlb_id),
json=test_pitches,
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)
response_json = response.json()
assert response.status_code == status.HTTP_200_OK

assert response_json["mlb_id"] == test_pitches["mlb_id"]
assert response_json["team_id"] == test_pitches["team_id"]
assert response_json["season"] == test_pitches["season"]
assert response_json["pitches"] == []


def test_read_all_pitches_by_mlb_id_endpoint(
get_test_player: Player, test_client: TestClient
) -> None:
test_pitches = {
"mlb_id": get_test_player.mlb_id,
"team_id": get_test_player.current_team_id,
"season": 1830,
}

response = test_client.get(
"/api/v1/player/pitches/all/" + str(get_test_player.mlb_id),
)

response_json = response.json()
assert response.status_code == status.HTTP_200_OK

assert response_json[0]["mlb_id"] == test_pitches["mlb_id"]
assert response_json[0]["team_id"] == test_pitches["team_id"]
assert response_json[0]["season"] == test_pitches["season"]
assert response_json[0]["pitches"] == []


def test_read_pitches_by_id_endpoint(
get_test_player: Player, test_client: TestClient
) -> None:
test_pitches = {
"mlb_id": get_test_player.mlb_id,
"team_id": get_test_player.current_team_id,
"season": 1830,
}

response = test_client.get(
"/api/v1/player/pitches/1",
)

response_json = response.json()
assert response.status_code == status.HTTP_200_OK

assert response_json["mlb_id"] == test_pitches["mlb_id"]
assert response_json["team_id"] == test_pitches["team_id"]
assert response_json["season"] == test_pitches["season"]
assert response_json["pitches"] == []


def test_read_pitches_by_id_endpoint_with_dne_id(test_client: TestClient) -> None:
response = test_client.get(
"/api/v1/player/pitches/-1",
)

response_json = response.json()
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response_json == {"detail": "Pitches not found."}


def test_update_pitches_by_id(
get_test_user_token: Token, get_test_player: Player, test_client: TestClient
) -> None:
update_test_pitches = {
"mlb_id": get_test_player.mlb_id,
"team_id": get_test_player.current_team_id,
"season": 1831,
"pitches": [],
}

response = test_client.put(
"/api/v1/player/pitches/1",
json=update_test_pitches,
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

assert response.status_code == status.HTTP_200_OK
response_json = response.json()
assert response_json["mlb_id"] == update_test_pitches["mlb_id"]
assert response_json["team_id"] == update_test_pitches["team_id"]
assert response_json["season"] == update_test_pitches["season"]
assert response_json["pitches"] == []


def test_update_pitches_by_id_with_dne_id(
get_test_user_token: Token, get_test_player: Player, test_client: TestClient
) -> None:
update_test_pitches = {
"mlb_id": get_test_player.mlb_id,
"team_id": get_test_player.current_team_id,
"season": 1831,
"pitches": [],
}

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

response_json = response.json()
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response_json == {"detail": "Pitches not found."}


def test_delete_stats_by_id(
get_test_user_token: Token, test_client: TestClient
) -> None:
response = test_client.delete(
"/api/v1/player/pitches/1",
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

assert response.status_code == status.HTTP_200_OK


def test_delete_stats_by_id_with_dne_id(
get_test_user_token: Token, test_client: TestClient
) -> None:
response = test_client.delete(
"/api/v1/player/pitches/-1",
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)

response_json = response.json()
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response_json == {"detail": "Pitches not found."}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ def test_create_player_endpoint(


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

assert response.status_code == status.HTTP_200_OK
response_json = response.json()
response = test_client.get("/api/v1/players/" + str(response_json["id"]))

assert response.status_code == status.HTTP_200_OK

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"]
Expand Down Expand Up @@ -149,17 +153,22 @@ def test_read_all_players_endpoint_without_position(test_client: TestClient) ->
assert response.status_code == status.HTTP_200_OK

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


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

assert response.status_code == status.HTTP_200_OK
response_json = response.json()

update_test_player = test_player.copy()
update_test_player["full_name"] = "New Test Name"

response = test_client.put(
"/api/v1/players/1",
"/api/v1/players/" + str(response_json["id"]),
json=update_test_player,
headers={"Authorization": f"Bearer {get_test_user_token.access_token}"},
)
Expand Down
Loading