diff --git a/app/api/utility.py b/app/api/utility.py index 6636325..b7f5cd5 100644 --- a/app/api/utility.py +++ b/app/api/utility.py @@ -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: @@ -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: diff --git a/tests/test_utility.py b/tests/test_utility.py index d120f61..90840f9 100644 --- a/tests/test_utility.py +++ b/tests/test_utility.py @@ -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): @@ -190,6 +189,7 @@ def test_validate_query_node_url_list( "https://firstnode.neurobagel.org/query/": "firstnode", }, ), + ({}, {}), ], ) def test_schema_invalid_nodes_raise_warning( @@ -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") == {}