Fix test failures for serialization changes

- Update test_invalid_input_types to test actual serialization failure
- Update test_interpolate_only_unsupported_type_error with proper __repr__ method
- Update test_interpolate_invalid_type_validation to test serialization vs rejection
- Update test_interpolate_custom_object_validation to distinguish serializable vs unserializable objects
- All tests now align with new behavior where unsupported types are serialized instead of rejected

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-31 19:53:59 +00:00
parent 80b48208d5
commit 1bf2e760ab
2 changed files with 38 additions and 47 deletions

View File

@@ -1279,54 +1279,40 @@ def test_interpolate_complex_combination():
def test_interpolate_invalid_type_validation():
# Test with invalid top-level type
with pytest.raises(ValueError) as excinfo:
interpolate_only("{data}", {"data": set()}) # type: ignore we are purposely testing this failure
assert "Unsupported type set" in str(excinfo.value)
# Test with invalid nested type
invalid_nested = {
"profile": {
"name": "John",
"age": 30,
"tags": {"a", "b", "c"}, # Set is invalid
}
}
with pytest.raises(ValueError) as excinfo:
interpolate_only("{data}", {"data": invalid_nested})
assert "Unsupported type set" in str(excinfo.value)
# Test with type that fails serialization
class UnserializableObject:
def __str__(self):
raise Exception("Cannot serialize")
def __repr__(self):
raise Exception("Cannot serialize")
with pytest.raises(ValueError, match="Unable to serialize UnserializableObject"):
interpolate_only("{data}", {"data": UnserializableObject()})
result = interpolate_only("{data}", {"data": {1, 2, 3}})
assert "1" in result and "2" in result and "3" in result
def test_interpolate_custom_object_validation():
class CustomObject:
class SerializableCustomObject:
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
# Test with custom object at top level
with pytest.raises(ValueError) as excinfo:
interpolate_only("{obj}", {"obj": CustomObject(5)}) # type: ignore we are purposely testing this failure
assert "Unsupported type CustomObject" in str(excinfo.value)
# Test with nested custom object in dictionary
with pytest.raises(ValueError) as excinfo:
interpolate_only("{data}", {"data": {"valid": 1, "invalid": CustomObject(5)}})
assert "Unsupported type CustomObject" in str(excinfo.value)
# Test with nested custom object in list
with pytest.raises(ValueError) as excinfo:
interpolate_only("{data}", {"data": [1, "valid", CustomObject(5)]})
assert "Unsupported type CustomObject" in str(excinfo.value)
# Test with deeply nested custom object
with pytest.raises(ValueError) as excinfo:
interpolate_only(
"{data}", {"data": {"level1": {"level2": [{"level3": CustomObject(5)}]}}}
)
assert "Unsupported type CustomObject" in str(excinfo.value)
class UnserializableCustomObject:
def __init__(self, value):
self.value = value
def __str__(self):
raise Exception("Cannot serialize")
def __repr__(self):
raise Exception("Cannot serialize")
result = interpolate_only("{obj}", {"obj": SerializableCustomObject(5)})
assert "5" in result
with pytest.raises(ValueError, match="Unable to serialize UnserializableCustomObject"):
interpolate_only("{obj}", {"obj": UnserializableCustomObject(5)})
def test_interpolate_valid_complex_types():

View File

@@ -91,16 +91,19 @@ class TestInterpolateOnly:
assert "name" in str(excinfo.value)
def test_invalid_input_types(self):
"""Test that an error is raised with invalid input types."""
"""Test that an error is raised when serialization fails."""
class UnserializableObject:
def __str__(self):
raise Exception("Cannot convert to string")
def __repr__(self):
raise Exception("Cannot convert to string")
template = "Hello, {name}!"
# Using Any for this test since we're intentionally testing an invalid type
inputs: Dict[str, Any] = {"name": object()} # Object is not a valid input type
inputs: Dict[str, Any] = {"name": UnserializableObject()}
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="Unable to serialize UnserializableObject"):
interpolate_only(template, inputs)
assert "unsupported type" in str(excinfo.value).lower()
def test_empty_input_string(self):
"""Test handling of empty or None input string."""
inputs: Dict[str, Union[str, int, float, Dict[str, Any], List[Any]]] = {
@@ -223,6 +226,8 @@ class TestInterpolateOnly:
class CustomObject:
def __str__(self):
raise Exception("Cannot serialize")
def __repr__(self):
raise Exception("Cannot serialize")
with pytest.raises(ValueError, match="Unable to serialize CustomObject"):
interpolate_only("Value: {obj}", {"obj": CustomObject()})