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

[FIX] Handle empty local nodes file #67

Merged
merged 2 commits into from
Feb 1, 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
3 changes: 2 additions & 1 deletion app/api/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def parse_nodes_as_dict(path: Path) -> dict:
where the keys are the node URLs, and the values are the node names.
Makes sure node URLs end with a slash and only valid nodes are returned.
"""
valid_nodes = []

if path.exists() and path.stat().st_size > 0:
try:
with open(path, "r") as f:
Expand All @@ -75,7 +77,6 @@ def parse_nodes_as_dict(path: Path) -> dict:
validate(instance=input_nodes, schema=LOCAL_NODE_SCHEMA)
valid_nodes = input_nodes
except jsonschema.ValidationError:
valid_nodes = []
invalid_nodes = []
for node in input_nodes:
try:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def test_add_trailing_slash(url, expected_url):
"https://secondnode.neurobagel.org/query/": "secondnode",
},
),
({}, {}),
],
)
def test_parse_nodes_as_dict(set_nodes, expected_nodes, tmp_path):
Expand Down Expand Up @@ -190,6 +189,7 @@ def test_validate_query_node_url_list(
"https://firstnode.neurobagel.org/query/": "firstnode",
},
),
({}, {}),
],
)
def test_schema_invalid_nodes_raise_warning(
Expand Down Expand Up @@ -220,3 +220,12 @@ def test_invalid_json_raises_warning(tmp_path):

with pytest.warns(UserWarning, match="You provided an invalid JSON"):
util.parse_nodes_as_dict(tmp_path / "local_nb_nodes.json")


def test_empty_json_does_not_error(tmp_path):
"""Ensure that an empty JSON file does not raise an error."""

with open(tmp_path / "local_nb_nodes.json", "w") as f:
f.write("")

assert util.parse_nodes_as_dict(tmp_path / "local_nb_nodes.json") == {}