import pytest import requests from datetime import datetime import json # 测试配置 class TestConfig: BASE_URL = "http://localhost:5000/api/v1" TEST_TOKEN = "test_token_123" # 测试用token HEADERS = { "Authorization": f"Bearer {TEST_TOKEN}", "Content-Type": "application/json" } # 测试夹具 @pytest.fixture def api_client(): class APIClient: def __init__(self): self.base_url = TestConfig.BASE_URL self.headers = TestConfig.HEADERS def get(self, endpoint, params=None): url = f"{self.base_url}{endpoint}" return requests.get(url, headers=self.headers, params=params) def post(self, endpoint, data): url = f"{self.base_url}{endpoint}" return requests.post(url, headers=self.headers, json=data) def put(self, endpoint, data): url = f"{self.base_url}{endpoint}" return requests.put(url, headers=self.headers, json=data) def delete(self, endpoint): url = f"{self.base_url}{endpoint}" return requests.delete(url, headers=self.headers) return APIClient() # 模板管理接口测试 class TestTemplateAPI: def test_get_template_list(self, api_client): """测试获取模板列表""" response = api_client.get("/templates") assert response.status_code == 200 data = response.json() assert "data" in data assert "templates" in data["data"] def test_get_template_with_filters(self, api_client): """测试带过滤条件的模板列表""" params = { "category": "软件开发", "industry": "互联网", "page": 1, "size": 10 } response = api_client.get("/templates", params=params) assert response.status_code == 200 data = response.json() assert "current_page" in data["data"] assert data["data"]["current_page"] == 1 def test_create_template(self, api_client): """测试创建模板""" template_data = { "name": "测试模板", "description": "这是一个测试模板", "category": "软件开发", "industry": "互联网", "profession": "开发工程师", "sub_category": "后端开发", "system_prompt": "你是一个专业的后端开发工程师..." } response = api_client.post("/templates", template_data) assert response.status_code == 200 data = response.json() assert "id" in data["data"] return data["data"]["id"] def test_get_template_detail(self, api_client): """测试获取单个模板详情""" # 先创建一个模板 template_id = self.test_create_template(api_client) response = api_client.get(f"/templates/{template_id}") assert response.status_code == 200 data = response.json() assert data["data"]["name"] == "测试模板" def test_update_template(self, api_client): """测试更新模板""" # 先创建一个模板 template_id = self.test_create_template(api_client) update_data = { "name": "更新后的模板", "description": "这是更新后的描述" } response = api_client.put(f"/templates/{template_id}", update_data) assert response.status_code == 200 # 验证更新结果 response = api_client.get(f"/templates/{template_id}") assert response.json()["data"]["name"] == "更新后的模板" def test_delete_template(self, api_client): """测试删除模板""" # 先创建一个模板 template_id = self.test_create_template(api_client) response = api_client.delete(f"/templates/{template_id}") assert response.status_code == 200 # 验证删除结果 response = api_client.get(f"/templates/{template_id}") assert response.status_code == 404 # 分类管理接口测试 class TestCategoryAPI: def test_get_categories(self, api_client): """测试获取分类列表""" response = api_client.get("/categories") assert response.status_code == 200 data = response.json() assert "categories" in data["data"] def test_create_category(self, api_client): """测试创建分类""" category_data = { "name": "测试分类", "icon": "test-icon", "description": "这是一个测试分类" } response = api_client.post("/categories", category_data) assert response.status_code == 200 # 搜索接口测试 class TestSearchAPI: def test_search_templates(self, api_client): """测试搜索模板""" params = { "keyword": "开发", "category": "软件开发" } response = api_client.get("/search/templates", params=params) assert response.status_code == 200 data = response.json() assert "results" in data["data"] # 统计接口测试 class TestStatisticsAPI: def test_get_template_statistics(self, api_client): """测试获取模板统计信息""" response = api_client.get("/statistics/templates") assert response.status_code == 200 data = response.json() assert "total_templates" in data["data"] assert "category_distribution" in data["data"] # 错误处理测试 class TestErrorHandling: def test_invalid_token(self, api_client): """测试无效token""" api_client.headers["Authorization"] = "Bearer invalid_token" response = api_client.get("/templates") assert response.status_code == 401 def test_invalid_request(self, api_client): """测试无效请求""" invalid_data = { "name": "" # 空名称,应该触发验证错误 } response = api_client.post("/templates", invalid_data) assert response.status_code == 400