File size: 3,188 Bytes
f21249a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
"""
Unit tests for custom exceptions.
"""
import pytest
from exceptions import (
KerdosError,
ConfigurationError,
ModelError,
ModelLoadError,
DataError,
DataValidationError,
TrainingError,
DeploymentError
)
class TestKerdosError:
"""Tests for base KerdosError."""
def test_basic_error(self):
"""Test basic error creation."""
error = KerdosError("Test error")
assert str(error) == "Test error"
assert error.message == "Test error"
assert error.details == {}
def test_error_with_details(self):
"""Test error with details."""
error = KerdosError(
"Test error",
details={"key": "value", "number": 42}
)
assert "Test error" in str(error)
assert "key=value" in str(error)
assert "number=42" in str(error)
def test_error_inheritance(self):
"""Test that KerdosError inherits from Exception."""
error = KerdosError("Test")
assert isinstance(error, Exception)
class TestSpecificErrors:
"""Tests for specific error types."""
def test_configuration_error(self):
"""Test ConfigurationError."""
error = ConfigurationError("Invalid config")
assert isinstance(error, KerdosError)
assert str(error) == "Invalid config"
def test_model_error(self):
"""Test ModelError."""
error = ModelError("Model failed")
assert isinstance(error, KerdosError)
def test_model_load_error(self):
"""Test ModelLoadError."""
error = ModelLoadError(
"Failed to load model",
details={"path": "/path/to/model"}
)
assert isinstance(error, ModelError)
assert "path=/path/to/model" in str(error)
def test_data_validation_error(self):
"""Test DataValidationError."""
error = DataValidationError(
"Invalid data format",
details={"expected": "json", "got": "csv"}
)
assert isinstance(error, DataError)
assert "expected=json" in str(error)
def test_training_error(self):
"""Test TrainingError."""
error = TrainingError("Training failed")
assert isinstance(error, KerdosError)
def test_deployment_error(self):
"""Test DeploymentError."""
error = DeploymentError("Deployment failed")
assert isinstance(error, KerdosError)
class TestErrorRaising:
"""Tests for raising and catching errors."""
def test_raise_and_catch(self):
"""Test raising and catching KerdosError."""
with pytest.raises(KerdosError) as exc_info:
raise KerdosError("Test error")
assert str(exc_info.value) == "Test error"
def test_catch_specific_error(self):
"""Test catching specific error types."""
with pytest.raises(ConfigurationError):
raise ConfigurationError("Config error")
def test_catch_base_error(self):
"""Test catching base error catches specific errors."""
with pytest.raises(KerdosError):
raise ConfigurationError("Config error")
|