"""U/V on sequence points: the optional field and the way it travels in a document.""" from __future__ import annotations import json from orisys_autocal.sequence_io import SequencePoint, from_document, to_document PROFILE = {"mode": "plane", "pitch_mm": 2.0, "repeats": 1} def _round_trip(points): document = to_document(profile=PROFILE, points=points) # Through JSON text, as a file would carry it. return from_document(json.loads(json.dumps(document)), current_profile=PROFILE) def test_uv_survives_a_round_trip(): point = SequencePoint( id=1, target_mm={"x": 1.0, "y": 2.0, "z": 3.0}, uv_px={"u": -130.0, "v": 470.5} ) back = _round_trip([point]).points[0] assert back.uv_px == {"u": -130.0, "v": 470.5} def test_a_point_without_uv_stays_without_it(): point = SequencePoint(id=1, target_mm={"x": 1.0, "y": 2.0, "z": 3.0}) document = to_document(profile=PROFILE, points=[point]) assert "uv_px" not in document["points"][0] assert _round_trip([point]).points[0].uv_px is None def test_an_older_file_with_no_uv_still_loads(): document = to_document( profile=PROFILE, points=[SequencePoint(id=1, target_mm={"x": 0.0, "y": 0.0, "z": 12.4})], ) assert from_document(document, current_profile=PROFILE).points[0].uv_px is None def test_a_half_written_uv_is_ignored_rather_than_half_used(): document = to_document( profile=PROFILE, points=[SequencePoint(id=1, target_mm={"x": 0.0, "y": 0.0, "z": 12.4})], ) document["points"][0]["uv_px"] = {"u": 5.0} assert from_document(document, current_profile=PROFILE).points[0].uv_px is None