tennis-front / tests /conftest.py
SlimG's picture
fixup: limit model listing to the ones having a version in production
08f481a
import pytest
import responses
from typing import Generator
from responses.matchers import query_param_matcher
from dotenv import load_dotenv
import os
from streamlit.testing.v1 import AppTest
load_dotenv()
API_URL = os.getenv('API_URL')
# -----------------------------------------------------------------
# Fixtures for Streamlit
# -----------------------------------------------------------------
@pytest.fixture()
def page_1() -> Generator[AppTest, None, None]:
"""
Fixture to get the page 1 of the AppTest instance.
"""
script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app", "1_Prédictions.py"))
app = AppTest.from_file(script_path)
app.run()
yield app
# Put here any cleaning operation
@pytest.fixture()
def page_2() -> Generator[AppTest, None, None]:
"""
Fixture to get the page 2 of the AppTest instance.
"""
script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app", "pages", "2_Archives.py"))
app = AppTest.from_file(script_path)
app.run()
yield app
# Put here any cleaning operation
# -----------------------------------------------------------------
# Fixtures for requests calls
# -----------------------------------------------------------------
@pytest.fixture(autouse=True)
def mock_responses():
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(
rsps.GET,
f"{API_URL}/references/courts",
json=[
"Indoor",
"Outdoor"
],
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/references/surfaces",
json=[
"Carpet",
"Clay",
"Grass",
"Hard"
],
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/references/series",
json=[
"ATP250",
"ATP500",
"Grand Slam",
"International",
"International Gold",
"Masters",
"Masters 1000",
"Masters Cup"
],
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/tournament/names",
json=[
{
"name": "BMW Open",
"first_year": 2025,
"last_year": 2025
},
{
"name": "BNP Paribas",
"first_year": 2004,
"last_year": 2008
},
{
"name": "BNP Paribas Masters",
"first_year": 2009,
"last_year": 2024
},
{
"name": "BNP Paribas Open",
"first_year": 2009,
"last_year": 2025
},
{
"name": "Brasil Open",
"first_year": 2004,
"last_year": 2019
},
{
"name": "French Open",
"first_year": 2004,
"last_year": 2024
},
{
"name": "Halle Open",
"first_year": 2019,
"last_year": 2024
},
{
"name": "Madrid Masters",
"first_year": 2004,
"last_year": 2008
},
{
"name": "US Open",
"first_year": 2004,
"last_year": 2024
},
{
"name": "Wimbledon",
"first_year": 2004,
"last_year": 2024
},
],
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/tournament/matches",
match=[query_param_matcher(params={
"name": "BMW Open",
"year": 2025,
})],
json=[
{
"id": 40926,
"date": "2025-04-20",
"comment": "Completed",
"winner_rank": 3,
"winner_points": 7595,
"loser_rank": 15,
"loser_points": 2700,
"tournament_name": "BMW Open",
"tournament_series": "ATP500",
"tournament_surface": "Clay",
"tournament_court": "Outdoor",
"tournament_round": "The Final",
"tournament_location": "Munich",
"fk_winner_id": 666,
"fk_loser_id": 1049,
"odds": [
{
"id": 218029,
"bookmaker": "Avg",
"winner": 1.21,
"loser": 4.53
},
{
"id": 218027,
"bookmaker": "B365",
"winner": 1.2,
"loser": 4.5
},
{
"id": 218030,
"bookmaker": "Max",
"winner": 1.23,
"loser": 5.19
},
{
"id": 218028,
"bookmaker": "PS",
"winner": 1.22,
"loser": 4.94
}
]
},
{
"id": 40924,
"date": "2025-04-19",
"comment": "Completed",
"winner_rank": 15,
"winner_points": 2700,
"loser_rank": 22,
"loser_points": 2075,
"tournament_name": "BMW Open",
"tournament_series": "ATP500",
"tournament_surface": "Clay",
"tournament_court": "Outdoor",
"tournament_round": "Semifinals",
"tournament_location": "Munich",
"fk_winner_id": 1049,
"fk_loser_id": 985,
"odds": [
{
"id": 218021,
"bookmaker": "Avg",
"winner": 3.25,
"loser": 1.35
},
{
"id": 218019,
"bookmaker": "B365",
"winner": 3.2,
"loser": 1.36
},
{
"id": 218022,
"bookmaker": "Max",
"winner": 3.5,
"loser": 1.39
},
{
"id": 218020,
"bookmaker": "PS",
"winner": 3.5,
"loser": 1.35
}
]
},
{
"id": 40925,
"date": "2025-04-19",
"comment": "Completed",
"winner_rank": 3,
"winner_points": 7595,
"loser_rank": 77,
"loser_points": 805,
"tournament_name": "BMW Open",
"tournament_series": "ATP500",
"tournament_surface": "Clay",
"tournament_court": "Outdoor",
"tournament_round": "Semifinals",
"tournament_location": "Munich",
"fk_winner_id": 666,
"fk_loser_id": 1097,
"odds": [
{
"id": 218025,
"bookmaker": "Avg",
"winner": 1.22,
"loser": 4.41
},
{
"id": 218023,
"bookmaker": "B365",
"winner": 1.2,
"loser": 4.5
},
{
"id": 218026,
"bookmaker": "Max",
"winner": 1.25,
"loser": 4.95
},
{
"id": 218024,
"bookmaker": "PS",
"winner": 1.22,
"loser": 4.95
}
]
}
],
)
rsps.add(
rsps.GET,
f"{API_URL}/players",
match=[query_param_matcher({
"ids": ['103', '104']
})],
json={
"103": {
"id": 103,
"name": "Nishikori K.",
"tennis_id": "N552",
"caracteristics": {
"id": 42,
"nationality": "Japan",
"last_name": "Nishikori",
"first_name": "Kei",
"play_hand": "R",
"back_hand": 2,
"height_cm": 178,
"weight_kg": 73,
"date_of_birth": "1989-12-29",
"pro_year": 2007
}
},
"104": {
"id": 104,
"name": "Reynolds B.",
"tennis_id": "R0G6",
"caracteristics": {
"id": 27,
"nationality": "United States",
"last_name": "Reynolds",
"first_name": "Bob",
"play_hand": "U",
"back_hand": 0,
"height_cm": None,
"weight_kg": None,
"date_of_birth": None,
"pro_year": None
}
}
},
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/players",
match=[query_param_matcher({
"ids": ['1097', '1049', '666', '985']
})],
json={
"666": {
"id": 666,
"name": "Zverev A.",
"tennis_id": "Z355",
"caracteristics": {
"id": 505,
"nationality": "Germany",
"last_name": "Zverev",
"first_name": "Alexander",
"play_hand": "R",
"back_hand": 2,
"height_cm": 198,
"weight_kg": 90,
"date_of_birth": "1997-04-20",
"pro_year": 2013
}
},
"985": {
"id": 985,
"name": "Cerundolo F.",
"tennis_id": "C0AU",
"caracteristics": {
"id": 798,
"nationality": "Argentina",
"last_name": "Cerundolo",
"first_name": "Francisco",
"play_hand": "R",
"back_hand": 2,
"height_cm": 185,
"weight_kg": 80,
"date_of_birth": "1998-08-13",
"pro_year": 2018
}
},
"1049": {
"id": 1049,
"name": "Shelton B.",
"tennis_id": "S0S1",
"caracteristics": {
"id": 857,
"nationality": "United States",
"last_name": "Shelton",
"first_name": "Ben",
"play_hand": "L",
"back_hand": 2,
"height_cm": 193,
"weight_kg": 88,
"date_of_birth": "2002-10-09",
"pro_year": 2022
}
},
"1097": {
"id": 1097,
"name": "Marozsan F.",
"tennis_id": "M0CI",
"caracteristics": {
"id": 903,
"nationality": "Hungary",
"last_name": "Marozsan",
"first_name": "Fabian",
"play_hand": "R",
"back_hand": 2,
"height_cm": 193,
"weight_kg": 75,
"date_of_birth": "1999-10-08",
"pro_year": 2017
}
}
},
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/player/103",
json={
"id": 103,
"name": "Nishikori K.",
"tennis_id": "N552",
"caracteristics": {
"id": 42,
"nationality": "Japan",
"last_name": "Nishikori",
"first_name": "Kei",
"play_hand": "R",
"back_hand": 2,
"height_cm": 178,
"weight_kg": 73,
"date_of_birth": "1989-12-29",
"pro_year": 2007
}
},
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/player/104",
json={
"id": 104,
"name": "Reynolds B.",
"tennis_id": "R0G6",
"caracteristics": {
"id": 27,
"nationality": "United States",
"last_name": "Reynolds",
"first_name": "Bob",
"play_hand": "U",
"back_hand": 0,
"height_cm": None,
"weight_kg": None,
"date_of_birth": None,
"pro_year": None
}
},
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/match/40926",
json={
"id": 40926,
"date": "2025-04-20",
"comment": "Completed",
"winner_rank": 3,
"winner_points": 7595,
"loser_rank": 15,
"loser_points": 2700,
"tournament_name": "BMW Open",
"tournament_series": "ATP500",
"tournament_surface": "Clay",
"tournament_court": "Outdoor",
"tournament_round": "The Final",
"tournament_location": "Munich",
"fk_winner_id": 666,
"fk_loser_id": 1049,
"odds": [
{
"id": 218029,
"bookmaker": "Avg",
"winner": 1.21,
"loser": 4.53
},
{
"id": 218027,
"bookmaker": "B365",
"winner": 1.2,
"loser": 4.5
},
{
"id": 218030,
"bookmaker": "Max",
"winner": 1.23,
"loser": 5.19
},
{
"id": 218028,
"bookmaker": "PS",
"winner": 1.22,
"loser": 4.94
}
],
"winner": {
"id": 666,
"name": "Zverev A.",
"tennis_id": "Z355",
"caracteristics": {
"id": 505,
"nationality": "Germany",
"last_name": "Zverev",
"first_name": "Alexander",
"play_hand": "R",
"back_hand": 2,
"height_cm": 198,
"weight_kg": 90,
"date_of_birth": "1997-04-20",
"pro_year": 2013
}
},
"loser": {
"id": 1049,
"name": "Shelton B.",
"tennis_id": "S0S1",
"caracteristics": {
"id": 857,
"nationality": "United States",
"last_name": "Shelton",
"first_name": "Ben",
"play_hand": "L",
"back_hand": 2,
"height_cm": 193,
"weight_kg": 88,
"date_of_birth": "2002-10-09",
"pro_year": 2022
}
}
},
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/list_available_models",
match=[query_param_matcher(params={"aliases": "prod"})],
json=[
{
"name": "LogisticRegression",
"run_id": "55e44730cce245a99da6f023a2afd54c",
"version": "1",
"aliases": ["prod"],
},
{
"name": "MLP",
"run_id": "a0b525d0a8d940f28e8db6b237414e6e",
"version": "1",
"aliases": ["prod"],
}
],
status=200
)
rsps.add(
rsps.GET,
f"{API_URL}/predict",
json={
"result": 1,
"prob": [
0.3512304338942953,
0.6487695661057047
]
},
status=200
)
yield rsps