File size: 2,854 Bytes
788fb65
4b09086
788fb65
 
 
 
 
 
 
 
4b09086
788fb65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b09086
788fb65
 
 
 
 
4b09086
 
 
 
 
 
 
 
 
3248a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from src.entity.match import Match
from starlette.status import HTTP_200_OK, HTTP_422_UNPROCESSABLE_ENTITY

def test_insert_match_success(client, db_session, raw_match):
    # Count the number of matches in the database before insertion
    match_count_before = db_session.query(Match).count()

    # Insert a valid match
    response = client.post("/match/insert", json=raw_match)
    
    assert response.status_code == HTTP_200_OK
    response_json = response.json()
    assert response_json.get("status") == "ok", "Status should be 'ok'"
    
    match_id = response_json.get("match_id")
    assert match_id is not None, "Match ID should not be None"

    # Count the number of matches in the database after insertion
    match_count_after = db_session.query(Match).count()
    assert match_count_after == match_count_before + 1

    # Check the match details
    match_in_db = db_session.query(Match).filter(Match.id == match_id).first()
    assert match_in_db is not None
    assert match_in_db.winner is not None
    assert match_in_db.loser is not None
    assert match_in_db.winner.name == raw_match.get("Winner")
    assert match_in_db.loser.name == raw_match.get("Loser")

    # Check the response
    assert response.status_code == HTTP_200_OK
    assert response.json() == {
        "status": "ok",
        "match_id": match_in_db.id,
    }
    
# Test for inserting an already existing match
def test_insert_match_integrity_error(client, db_session, wimbledon_final_raw):
    """
    Test inserting an already existing match into the database
    """
    # Attempt to insert the same match again
    response = client.post("/match/insert", json=wimbledon_final_raw)

    assert response.status_code == HTTP_422_UNPROCESSABLE_ENTITY, f"Status code should be {HTTP_422_UNPROCESSABLE_ENTITY} for duplicate match"

def test_list_surfaces(client, with_materialized_views):
    """
    Test the list of surfaces
    """
    response = client.get("/references/surfaces")
    
    assert response.status_code == HTTP_200_OK
    assert set(response.json()) == set([
        "Carpet",
        "Clay",
        "Grass",
        "Hard",
    ])

def test_list_courts(client, with_materialized_views):
    """
    Test the list of courts
    """
    response = client.get("/references/courts")
    
    assert response.status_code == HTTP_200_OK
    assert set(response.json()) == set([
        "Indoor",
        "Outdoor",
    ])

def test_list_series(client, with_materialized_views):
    """
    Test the list of surfaces
    """
    response = client.get("/references/series")

    assert response.status_code == HTTP_200_OK
    assert set(response.json()) == set([
        "ATP250",
        "ATP500",
        "Grand Slam",
        "International",
        "International Gold",
        "Masters",
        "Masters 1000",
        "Masters Cup"
    ])