Skip to content

Commit 2995b62

Browse files
author
Piotr Gulbinowicz
committed
add tests for /stats
1 parent 43e7ee9 commit 2995b62

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

api/tests/universal/test_stats.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import os
2+
3+
import pytest
4+
from fastapi import status
5+
from fastapi.testclient import TestClient
6+
from httpx import BasicAuth
7+
from universal.helpers import create_flush, create_user
8+
9+
from api.main import app
10+
11+
if os.environ["MONGO_URL"] == "mock":
12+
pytest.skip("Skipping stats tests on mock db", allow_module_level=True)
13+
client = TestClient(app)
14+
15+
16+
def test_getting_flush_stats():
17+
flushes = [
18+
{
19+
"time_start": "2021-01-01T00:00:00",
20+
"time_end": "2021-01-01T01:00:00",
21+
"rating": 1,
22+
"note": "test",
23+
"phone_used": False,
24+
},
25+
{
26+
"time_start": "2021-01-01T01:00:00",
27+
"time_end": "2021-01-01T02:00:00",
28+
"rating": 2,
29+
"note": "test",
30+
"phone_used": True,
31+
},
32+
{
33+
"time_start": "2021-01-01T02:00:00",
34+
"time_end": "2021-01-01T03:00:00",
35+
"rating": 3,
36+
"note": "test",
37+
"phone_used": True,
38+
},
39+
{
40+
"time_start": "2021-01-01T03:00:00",
41+
"time_end": "2021-01-01T04:00:00",
42+
"rating": 4,
43+
"note": "test",
44+
"phone_used": True,
45+
},
46+
{
47+
"time_start": "2021-01-01T04:00:00",
48+
"time_end": "2021-01-01T05:00:00",
49+
"rating": 5,
50+
"note": "test",
51+
"phone_used": True,
52+
},
53+
{
54+
"time_start": "2021-01-01T05:00:00",
55+
"time_end": "2021-01-01T06:00:00",
56+
"rating": 6,
57+
"note": "test",
58+
"phone_used": True,
59+
},
60+
]
61+
username, password = "teststats", "teststats"
62+
create_user(client, username, password)
63+
for f in flushes:
64+
create_flush(client, username, password, f)
65+
66+
response = client.get(
67+
"/stats", auth=BasicAuth(username=username, password=password)
68+
)
69+
assert response.status_code == status.HTTP_200_OK
70+
js = response.json()
71+
assert js["flushCount"] == 6
72+
assert js["totalTime"] == 360
73+
assert js["meanTime"] == 60
74+
assert js["meanRating"] == 3
75+
assert js["phoneUsedCount"] == 5
76+
assert js["percentPhoneUsed"] == 83
77+
78+
79+
def test_getting_flush_stats_noflushes():
80+
username, password = "teststatsempty", "teststatsempty"
81+
create_user(client, username, password)
82+
83+
response = client.get(
84+
"/stats", auth=BasicAuth(username=username, password=password)
85+
)
86+
assert response.status_code == status.HTTP_200_OK
87+
js = response.json()
88+
assert js["flushCount"] == 0
89+
assert js["totalTime"] == 0
90+
assert js["meanTime"] == 0
91+
assert js["meanRating"] == 0
92+
assert js["phoneUsedCount"] == 0
93+
assert js["percentPhoneUsed"] == 0

0 commit comments

Comments
 (0)