-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
78 lines (63 loc) · 2.02 KB
/
tests.py
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
import itertools
import pytest
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
@pytest.fixture(autouse=True)
def reset_state():
app.state.matches.clear()
app.state.id_generator = itertools.count(1)
@pytest.fixture
def create_test_matches():
match_data_1 = {
"home_team": "Волки",
"away_team": "Быки",
"home_score": 2,
"away_score": 5,
"match_date": "2024-10-19",
"place": "Лесная опушка",
"duration": 92,
"yellow_cards": 3,
"red_cards": 0,
}
match_data_2 = {
"home_team": "Кошки",
"away_team": "Собаки",
"home_score": 1,
"away_score": 1,
"match_date": "2024-10-20",
"place": "Коридор",
"duration": 93,
"yellow_cards": 2,
"red_cards": 1,
}
client.post("/matches/", json=match_data_1)
client.post("/matches/", json=match_data_2)
def test_get_all_matches(create_test_matches):
response = client.get("/matches/")
assert response.status_code == 200
data = response.json()
assert len(data) == 2
assert data[0]["home_team"] == "Волки"
assert data[1]["home_team"] == "Кошки"
def test_get_match_by_id(create_test_matches):
response = client.get("/matches/1")
assert response.status_code == 200
data = response.json()
assert data["id"] == 1
assert data["home_team"] == "Волки"
assert data["away_team"] == "Быки"
def test_stats(create_test_matches):
response = client.get("/matches/stats/?field=home_score")
assert response.status_code == 200
data = response.json()
assert data["average"] == 1.5
assert data["max"] == 2
assert data["min"] == 1
def test_delete_match(create_test_matches):
response = client.delete("/matches/1")
assert response.status_code == 200
data = response.json()
assert data["detail"] == "Match deleted"
response = client.get("/matches/1")
assert response.status_code == 404