feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
This commit is contained in:
@@ -0,0 +1,378 @@
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
from apitest.models.test_models import (
|
||||
HTTPMethod,
|
||||
ValidationRule,
|
||||
TestCase,
|
||||
PerformanceMetrics,
|
||||
TestResult,
|
||||
TestSuiteResult
|
||||
)
|
||||
from apitest.models.exceptions import (
|
||||
APITestException,
|
||||
ConfigException,
|
||||
DataException,
|
||||
AuthException,
|
||||
RequestException,
|
||||
ValidationException,
|
||||
TestRunException,
|
||||
ReportException
|
||||
)
|
||||
|
||||
|
||||
class TestHTTPMethod:
|
||||
"""测试HTTPMethod枚举"""
|
||||
|
||||
def test_http_methods(self):
|
||||
"""测试所有HTTP方法"""
|
||||
assert HTTPMethod.GET.value == "GET"
|
||||
assert HTTPMethod.POST.value == "POST"
|
||||
assert HTTPMethod.PUT.value == "PUT"
|
||||
assert HTTPMethod.DELETE.value == "DELETE"
|
||||
assert HTTPMethod.PATCH.value == "PATCH"
|
||||
assert HTTPMethod.HEAD.value == "HEAD"
|
||||
assert HTTPMethod.OPTIONS.value == "OPTIONS"
|
||||
|
||||
|
||||
class TestValidationRule:
|
||||
"""测试ValidationRule数据类"""
|
||||
|
||||
def test_validation_rule_creation(self):
|
||||
"""测试创建验证规则"""
|
||||
rule = ValidationRule(
|
||||
type="status_code",
|
||||
expected=200,
|
||||
message="状态码应为200"
|
||||
)
|
||||
assert rule.type == "status_code"
|
||||
assert rule.expected == 200
|
||||
assert rule.message == "状态码应为200"
|
||||
assert rule.json_path is None
|
||||
|
||||
def test_validation_rule_with_json_path(self):
|
||||
"""测试带JSON路径的验证规则"""
|
||||
rule = ValidationRule(
|
||||
type="json_path",
|
||||
expected="success",
|
||||
json_path="$.status",
|
||||
message="状态应为success"
|
||||
)
|
||||
assert rule.json_path == "$.status"
|
||||
|
||||
def test_validation_rule_immutability(self):
|
||||
"""测试验证规则的不可变性"""
|
||||
rule = ValidationRule(type="status_code", expected=200)
|
||||
with pytest.raises(Exception):
|
||||
rule.expected = 201
|
||||
|
||||
|
||||
class TestTestCaseModel:
|
||||
"""测试TestCase数据类"""
|
||||
|
||||
def test_test_case_creation(self):
|
||||
"""测试创建测试用例"""
|
||||
test_case = TestCase(
|
||||
id="TC001",
|
||||
name="测试用户登录",
|
||||
description="验证用户登录功能",
|
||||
module="user",
|
||||
endpoint="/api/auth/login",
|
||||
method=HTTPMethod.POST,
|
||||
headers={"Content-Type": "application/json"},
|
||||
body={"username": "admin", "password": "admin123"}
|
||||
)
|
||||
assert test_case.id == "TC001"
|
||||
assert test_case.name == "测试用户登录"
|
||||
assert test_case.method == HTTPMethod.POST
|
||||
assert test_case.auth_required == True
|
||||
assert test_case.enabled == True
|
||||
assert test_case.dependencies == []
|
||||
assert test_case.validations == []
|
||||
assert test_case.tags == []
|
||||
|
||||
def test_test_case_with_dependencies(self):
|
||||
"""测试带依赖的测试用例"""
|
||||
test_case = TestCase(
|
||||
id="TC002",
|
||||
name="测试获取用户信息",
|
||||
description="验证获取用户信息功能",
|
||||
module="user",
|
||||
endpoint="/api/user/info",
|
||||
method=HTTPMethod.GET,
|
||||
headers={"Content-Type": "application/json"},
|
||||
dependencies=["TC001"]
|
||||
)
|
||||
assert len(test_case.dependencies) == 1
|
||||
assert "TC001" in test_case.dependencies
|
||||
|
||||
def test_test_case_with_validations(self):
|
||||
"""测试带验证规则的测试用例"""
|
||||
test_case = TestCase(
|
||||
id="TC003",
|
||||
name="测试创建用户",
|
||||
description="验证创建用户功能",
|
||||
module="user",
|
||||
endpoint="/api/user",
|
||||
method=HTTPMethod.POST,
|
||||
headers={"Content-Type": "application/json"},
|
||||
body={"username": "test", "password": "test123"},
|
||||
validations=[
|
||||
{"type": "status_code", "expected": 201},
|
||||
{"type": "json_path", "json_path": "$.success", "expected": True}
|
||||
]
|
||||
)
|
||||
assert len(test_case.validations) == 2
|
||||
assert test_case.validations[0]["type"] == "status_code"
|
||||
|
||||
def test_test_case_immutability(self):
|
||||
"""测试测试用例的不可变性"""
|
||||
test_case = TestCase(
|
||||
id="TC004",
|
||||
name="测试用例",
|
||||
description="测试",
|
||||
module="test",
|
||||
endpoint="/api/test",
|
||||
method=HTTPMethod.GET,
|
||||
headers={}
|
||||
)
|
||||
with pytest.raises(Exception):
|
||||
test_case.name = "修改后的名称"
|
||||
|
||||
|
||||
class TestPerformanceMetrics:
|
||||
"""测试PerformanceMetrics数据类"""
|
||||
|
||||
def test_performance_metrics_creation(self):
|
||||
"""测试创建性能指标"""
|
||||
metrics = PerformanceMetrics(
|
||||
response_time=500,
|
||||
request_size=100,
|
||||
response_size=200,
|
||||
timestamp=datetime.now()
|
||||
)
|
||||
assert metrics.response_time == 500
|
||||
assert metrics.request_size == 100
|
||||
assert metrics.response_size == 200
|
||||
|
||||
def test_performance_metrics_to_dict(self):
|
||||
"""测试性能指标转换为字典"""
|
||||
timestamp = datetime.now()
|
||||
metrics = PerformanceMetrics(
|
||||
response_time=500,
|
||||
request_size=100,
|
||||
response_size=200,
|
||||
timestamp=timestamp
|
||||
)
|
||||
result = metrics.to_dict()
|
||||
assert result["response_time"] == 500
|
||||
assert result["request_size"] == 100
|
||||
assert result["response_size"] == 200
|
||||
assert result["timestamp"] == timestamp.isoformat()
|
||||
|
||||
|
||||
class TestTestResultModel:
|
||||
"""测试TestResult数据类"""
|
||||
|
||||
def test_test_result_creation(self):
|
||||
"""测试创建测试结果"""
|
||||
test_case = TestCase(
|
||||
id="TC001",
|
||||
name="测试用例",
|
||||
description="测试",
|
||||
module="test",
|
||||
endpoint="/api/test",
|
||||
method=HTTPMethod.GET,
|
||||
headers={}
|
||||
)
|
||||
result = TestResult(
|
||||
test_case=test_case,
|
||||
passed=True,
|
||||
status_code=200,
|
||||
response_body={"success": True},
|
||||
response_headers={"Content-Type": "application/json"}
|
||||
)
|
||||
assert result.passed == True
|
||||
assert result.status_code == 200
|
||||
assert result.execution_time == 0.0
|
||||
assert result.retry_count == 0
|
||||
assert result.timestamp is not None
|
||||
|
||||
def test_test_result_with_performance(self):
|
||||
"""测试带性能指标的测试结果"""
|
||||
test_case = TestCase(
|
||||
id="TC001",
|
||||
name="测试用例",
|
||||
description="测试",
|
||||
module="test",
|
||||
endpoint="/api/test",
|
||||
method=HTTPMethod.GET,
|
||||
headers={}
|
||||
)
|
||||
performance = PerformanceMetrics(
|
||||
response_time=500,
|
||||
request_size=100,
|
||||
response_size=200,
|
||||
timestamp=datetime.now()
|
||||
)
|
||||
result = TestResult(
|
||||
test_case=test_case,
|
||||
passed=True,
|
||||
status_code=200,
|
||||
response_body={"success": True},
|
||||
response_headers={},
|
||||
performance=performance,
|
||||
execution_time=0.5
|
||||
)
|
||||
assert result.performance == performance
|
||||
assert result.execution_time == 0.5
|
||||
|
||||
def test_test_result_to_dict(self):
|
||||
"""测试测试结果转换为字典"""
|
||||
test_case = TestCase(
|
||||
id="TC001",
|
||||
name="测试用例",
|
||||
description="测试",
|
||||
module="test",
|
||||
endpoint="/api/test",
|
||||
method=HTTPMethod.GET,
|
||||
headers={}
|
||||
)
|
||||
result = TestResult(
|
||||
test_case=test_case,
|
||||
passed=True,
|
||||
status_code=200,
|
||||
response_body={"success": True},
|
||||
response_headers={}
|
||||
)
|
||||
result_dict = result.to_dict()
|
||||
assert result_dict["test_case_id"] == "TC001"
|
||||
assert result_dict["test_case_name"] == "测试用例"
|
||||
assert result_dict["passed"] == True
|
||||
assert result_dict["status_code"] == 200
|
||||
|
||||
|
||||
class TestTestSuiteResultModel:
|
||||
"""测试TestSuiteResult数据类"""
|
||||
|
||||
def test_test_suite_result_creation(self):
|
||||
"""测试创建测试套件结果"""
|
||||
suite_result = TestSuiteResult(
|
||||
suite_name="user",
|
||||
total=10,
|
||||
passed=8,
|
||||
failed=1,
|
||||
skipped=1,
|
||||
results=[],
|
||||
start_time=datetime.now()
|
||||
)
|
||||
assert suite_result.suite_name == "user"
|
||||
assert suite_result.total == 10
|
||||
assert suite_result.passed == 8
|
||||
assert suite_result.failed == 1
|
||||
assert suite_result.skipped == 1
|
||||
assert suite_result.end_time is None
|
||||
|
||||
def test_test_suite_result_duration(self):
|
||||
"""测试测试套件执行时长"""
|
||||
start_time = datetime.now()
|
||||
end_time = datetime.now()
|
||||
suite_result = TestSuiteResult(
|
||||
suite_name="user",
|
||||
total=10,
|
||||
passed=8,
|
||||
failed=1,
|
||||
skipped=1,
|
||||
results=[],
|
||||
start_time=start_time,
|
||||
end_time=end_time
|
||||
)
|
||||
assert suite_result.duration >= 0
|
||||
|
||||
def test_test_suite_result_pass_rate(self):
|
||||
"""测试测试套件通过率"""
|
||||
suite_result = TestSuiteResult(
|
||||
suite_name="user",
|
||||
total=10,
|
||||
passed=8,
|
||||
failed=1,
|
||||
skipped=1,
|
||||
results=[],
|
||||
start_time=datetime.now()
|
||||
)
|
||||
assert suite_result.pass_rate == 80.0
|
||||
|
||||
def test_test_suite_result_pass_rate_zero_total(self):
|
||||
"""测试总数为0时的通过率"""
|
||||
suite_result = TestSuiteResult(
|
||||
suite_name="user",
|
||||
total=0,
|
||||
passed=0,
|
||||
failed=0,
|
||||
skipped=0,
|
||||
results=[],
|
||||
start_time=datetime.now()
|
||||
)
|
||||
assert suite_result.pass_rate == 0.0
|
||||
|
||||
def test_test_suite_result_to_dict(self):
|
||||
"""测试测试套件结果转换为字典"""
|
||||
suite_result = TestSuiteResult(
|
||||
suite_name="user",
|
||||
total=10,
|
||||
passed=8,
|
||||
failed=1,
|
||||
skipped=1,
|
||||
results=[],
|
||||
start_time=datetime.now()
|
||||
)
|
||||
result_dict = suite_result.to_dict()
|
||||
assert result_dict["suite_name"] == "user"
|
||||
assert result_dict["total"] == 10
|
||||
assert result_dict["passed"] == 8
|
||||
assert result_dict["failed"] == 1
|
||||
assert result_dict["skipped"] == 1
|
||||
assert result_dict["pass_rate"] == 80.0
|
||||
|
||||
|
||||
class TestExceptions:
|
||||
"""测试异常类"""
|
||||
|
||||
def test_api_test_exception(self):
|
||||
"""测试API测试基础异常"""
|
||||
with pytest.raises(APITestException):
|
||||
raise APITestException("测试异常")
|
||||
|
||||
def test_config_exception(self):
|
||||
"""测试配置异常"""
|
||||
with pytest.raises(ConfigException):
|
||||
raise ConfigException("配置错误")
|
||||
|
||||
def test_data_exception(self):
|
||||
"""测试数据异常"""
|
||||
with pytest.raises(DataException):
|
||||
raise DataException("数据错误")
|
||||
|
||||
def test_auth_exception(self):
|
||||
"""测试认证异常"""
|
||||
with pytest.raises(AuthException):
|
||||
raise AuthException("认证失败")
|
||||
|
||||
def test_request_exception(self):
|
||||
"""测试请求异常"""
|
||||
with pytest.raises(RequestException):
|
||||
raise RequestException("请求失败")
|
||||
|
||||
def test_validation_exception(self):
|
||||
"""测试验证异常"""
|
||||
with pytest.raises(ValidationException):
|
||||
raise ValidationException("验证失败")
|
||||
|
||||
def test_test_execution_exception(self):
|
||||
"""测试测试执行异常"""
|
||||
with pytest.raises(TestRunException):
|
||||
raise TestRunException("执行失败")
|
||||
|
||||
def test_report_exception(self):
|
||||
"""测试报告生成异常"""
|
||||
with pytest.raises(ReportException):
|
||||
raise ReportException("报告生成失败")
|
||||
Reference in New Issue
Block a user