-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
51 lines (41 loc) · 1.72 KB
/
test_main.py
File metadata and controls
51 lines (41 loc) · 1.72 KB
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
from fastapi.testclient import TestClient
from main import app, get_db_connection
from datetime import datetime, timezone, timedelta
import pytest
client = TestClient(app)
@pytest.fixture(scope="function")
def test_db():
db = get_db_connection()
cursor = db.cursor()
cursor.execute("DELETE FROM events")
db.commit()
yield db
db.close()
def test_process_event(test_db):
response = client.post("/process_event/", json={"userid": "user123", "eventname": "Test Event"})
assert response.status_code == 200
assert response.json() == {"message": "Event processed successfully"}
cursor = test_db.cursor()
cursor.execute("SELECT * FROM events")
event_records = cursor.fetchall()
assert len(event_records) == 1
assert event_records[0][1] == "user123"
assert event_records[0][2] == "Test Event"
def test_get_reports(test_db):
cursor = test_db.cursor()
eventtimestamputc = datetime.now(timezone.utc) - timedelta(seconds=100)
cursor.execute('''
INSERT INTO events (eventtimestamputc, userid, eventname) VALUES (?, ?, ?)
''', (eventtimestamputc, 'user123', 'Test Event'))
test_db.commit()
response = client.post("/get_reports/", params={"lastseconds": 200, "userid": "user123"})
assert response.status_code == 200
response_json = response.json()
assert "events" in response_json
assert len(response_json["events"]) == 1
assert response_json["events"][0]["userid"] == "user123"
assert response_json["events"][0]["eventname"] == "Test Event"
response = client.post("/get_reports/", params={"lastseconds": 50, "userid": "user123"})
assert response.status_code == 200
response_json = response.json()
assert len(response_json["events"]) == 0