|
| 1 | +"""Error handling tests for SQLAlchemy serializer.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import sqlalchemy as sa |
| 5 | + |
| 6 | +from sqlalchemy_serializer import SerializerMixin |
| 7 | + |
| 8 | +from .models import Base |
| 9 | + |
| 10 | + |
| 11 | +class ModelWithFailingProperty(Base, SerializerMixin): |
| 12 | + """Model with a property that raises an exception.""" |
| 13 | + |
| 14 | + __tablename__ = "model_with_failing_property" |
| 15 | + serialize_only = () |
| 16 | + serialize_rules = () |
| 17 | + |
| 18 | + id = sa.Column(sa.Integer, primary_key=True) |
| 19 | + string = sa.Column(sa.String(256), default="test") |
| 20 | + |
| 21 | + @property |
| 22 | + def failing_prop(self): |
| 23 | + """Property that raises an exception.""" |
| 24 | + raise ValueError("Property access failed") |
| 25 | + |
| 26 | + @property |
| 27 | + def valid_prop(self): |
| 28 | + """Property that works correctly.""" |
| 29 | + return "valid value" |
| 30 | + |
| 31 | + |
| 32 | +class ModelWithFailingMethod(Base, SerializerMixin): |
| 33 | + """Model with a method that raises an exception.""" |
| 34 | + |
| 35 | + __tablename__ = "model_with_failing_method" |
| 36 | + serialize_only = () |
| 37 | + serialize_rules = () |
| 38 | + |
| 39 | + id = sa.Column(sa.Integer, primary_key=True) |
| 40 | + string = sa.Column(sa.String(256), default="test") |
| 41 | + |
| 42 | + def failing_method(self): |
| 43 | + """Method that raises an exception.""" |
| 44 | + raise RuntimeError("Method call failed") |
| 45 | + |
| 46 | + def valid_method(self): |
| 47 | + """Method that works correctly.""" |
| 48 | + return "valid result" |
| 49 | + |
| 50 | + |
| 51 | +class ModelWithFailingCustomSerializer(Base, SerializerMixin): |
| 52 | + """Model with custom serializer that raises an exception.""" |
| 53 | + |
| 54 | + __tablename__ = "model_with_failing_custom_serializer" |
| 55 | + serialize_only = () |
| 56 | + serialize_rules = () |
| 57 | + |
| 58 | + id = sa.Column(sa.Integer, primary_key=True) |
| 59 | + string = sa.Column(sa.String(256), default="test") |
| 60 | + number = sa.Column(sa.Integer, default=42) |
| 61 | + |
| 62 | + |
| 63 | +def test_property_raises_exception_default_behavior(get_instance): |
| 64 | + model = get_instance(ModelWithFailingProperty) |
| 65 | + |
| 66 | + with pytest.raises(ValueError, match="Property access failed"): |
| 67 | + model.to_dict(rules=("failing_prop",)) |
| 68 | + |
| 69 | + |
| 70 | +def test_method_raises_exception_default_behavior(get_instance): |
| 71 | + model = get_instance(ModelWithFailingMethod) |
| 72 | + |
| 73 | + with pytest.raises(RuntimeError, match="Method call failed"): |
| 74 | + model.to_dict(rules=("failing_method",)) |
| 75 | + |
| 76 | + |
| 77 | +def test_custom_serializer_raises_exception_default_behavior(get_instance): |
| 78 | + model = get_instance(ModelWithFailingCustomSerializer) |
| 79 | + |
| 80 | + def failing_serializer(_value): |
| 81 | + """Custom serializer that raises an exception.""" |
| 82 | + raise TypeError("Custom serializer failed") |
| 83 | + |
| 84 | + with pytest.raises(TypeError, match="Custom serializer failed"): |
| 85 | + model.to_dict(serialize_columns={"string": failing_serializer}) |
0 commit comments