|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 | from iris.callbacks.pipeline_trace import PipelineCallTraceStorage |
7 | | -from iris.io.dataclasses import AlignedTemplates, DistanceMatrix, OutputFieldSpec, WeightedIrisTemplate |
| 7 | +from iris.io.dataclasses import ( |
| 8 | + AlignedTemplates, |
| 9 | + DistanceMatrix, |
| 10 | + EyeCenters, |
| 11 | + Offgaze, |
| 12 | + OutputFieldSpec, |
| 13 | + WeightedIrisTemplate, |
| 14 | +) |
| 15 | +from iris.orchestration import output_builders as ob |
8 | 16 | from iris.orchestration.output_builders import ( |
9 | 17 | _build_from_spec, |
10 | 18 | build_aggregation_templates_orb_output, |
|
13 | 21 | build_simple_iris_pipeline_orb_output, |
14 | 22 | ) |
15 | 23 |
|
| 24 | +SAFE_SERIALIZE = getattr(ob, "__safe_serialize") |
| 25 | + |
16 | 26 |
|
17 | 27 | class TestOutputBuildersWithMissingKeys: |
18 | 28 | """Test output builders behavior when keys are missing from call_trace.""" |
@@ -265,3 +275,42 @@ def test_with_alignment_and_identity_filter(self, mock_call_trace_with_alignment |
265 | 275 | assert metadata["aligned_templates"]["reference_template_id"] == 0 |
266 | 276 | assert metadata["aligned_templates"]["distances"] == {(0, 0): 0.0} |
267 | 277 | assert metadata["post_identity_filter_templates_count"] == 1 |
| 278 | + |
| 279 | + |
| 280 | +class TestSafeSerialize: |
| 281 | + def test_none_returns_none(self): |
| 282 | + assert SAFE_SERIALIZE(None) is None |
| 283 | + |
| 284 | + def test_immutable_model_serialization(self): |
| 285 | + offgaze = Offgaze(score=0.5) |
| 286 | + assert SAFE_SERIALIZE(offgaze) == offgaze.serialize() |
| 287 | + |
| 288 | + eye_centers = EyeCenters(pupil_x=1.0, pupil_y=2.0, iris_x=3.0, iris_y=4.0) |
| 289 | + assert SAFE_SERIALIZE(eye_centers) == eye_centers.serialize() |
| 290 | + |
| 291 | + def test_numpy_array_to_list(self): |
| 292 | + arr = np.array([[1, 2], [3, 4]]) |
| 293 | + assert SAFE_SERIALIZE(arr) == arr.tolist() |
| 294 | + |
| 295 | + def test_list_and_tuple_recursion(self): |
| 296 | + offgaze = Offgaze(score=0.25) |
| 297 | + arr = np.array([1, 2, 3]) |
| 298 | + |
| 299 | + data_list = [1, "a", offgaze, arr] |
| 300 | + assert SAFE_SERIALIZE(data_list) == [1, "a", offgaze.serialize(), arr.tolist()] |
| 301 | + |
| 302 | + data_tuple = (True, 3.14, offgaze, arr) |
| 303 | + assert SAFE_SERIALIZE(data_tuple) == (True, 3.14, offgaze.serialize(), arr.tolist()) |
| 304 | + |
| 305 | + def test_primitives_passthrough(self): |
| 306 | + assert SAFE_SERIALIZE("hello") == "hello" |
| 307 | + assert SAFE_SERIALIZE(123) == 123 |
| 308 | + assert SAFE_SERIALIZE(3.14) == 3.14 |
| 309 | + assert SAFE_SERIALIZE(True) is True |
| 310 | + |
| 311 | + def test_unsupported_type_raises(self): |
| 312 | + class Foo: |
| 313 | + pass |
| 314 | + |
| 315 | + with pytest.raises(NotImplementedError): |
| 316 | + SAFE_SERIALIZE(Foo()) |
0 commit comments