Skip to content
Open
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
4 changes: 3 additions & 1 deletion data_resource_api/app/utils/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ def _create_descriptor(self, schema_dir: str, file_name: str):
try:
descriptor_dict = json.load(fh)
except ValueError:
logger.info(
raise RuntimeError(
f"Failed to load JSON file. JSON is probably invalid in file '{os.path.join(schema_dir, file_name)}'"
)

except RuntimeError:
raise
except Exception:
raise RuntimeError(f"Error opening schema {file_name}")

Expand Down
60 changes: 48 additions & 12 deletions tests/descriptor_util_classes/test_descriptor_from_file.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
from tests.schemas import frameworks_descriptor
import json
import os
import shutil

import pytest
from data_resource_api.app.utils.descriptor import DescriptorFromFile
from expects import equal, expect
from data_resource_api.app.utils.descriptor import (
DescriptorFromFile,
DescriptorsFromDirectory,
)

IO_TEST_FILES = os.path.join(os.path.dirname(__file__), "..", "io_test_files")
INVALID_JSON = os.path.join(IO_TEST_FILES, "invalid_json.json")

@pytest.mark.skip
def test_check_if_the_file_is_a_directory(self):
pass

def test_raises_on_invalid_json():
"""DescriptorFromFile must raise RuntimeError for invalid JSON, not crash with NameError."""
schema_dir = os.path.dirname(INVALID_JSON)
file_name = os.path.basename(INVALID_JSON)
with pytest.raises(RuntimeError, match="JSON is probably invalid"):
DescriptorFromFile(schema_dir, file_name)

@pytest.mark.skip
def test_test_get_only_json_files(self):
pass

def test_raises_on_directory_instead_of_file(tmp_path):
"""DescriptorFromFile must raise RuntimeError when given a directory path."""
subdir = tmp_path / "subdir"
subdir.mkdir()
with pytest.raises(RuntimeError, match="Cannot open a directory"):
DescriptorFromFile(str(tmp_path), "subdir")

@pytest.mark.skip
def test_returns_correctly(self):
pass

def test_skips_invalid_json_and_continues(tmp_path):
"""DescriptorsFromDirectory must skip invalid JSON files and keep loading valid ones."""
shutil.copy(INVALID_JSON, tmp_path / "invalid_json.json")

valid_descriptor = {
"api": {
"resource": "frameworks",
"methods": [{"get": {"enabled": True, "secured": False, "grants": []}}],
},
"datastore": {
"tablename": "frameworks",
"restricted_fields": [],
"schema": {
"fields": [
{"name": "id", "type": "integer", "title": "id", "required": False}
],
"primaryKey": "id",
},
},
}
with open(tmp_path / "valid_descriptor.json", "w") as f:
json.dump(valid_descriptor, f)

descriptors = list(DescriptorsFromDirectory([str(tmp_path)]).iter_files())
assert len(descriptors) == 1
assert descriptors[0].table_name == "frameworks"