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

View File

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