Skip to content

Commit 5e601e8

Browse files
committed
test: rewrite test_convert_array_invalid_json using caplog fixture (simpler, robust)
1 parent 81ce7a8 commit 5e601e8

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

tests/utils_test.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from unittest.mock import patch
21
import json
32

43
import numpy as np
@@ -69,32 +68,31 @@ def test_convert_array_empty():
6968
assert result.is_simulation_result is False
7069

7170

72-
def test_convert_array_invalid_json():
71+
def test_convert_array_invalid_json(caplog):
7372
"""Test that invalid JSON tensor data is skipped with a warning instead of crashing.
7473
7574
Previously json.loads() was unguarded and would crash the entire conversion.
7675
After the fix, malformed JSON tensors are skipped and logged as warnings so
7776
all other valid tensors in the same response are still returned.
7877
"""
78+
import logging
79+
7980
invalid_json_data = [
8081
[], # Empty number tensors
8182
[], # Empty string tensors
8283
[["invalid_json", '{"key": invalid}']], # Invalid JSON
8384
False,
8485
]
8586

86-
import logging
87-
with pytest.warns(None) as warning_list:
88-
# Capture logging.warning calls via caplog-style check
89-
with patch("opengradient.client._conversions.logging") as mock_log:
90-
result = utils.convert_array_to_model_output(invalid_json_data)
87+
with caplog.at_level(logging.WARNING):
88+
result = utils.convert_array_to_model_output(invalid_json_data)
9189

92-
# Should not raise; result should have empty jsons dict (bad tensor skipped)
90+
# Should not raise; malformed tensor is skipped
9391
assert isinstance(result, types.ModelOutput)
9492
assert result.jsons == {}
95-
# The warning should have been logged
96-
mock_log.warning.assert_called_once()
97-
assert "invalid_json" in mock_log.warning.call_args[0][0]
93+
94+
# A warning should have been emitted mentioning the tensor name
95+
assert any("invalid_json" in record.message for record in caplog.records)
9896

9997

10098
def test_convert_array_invalid_shape():

0 commit comments

Comments
 (0)