mirror of
https://github.com/Sosokker/HomieCare.git
synced 2025-12-18 17:54:04 +01:00
Add unittest to backend
This commit is contained in:
parent
33fd746d77
commit
5bfe9d10a4
@ -252,4 +252,11 @@ async def websocket_endpoint(camera_id: int, websocket: WebSocket):
|
||||
print(f"Error sending frame: {e}")
|
||||
finally:
|
||||
cap.release()
|
||||
await websocket.close(code=1000)
|
||||
await websocket.close(code=1000)
|
||||
|
||||
@router.get("/action/test", response_model=bool)
|
||||
def test_notification():
|
||||
"""Test notification by sending a POST request to LINE Notify API"""
|
||||
if action_model.IS_FALL_DOWN:
|
||||
return True
|
||||
return False
|
||||
10
StreamServer/src/run_test.py
Normal file
10
StreamServer/src/run_test.py
Normal file
@ -0,0 +1,10 @@
|
||||
import pytest
|
||||
import os
|
||||
|
||||
def run_tests():
|
||||
current_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
tests_dir = os.path.join(current_dir, 'tests')
|
||||
pytest.main([tests_dir])
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_tests()
|
||||
0
StreamServer/src/tests/__init__.py
Normal file
0
StreamServer/src/tests/__init__.py
Normal file
25
StreamServer/src/tests/test_camera.py
Normal file
25
StreamServer/src/tests/test_camera.py
Normal file
@ -0,0 +1,25 @@
|
||||
# import pytest
|
||||
# from fastapi.testclient import TestClient
|
||||
# from main import app
|
||||
|
||||
# @pytest.fixture
|
||||
# def client():
|
||||
# with TestClient(app) as client:
|
||||
# yield client
|
||||
|
||||
# def test_list_cameras(client):
|
||||
# response = client.get("/camera/list")
|
||||
# assert response.status_code == 200
|
||||
# assert isinstance(response.json(), list)
|
||||
|
||||
# @pytest.mark.parametrize("camera_id", [1, 2, 3])
|
||||
# def test_stream_video(client, camera_id):
|
||||
# response = client.get(f"/stream/{camera_id}")
|
||||
# assert response.status_code == 200
|
||||
# assert "multipart/x-mixed-replace; boundary=frame" in response.headers["content-type"]
|
||||
|
||||
# @pytest.mark.parametrize("camera_id", [1, 2, 3])
|
||||
# def test_stream_action_video(client, camera_id):
|
||||
# response = client.get(f"/stream/action/{camera_id}")
|
||||
# assert response.status_code == 200
|
||||
# assert "multipart/x-mixed-replace; boundary=frame" in response.headers["content-type"]
|
||||
18
StreamServer/src/tests/test_fall.py
Normal file
18
StreamServer/src/tests/test_fall.py
Normal file
@ -0,0 +1,18 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from models import ActionData
|
||||
from analytic.action.action_model import ActionModel
|
||||
from database import SessionLocal
|
||||
|
||||
client = TestClient(app)
|
||||
action_model = ActionModel()
|
||||
|
||||
|
||||
def test_fall_detection_and_notification():
|
||||
action_model.IS_FALL_DOWN = True
|
||||
response = client.get("/camera/action/test")
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
41
StreamServer/src/tests/test_indoor.py
Normal file
41
StreamServer/src/tests/test_indoor.py
Normal file
@ -0,0 +1,41 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from routers.prediction import _fetch_data_from_api
|
||||
from database import SessionLocal
|
||||
from main import app
|
||||
from datetime import datetime
|
||||
from models import PredictionData
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_weather_data():
|
||||
# Sample weather data for testing
|
||||
return {
|
||||
"list": [
|
||||
{
|
||||
"dt": datetime.now().timestamp(),
|
||||
"main": {
|
||||
"temp": 25,
|
||||
"feels_like": 26,
|
||||
"pressure": 1010,
|
||||
"humidity": 50
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def test_get_tomorrow_indoor_temp():
|
||||
db_session = SessionLocal()
|
||||
|
||||
response = client.get("/weather/indoor/predict/")
|
||||
assert response.status_code == 200
|
||||
assert isinstance(response.json(), list)
|
||||
for item in response.json():
|
||||
assert "timestamp" in item
|
||||
assert "indoor_temp" in item
|
||||
assert "outdoor_temp" in item
|
||||
|
||||
# Clean up dependency overrides
|
||||
app.dependency_overrides.clear()
|
||||
25
StreamServer/src/tests/test_recommend.py
Normal file
25
StreamServer/src/tests/test_recommend.py
Normal file
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from scheme import HealthData
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
@pytest.fixture
|
||||
def sample_health_data():
|
||||
return HealthData(
|
||||
indoor_temp=25,
|
||||
outdoor_temp=30,
|
||||
outdoor_pm25=40,
|
||||
outdoor_pm10=60,
|
||||
outdoor_humidity=30
|
||||
)
|
||||
|
||||
def test_get_health_recommendation(sample_health_data):
|
||||
response = client.post("/recommend/recommendation/", json=sample_health_data.model_dump())
|
||||
assert response.status_code == 200
|
||||
assert isinstance(response.json(), list)
|
||||
for item in response.json():
|
||||
assert "timestamp" in item
|
||||
assert "recommendation" in item
|
||||
assert "status" in item
|
||||
Loading…
Reference in New Issue
Block a user