Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle parameters with empty lists as values #31

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading