You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
健康检查 API 测试
|
|
"""
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
class TestHealthEndpoints:
|
|
"""健康检查端点测试"""
|
|
|
|
def test_health_check_success(self, test_client: TestClient):
|
|
"""测试健康检查端点 - 正常情况"""
|
|
response = test_client.get("/api/v1/health")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "healthy"
|
|
assert data["model_loaded"] is True
|
|
assert "version" in data
|
|
|
|
def test_readiness_check_success(self, test_client: TestClient):
|
|
"""测试就绪检查端点 - 正常情况"""
|
|
response = test_client.get("/api/v1/health/ready")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "healthy"
|
|
assert data["model_loaded"] is True
|
|
|
|
def test_root_endpoint(self, test_client: TestClient):
|
|
"""测试根路径端点"""
|
|
response = test_client.get("/")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["name"] == "Vision-OCR API"
|
|
assert "version" in data
|
|
assert "docs" in data
|
|
|
|
|
|
class TestHealthEndpointsUnhealthy:
|
|
"""健康检查端点测试 - 模型未加载情况"""
|
|
|
|
def test_health_check_model_not_loaded(self, test_client: TestClient):
|
|
"""测试健康检查 - 模型未加载"""
|
|
# 临时设置模型未加载状态
|
|
original_state = test_client.app.state.model_loaded
|
|
test_client.app.state.model_loaded = False
|
|
|
|
try:
|
|
response = test_client.get("/api/v1/health")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "unhealthy"
|
|
assert data["model_loaded"] is False
|
|
finally:
|
|
# 恢复原始状态
|
|
test_client.app.state.model_loaded = original_state
|
|
|
|
def test_readiness_check_model_not_loaded(self, test_client: TestClient):
|
|
"""测试就绪检查 - 模型未加载"""
|
|
original_state = test_client.app.state.model_loaded
|
|
test_client.app.state.model_loaded = False
|
|
|
|
try:
|
|
response = test_client.get("/api/v1/health/ready")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "unhealthy"
|
|
assert data["model_loaded"] is False
|
|
finally:
|
|
test_client.app.state.model_loaded = original_state
|