295 lines
9.3 KiB
Python
295 lines
9.3 KiB
Python
import pytest
|
|
import json
|
|
import sys
|
|
import os
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
# 添加项目根目录到Python路径
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
from app import create_app
|
|
from database import DatabaseManager
|
|
from utils import Config
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
"""创建测试应用实例"""
|
|
config = Config()
|
|
config.DATABASE['path'] = ':memory:' # 使用内存数据库进行测试
|
|
app = create_app(config)
|
|
app.config['TESTING'] = True
|
|
return app
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
"""创建测试客户端"""
|
|
return app.test_client()
|
|
|
|
|
|
@pytest.fixture
|
|
def db():
|
|
"""创建测试数据库"""
|
|
db_manager = DatabaseManager(':memory:')
|
|
db_manager.init_database()
|
|
return db_manager
|
|
|
|
|
|
class TestHealthAPI:
|
|
"""健康检查API测试"""
|
|
|
|
def test_health_check(self, client):
|
|
"""测试健康检查端点"""
|
|
response = client.get('/api/health')
|
|
assert response.status_code == 200
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
assert 'timestamp' in data['data']
|
|
assert 'uptime' in data['data']
|
|
|
|
|
|
class TestSystemAPI:
|
|
"""系统信息API测试"""
|
|
|
|
def test_system_info(self, client):
|
|
"""测试系统信息端点"""
|
|
response = client.get('/api/system/info')
|
|
assert response.status_code == 200
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
assert 'system' in data['data']
|
|
assert 'python' in data['data']
|
|
assert 'memory' in data['data']
|
|
assert 'disk' in data['data']
|
|
|
|
|
|
class TestPatientAPI:
|
|
"""患者管理API测试"""
|
|
|
|
def test_create_patient(self, client):
|
|
"""测试创建患者"""
|
|
patient_data = {
|
|
'name': '测试患者',
|
|
'age': 30,
|
|
'gender': 'male',
|
|
'height': 175,
|
|
'weight': 70,
|
|
'phone': '13800138000',
|
|
'email': 'test@example.com',
|
|
'medical_history': '无',
|
|
'notes': '测试患者'
|
|
}
|
|
|
|
response = client.post('/api/patients',
|
|
data=json.dumps(patient_data),
|
|
content_type='application/json')
|
|
|
|
assert response.status_code == 201
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
assert 'patient_id' in data['data']
|
|
|
|
def test_get_patients(self, client):
|
|
"""测试获取患者列表"""
|
|
response = client.get('/api/patients')
|
|
assert response.status_code == 200
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
assert 'patients' in data['data']
|
|
assert 'total' in data['data']
|
|
assert 'page' in data['data']
|
|
assert 'per_page' in data['data']
|
|
|
|
def test_get_patient_invalid_id(self, client):
|
|
"""测试获取不存在的患者"""
|
|
response = client.get('/api/patients/99999')
|
|
assert response.status_code == 404
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'error'
|
|
|
|
def test_create_patient_invalid_data(self, client):
|
|
"""测试创建患者时提供无效数据"""
|
|
invalid_data = {
|
|
'name': '', # 空名称
|
|
'age': -1, # 无效年龄
|
|
'gender': 'invalid' # 无效性别
|
|
}
|
|
|
|
response = client.post('/api/patients',
|
|
data=json.dumps(invalid_data),
|
|
content_type='application/json')
|
|
|
|
assert response.status_code == 400
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'error'
|
|
|
|
|
|
class TestDeviceAPI:
|
|
"""设备管理API测试"""
|
|
|
|
@patch('device_manager.DeviceManager')
|
|
def test_device_status(self, mock_device_manager, client):
|
|
"""测试设备状态查询"""
|
|
# 模拟设备管理器
|
|
mock_instance = MagicMock()
|
|
mock_instance.get_device_status.return_value = {
|
|
'camera': {'connected': True, 'status': 'ready'},
|
|
'imu': {'connected': True, 'status': 'ready'},
|
|
'pressure': {'connected': True, 'status': 'ready'}
|
|
}
|
|
mock_device_manager.return_value = mock_instance
|
|
|
|
response = client.get('/api/devices/status')
|
|
assert response.status_code == 200
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
assert 'devices' in data['data']
|
|
|
|
@patch('device_manager.DeviceManager')
|
|
def test_device_refresh(self, mock_device_manager, client):
|
|
"""测试设备刷新"""
|
|
mock_instance = MagicMock()
|
|
mock_instance.refresh_devices.return_value = True
|
|
mock_device_manager.return_value = mock_instance
|
|
|
|
response = client.post('/api/devices/refresh')
|
|
assert response.status_code == 200
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
|
|
|
|
class TestDetectionAPI:
|
|
"""检测管理API测试"""
|
|
|
|
@patch('detection_engine.DetectionEngine')
|
|
def test_start_detection(self, mock_detection_engine, client):
|
|
"""测试开始检测"""
|
|
mock_instance = MagicMock()
|
|
mock_instance.start_session.return_value = 'session_123'
|
|
mock_detection_engine.return_value = mock_instance
|
|
|
|
detection_config = {
|
|
'patient_id': 1,
|
|
'duration': 60,
|
|
'recording': True,
|
|
'real_time_analysis': True
|
|
}
|
|
|
|
response = client.post('/api/detection/start',
|
|
data=json.dumps(detection_config),
|
|
content_type='application/json')
|
|
|
|
assert response.status_code == 200
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
assert 'session_id' in data['data']
|
|
|
|
@patch('detection_engine.DetectionEngine')
|
|
def test_stop_detection(self, mock_detection_engine, client):
|
|
"""测试停止检测"""
|
|
mock_instance = MagicMock()
|
|
mock_instance.stop_session.return_value = True
|
|
mock_detection_engine.return_value = mock_instance
|
|
|
|
response = client.post('/api/detection/stop/session_123')
|
|
assert response.status_code == 200
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'success'
|
|
|
|
def test_start_detection_invalid_config(self, client):
|
|
"""测试使用无效配置开始检测"""
|
|
invalid_config = {
|
|
'patient_id': -1, # 无效患者ID
|
|
'duration': 0 # 无效持续时间
|
|
}
|
|
|
|
response = client.post('/api/detection/start',
|
|
data=json.dumps(invalid_config),
|
|
content_type='application/json')
|
|
|
|
assert response.status_code == 400
|
|
data = json.loads(response.data)
|
|
assert data['status'] == 'error'
|
|
|
|
|
|
class TestDatabaseManager:
|
|
"""数据库管理器测试"""
|
|
|
|
def test_create_patient(self, db):
|
|
"""测试创建患者"""
|
|
patient_data = {
|
|
'name': '测试患者',
|
|
'age': 30,
|
|
'gender': 'male',
|
|
'height': 175,
|
|
'weight': 70
|
|
}
|
|
|
|
patient_id = db.create_patient(patient_data)
|
|
assert patient_id is not None
|
|
assert isinstance(patient_id, int)
|
|
|
|
def test_get_patient(self, db):
|
|
"""测试获取患者信息"""
|
|
# 先创建患者
|
|
patient_data = {
|
|
'name': '测试患者',
|
|
'age': 30,
|
|
'gender': 'male'
|
|
}
|
|
patient_id = db.create_patient(patient_data)
|
|
|
|
# 获取患者信息
|
|
patient = db.get_patient(patient_id)
|
|
assert patient is not None
|
|
assert patient['name'] == '测试患者'
|
|
assert patient['age'] == 30
|
|
|
|
def test_get_nonexistent_patient(self, db):
|
|
"""测试获取不存在的患者"""
|
|
patient = db.get_patient(99999)
|
|
assert patient is None
|
|
|
|
def test_create_session(self, db):
|
|
"""测试创建检测会话"""
|
|
# 先创建患者
|
|
patient_data = {'name': '测试患者', 'age': 30, 'gender': 'male'}
|
|
patient_id = db.create_patient(patient_data)
|
|
|
|
# 创建会话
|
|
session_data = {
|
|
'patient_id': patient_id,
|
|
'duration': 60,
|
|
'recording': True,
|
|
'config': {'test': 'config'}
|
|
}
|
|
|
|
session_id = db.create_session(session_data)
|
|
assert session_id is not None
|
|
assert isinstance(session_id, str)
|
|
|
|
def test_update_session_status(self, db):
|
|
"""测试更新会话状态"""
|
|
# 创建患者和会话
|
|
patient_data = {'name': '测试患者', 'age': 30, 'gender': 'male'}
|
|
patient_id = db.create_patient(patient_data)
|
|
|
|
session_data = {
|
|
'patient_id': patient_id,
|
|
'duration': 60,
|
|
'recording': True
|
|
}
|
|
session_id = db.create_session(session_data)
|
|
|
|
# 更新状态
|
|
result = db.update_session_status(session_id, 'completed')
|
|
assert result is True
|
|
|
|
# 验证状态已更新
|
|
session = db.get_session(session_id)
|
|
assert session['status'] == 'completed'
|
|
|
|
|
|
if __name__ == '__main__':
|
|
pytest.main([__file__, '-v']) |