Skip to content

Commit

Permalink
Merge pull request #31 from DiamondLightSource/empty-list-parameters
Browse files Browse the repository at this point in the history
Correctly handle parameters with empty lists as values
  • Loading branch information
GDYendell authored Aug 20, 2024
2 parents c213f25 + 7467e1e commit 17f44ea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/odin_fastcs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ def _walk_odin_metadata(
for node_name, node_value in tree.items():
node_path = path + [node_name]

# Branches - dict or list[dict] to recurse through
if isinstance(node_value, dict) and not is_metadata_object(node_value):
yield from _walk_odin_metadata(node_value, node_path)
elif isinstance(node_value, list) and all(
isinstance(m, dict) for m in node_value
elif (
isinstance(node_value, list)
and node_value # Exclude parameters with an empty list as a value
and all(isinstance(m, dict) for m in node_value)
):
for idx, sub_node in enumerate(node_value):
sub_node_path = node_path + [str(idx)]
Expand Down
2 changes: 1 addition & 1 deletion tests/input/one_node_fp_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@
}
}
}
}
}
30 changes: 28 additions & 2 deletions tests/test_introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def test_one_node():
response = json.loads(f.read())

parameters = create_odin_parameters(response)
assert len(parameters) == 96
assert len(parameters) == 97


def test_two_node():
with (HERE / "input/two_node_fp_response.json").open() as f:
response = json.loads(f.read())

parameters = create_odin_parameters(response)
assert len(parameters) == 188
assert len(parameters) == 190


@pytest.mark.asyncio
Expand All @@ -41,3 +41,29 @@ async def get_plugins(idx: int):
controller = FrameProcessorAdapterController(mock_connection, parameters, "prefix")
await controller.initialise()
assert all(fpx in controller.get_sub_controllers() for fpx in ("FP0", "FP1"))


def test_node_with_empty_list_is_correctly_counted():
parameters = create_odin_parameters({"test": []})
names = [p.name for p in parameters]
assert "test" in names
assert len(parameters) == 1


def test_node_that_has_metadata_only_counts_once():
data = {"count": {"value": 1, "writeable": False, "type": "int"}}
parameters = create_odin_parameters(data)
assert len(parameters) == 1


def test_nested_node_gives_correct_name():
data = {"top": {"nest-1": {"nest-2": 1}}}
parameters = create_odin_parameters(data)
assert len(parameters) == 1
assert parameters[0].name == "top_nest-1_nest-2"


def test_config_node_splits_list_into_mutiples():
data = {"config": {"param": [1, 2]}}
parameters = create_odin_parameters(data)
assert len(parameters) == 2

0 comments on commit 17f44ea

Please sign in to comment.