Skip to content

Commit

Permalink
[FIX] Handle empty local nodes file (#67)
Browse files Browse the repository at this point in the history
* fix handling of empty nodes JSON and add test

* move test case for empty dict to schema invalid test
  • Loading branch information
alyssadai authored Feb 1, 2024
1 parent 50be247 commit 9d685db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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") == {}

0 comments on commit 9d685db

Please sign in to comment.