diff --git a/tests/json/test_load_from_json.py b/tests/json/test_load_from_json.py index 8142b2baf..7182d52a8 100644 --- a/tests/json/test_load_from_json.py +++ b/tests/json/test_load_from_json.py @@ -316,18 +316,76 @@ def contains_warning_of_type( return any(isinstance(json_warning, warning_type) for json_warning in json_warnings) +@pytest.mark.parametrize( + "depends_on_path", + [ + "/entry/instrument/test_component/transformations/location", + "transformations/location", + "location" + ], +) def test_GIVEN_json_with_component_depending_on_non_existent_transform_WHEN_loaded_THEN_warning_is_added( - json_dict_with_component, json_reader + json_dict_with_component, json_reader, depends_on_path ): - depends_on_dataset_str = """ - { + depends_on_dataset_str = f""" + {{ "module":"dataset", "attributes":[], - "config":{ + "config":{{ "type":"string", - "values": "/entry/instrument/test_component/transformations/location", + "values": "{depends_on_path}", "name":"depends_on" - } + }} + }} + """ + depends_on_dataset = json.loads(depends_on_dataset_str) + + # Add depends_on dataset which points to a transformation which does not exist in the JSON + json_dict_with_component["children"][0]["children"][0]["children"][0][ + "children" + ].append(depends_on_dataset) + json_reader._load_from_json_dict(json_dict_with_component) + + assert contains_warning_of_type(json_reader.warnings, TransformDependencyMissing) + + +def test_GIVEN_json_with_attribute_depends_on_WHEN_loaded_THEN_warning_is_added( + json_dict_with_component, json_reader +): + depends_on_dataset_str = """ + { + "module": "dataset", + "config": { + "name": "translation2", + "type": "double", + "values": -157.405 + }, + "attributes": [ + { + "dtype": "string", + "name": "transformation_type", + "values": "translation" + }, + { + "dtype": "string", + "name": "units", + "values": "m" + }, + { + "dtype": "string", + "name": "vector", + "values": [ + 0.0, + 0.0, + 1.0 + ] + }, + { + "dtype": "string", + "name": "depends_on", + "values": "translation1" + } + ] } """ depends_on_dataset = json.loads(depends_on_dataset_str) @@ -344,14 +402,47 @@ def test_GIVEN_json_with_component_depending_on_non_existent_transform_WHEN_load @pytest.mark.parametrize( "depends_on_path", [ - "/entry/instrument/componentname/transformations/slit0", - "transformations/slit0", - "slit0", ".", "", None ], ) +def test_GIVEN_json_with_component_with_null_depends_on_WHEN_loaded_THEN_no_effect( + json_dict_with_component, json_reader, depends_on_path +): + # Add depends_on dataset which points to a transformation in the JSON + depends_on_dataset_str = f""" + {{ + "module":"dataset", + "attributes":[], + "config":{{ + "type":"string", + "values": "{depends_on_path}", + "name":"depends_on" + }} + }} + """ + depends_on_dataset = json.loads(depends_on_dataset_str) + json_dict_with_component["children"][0]["children"][0]["children"][0][ + "children" + ].append(depends_on_dataset) + json_reader._load_from_json_dict(json_dict_with_component) + success = False + try: + json_reader._components_depends_on["componentname"][1].component_name + except AttributeError: + success = True + assert success + + +@pytest.mark.parametrize( + "depends_on_path", + [ + "/entry/instrument/componentname/transformations/slit0", + "transformations/slit0", + "slit0", + ], +) def test_GIVEN_json_with_component_depending_on_relative_transform_WHEN_loaded_THEN_model_updated( json_dict_with_component, json_reader, depends_on_path ): @@ -382,14 +473,13 @@ def test_GIVEN_json_with_component_depending_on_relative_transform_WHEN_loaded_T json_reader._components_depends_on["componentname"][1].component_name except AttributeError: assert True - else: - assert ( - json_reader._components_depends_on["componentname"][1].component_name - == "componentname" - ) - assert ( - json_reader._components_depends_on["componentname"][1].transform_name == transform_name - ) + assert ( + json_reader._components_depends_on["componentname"][1].component_name + == "componentname" + ) + assert ( + json_reader._components_depends_on["componentname"][1].transform_name == transform_name + ) def test_when_experiment_id_in_json_then_it_is_added_to_entry(json_reader):