65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
"""异常类型单元测试"""
|
|
|
|
import pytest
|
|
from exceptions import ApiError, ParseError, RateLimitError, UnsupportedFormatError
|
|
|
|
|
|
class TestParseError:
|
|
def test_attributes(self):
|
|
err = ParseError("test.pdf", "文件损坏")
|
|
assert err.file_name == "test.pdf"
|
|
assert err.reason == "文件损坏"
|
|
|
|
def test_message_format(self):
|
|
err = ParseError("data.csv", "编码无法识别")
|
|
assert str(err) == "解析失败 [data.csv]: 编码无法识别"
|
|
|
|
def test_is_exception(self):
|
|
err = ParseError("f.txt", "reason")
|
|
assert isinstance(err, Exception)
|
|
|
|
|
|
class TestUnsupportedFormatError:
|
|
def test_inherits_parse_error(self):
|
|
err = UnsupportedFormatError("file.xyz", ".xyz")
|
|
assert isinstance(err, ParseError)
|
|
|
|
def test_extension_attribute(self):
|
|
err = UnsupportedFormatError("file.abc", ".abc")
|
|
assert err.extension == ".abc"
|
|
|
|
def test_message_format(self):
|
|
err = UnsupportedFormatError("doc.bin", ".bin")
|
|
assert str(err) == "解析失败 [doc.bin]: 不支持的文件格式: .bin"
|
|
|
|
def test_file_name_propagated(self):
|
|
err = UnsupportedFormatError("my_file.xyz", ".xyz")
|
|
assert err.file_name == "my_file.xyz"
|
|
assert err.reason == "不支持的文件格式: .xyz"
|
|
|
|
|
|
class TestApiError:
|
|
def test_with_status_code(self):
|
|
err = ApiError("服务端错误", status_code=500)
|
|
assert err.status_code == 500
|
|
assert str(err) == "服务端错误"
|
|
|
|
def test_without_status_code(self):
|
|
err = ApiError("网络错误")
|
|
assert err.status_code is None
|
|
assert str(err) == "网络错误"
|
|
|
|
def test_is_exception(self):
|
|
assert isinstance(ApiError("msg"), Exception)
|
|
|
|
|
|
class TestRateLimitError:
|
|
def test_inherits_api_error(self):
|
|
err = RateLimitError("速率限制", status_code=429)
|
|
assert isinstance(err, ApiError)
|
|
|
|
def test_status_code(self):
|
|
err = RateLimitError("速率限制", status_code=429)
|
|
assert err.status_code == 429
|
|
assert str(err) == "速率限制"
|