Skip to content

Commit a967a4e

Browse files
committed
Add unit tests for JSON sanitization methods to satisfy coverage
1 parent 6ec6809 commit a967a4e

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

tests/objects/test_operation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,28 @@ async def test_init_source_seeds_relationship_with_resolved_facts(self, knowledg
731731
assert seeded_rel.target.value == 's3cr3t', (
732732
'Relationship target fact value should be resolved from the source fact list, not None'
733733
)
734+
735+
def test_sanitize_for_json(self):
736+
"""Test that Operation._sanitize_for_json properly handles un-serializable inputs."""
737+
# Contains a null byte, normal string, and a valid dictionary
738+
test_cases = [
739+
'normal string',
740+
'string\x00with null',
741+
{'dict_key': 'dict_val'},
742+
'🔥 emoji', # Emojis are fine but shouldn't crash
743+
{'nested': 'bad\x00string'} # Dictionary with nested bad string
744+
]
745+
746+
for case in test_cases:
747+
sanitized = Operation._sanitize_for_json(case)
748+
# The output must be JSON serializable
749+
serialized = json.dumps(sanitized)
750+
assert serialized is not None
751+
752+
# Test surrogate fallback specifically
753+
bad_surrogate = 'hello\ud800world'
754+
sanitized_surrogate = Operation._sanitize_for_json(bad_surrogate)
755+
try:
756+
json.dumps(sanitized_surrogate)
757+
except UnicodeEncodeError:
758+
pytest.fail("Sanitized surrogate failed serialization")

0 commit comments

Comments
 (0)