|
1 | | -from unittest.mock import patch |
2 | 1 | import json |
3 | 2 |
|
4 | 3 | import numpy as np |
@@ -69,32 +68,31 @@ def test_convert_array_empty(): |
69 | 68 | assert result.is_simulation_result is False |
70 | 69 |
|
71 | 70 |
|
72 | | -def test_convert_array_invalid_json(): |
| 71 | +def test_convert_array_invalid_json(caplog): |
73 | 72 | """Test that invalid JSON tensor data is skipped with a warning instead of crashing. |
74 | 73 |
|
75 | 74 | Previously json.loads() was unguarded and would crash the entire conversion. |
76 | 75 | After the fix, malformed JSON tensors are skipped and logged as warnings so |
77 | 76 | all other valid tensors in the same response are still returned. |
78 | 77 | """ |
| 78 | + import logging |
| 79 | + |
79 | 80 | invalid_json_data = [ |
80 | 81 | [], # Empty number tensors |
81 | 82 | [], # Empty string tensors |
82 | 83 | [["invalid_json", '{"key": invalid}']], # Invalid JSON |
83 | 84 | False, |
84 | 85 | ] |
85 | 86 |
|
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) |
91 | 89 |
|
92 | | - # Should not raise; result should have empty jsons dict (bad tensor skipped) |
| 90 | + # Should not raise; malformed tensor is skipped |
93 | 91 | assert isinstance(result, types.ModelOutput) |
94 | 92 | 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) |
98 | 96 |
|
99 | 97 |
|
100 | 98 | def test_convert_array_invalid_shape(): |
|
0 commit comments