hatethisworld commited on
Commit
484d03b
·
1 Parent(s): 3c0a5ad

add api test

Browse files
test_focus_guard.db ADDED
Binary file (20.5 kB). View file
 
tests/test_api_sessions.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ from fastapi.testclient import TestClient
5
+
6
+ PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
7
+ if PROJECT_ROOT not in sys.path:
8
+ sys.path.insert(0, PROJECT_ROOT)
9
+
10
+ import main # noqa: E402
11
+
12
+
13
+ def _make_test_client():
14
+ # use test db
15
+ main.db_path = os.path.join(PROJECT_ROOT, "test_focus_guard.db")
16
+ # clean old file
17
+ if os.path.exists(main.db_path):
18
+ os.remove(main.db_path)
19
+ return TestClient(main.app)
20
+
21
+
22
+ def test_start_session_returns_id():
23
+ client = _make_test_client()
24
+ with client:
25
+ resp = client.post("/api/sessions/start")
26
+ assert resp.status_code == 200
27
+ data = resp.json()
28
+ assert "session_id" in data
29
+ assert isinstance(data["session_id"], int)
30
+ assert data["session_id"] > 0
31
+
32
+
33
+ def test_end_session_after_start():
34
+ client = _make_test_client()
35
+ with client:
36
+ # create session
37
+ r_start = client.post("/api/sessions/start")
38
+ session_id = r_start.json()["session_id"]
39
+
40
+ # end session
41
+ r_end = client.post("/api/sessions/end", json={"session_id": session_id})
42
+ assert r_end.status_code == 200
43
+ summary = r_end.json()
44
+ assert summary["session_id"] == session_id
45
+ assert "duration_seconds" in summary
46
+ assert "focus_score" in summary
47
+
48
+
49
+ def test_get_sessions_list():
50
+ client = _make_test_client()
51
+ with client:
52
+ r_start = client.post("/api/sessions/start")
53
+ session_id = r_start.json()["session_id"]
54
+ client.post("/api/sessions/end", json={"session_id": session_id})
55
+
56
+ r_list = client.get("/api/sessions", params={"filter": "all", "limit": 10, "offset": 0})
57
+ assert r_list.status_code == 200
58
+ data = r_list.json()
59
+ assert isinstance(data, list)
60
+ if data:
61
+ item = data[0]
62
+ assert "id" in item
63
+ assert "start_time" in item
64
+ assert "focus_score" in item
65
+
tests/test_api_settings.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ from fastapi.testclient import TestClient
5
+
6
+ PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
7
+ if PROJECT_ROOT not in sys.path:
8
+ sys.path.insert(0, PROJECT_ROOT)
9
+
10
+ import main # noqa: E402
11
+
12
+
13
+ def _make_test_client():
14
+ # use test db
15
+ main.db_path = os.path.join(PROJECT_ROOT, "test_focus_guard.db")
16
+ if os.path.exists(main.db_path):
17
+ os.remove(main.db_path)
18
+ return TestClient(main.app)
19
+
20
+
21
+ def test_get_settings_default_fields():
22
+ client = _make_test_client()
23
+ with client:
24
+ resp = client.get("/api/settings")
25
+ assert resp.status_code == 200
26
+ data = resp.json()
27
+ assert "sensitivity" in data
28
+ assert "notification_enabled" in data
29
+ assert "notification_threshold" in data
30
+ assert "frame_rate" in data
31
+ assert "model_name" in data
32
+
33
+
34
+ def test_update_settings_clamped_ranges():
35
+ client = _make_test_client()
36
+ with client:
37
+ # get setting
38
+ r0 = client.get("/api/settings")
39
+ assert r0.status_code == 200
40
+
41
+ # set unlogic params
42
+ payload = {
43
+ "sensitivity": 100,
44
+ "notification_enabled": False,
45
+ "notification_threshold": 1,
46
+ "frame_rate": 1000,
47
+ }
48
+ r_put = client.put("/api/settings", json=payload)
49
+ assert r_put.status_code == 200
50
+ body = r_put.json()
51
+ assert body["status"] == "success"
52
+ assert body["updated"] is True
53
+
54
+ r1 = client.get("/api/settings")
55
+ data = r1.json()
56
+ assert 1 <= data["sensitivity"] <= 10
57
+ assert bool(data["notification_enabled"]) is False
58
+ assert 5 <= data["notification_threshold"] <= 300
59
+ assert 5 <= data["frame_rate"] <= 60
60
+