Skip to content

Commit

Permalink
fix circular import error with loaded datalad-next
Browse files Browse the repository at this point in the history
This commit fixes a circular import error that
occurs when datalad-next is loaded and tests are
patched. The error is described in
<datalad/datalad-next#716>

The reason for the import error is that importing
datalad_catalog.add from the module-level context
in datalad_catalog.tests.test_add, will import
datalad_next, which will in turn import certain
tests, which will trigger the extension-api
generation process, which tries to process
datalad_catalog.add, which is not yet
completely loaded.
  • Loading branch information
christian-monch committed May 28, 2024
1 parent 76bb8d9 commit 2b56754
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 2 additions & 3 deletions datalad_catalog/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
eval_results,
get_status_dict,
)
from datalad_next.constraints import (
EnsurePath,
)
from datalad_next.exceptions import CapturedException

import json
Expand All @@ -44,6 +41,8 @@ class AddParameterValidator(EnsureCommandParameterization):
""""""

def __init__(self):
from datalad_next.constraints import EnsurePath

super().__init__(
param_constraints=dict(
catalog=CatalogRequired() & EnsureWebCatalog(),
Expand Down
21 changes: 18 additions & 3 deletions datalad_catalog/tests/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
assert_in_results,
assert_result_count,
)
from datalad_catalog.add import Add

import io
import json
Expand All @@ -11,11 +10,12 @@

from datalad_next.constraints.exceptions import CommandParametrizationError

catalog_add = Add()


def test_no_args(demo_catalog, test_data):
from datalad_catalog.add import Add

"""Both the catalog and metadata arguments are required"""
catalog_add = Add()
with pytest.raises(CommandParametrizationError):
catalog_add()

Expand All @@ -28,6 +28,9 @@ def test_no_args(demo_catalog, test_data):

def test_add_from_file(demo_catalog, test_data):
"""Add catalog metadata from a file with json line(s)"""
from datalad_catalog.add import Add

catalog_add = Add()
res = catalog_add(
catalog=demo_catalog,
metadata=test_data.catalog_metadata_dataset1,
Expand Down Expand Up @@ -57,6 +60,9 @@ def test_add_from_file(demo_catalog, test_data):
def test_add_from_file_faulty(demo_catalog, test_data):
"""Add catalog metadata from a file with json lines
where at least one line is not valid json"""
from datalad_catalog.add import Add

catalog_add = Add()
res = catalog_add(
catalog=demo_catalog,
metadata=test_data.catalog_metadata_valid_invalid,
Expand All @@ -80,10 +86,13 @@ def test_add_from_file_faulty(demo_catalog, test_data):

def test_add_from_stdin(monkeypatch, demo_catalog):
"""Add catalog metadata from stdin"""
from datalad_catalog.add import Add

mdata1 = '{"dataset_id": "deabeb9b-7a37-4062-a1e0-8fcef7909609", "dataset_version": "0321dbde969d2f5d6b533e35b5c5c51ac0b15758", "type": "dataset", "metadata_sources": {"key_source_map": {}, "sources": [{"source_name": "", "source_version": ""}]}}'
mdata2 = '{"dataset_id": "3344ffv5-7a37-4062-a1e0-8fcef7909609", "dataset_version": "8888dbde969d2f5d6b533e35b5c5c51ac0b15758", "type": "dataset", "metadata_sources": {"key_source_map": {}, "sources": [{"source_name": "", "source_version": ""}]}}'
content = io.StringIO(json.dumps(mdata1) + "\n" + json.dumps(mdata2))
monkeypatch.setattr("sys.stdin", content)
catalog_add = Add()
res = catalog_add(
catalog=demo_catalog,
metadata="-",
Expand All @@ -101,7 +110,10 @@ def test_add_from_stdin(monkeypatch, demo_catalog):

def test_add_from_json_str(demo_catalog, test_data):
"""Add catalog metadata from a json serialized string"""
from datalad_catalog.add import Add

mdata = '{"dataset_id": "deabeb9b-7a37-4062-a1e0-8fcef7909609", "dataset_version": "0321dbde969d2f5d6b533e35b5c5c51ac0b15758", "type": "dataset", "metadata_sources": {"key_source_map": {}, "sources": [{"source_name": "", "source_version": ""}]}}'
catalog_add = Add()
res = catalog_add(
catalog=demo_catalog,
metadata=mdata,
Expand All @@ -118,6 +130,8 @@ def test_add_from_json_str(demo_catalog, test_data):

def test_add_from_dict(demo_catalog):
"""Add catalog metadata from a json serialized string"""
from datalad_catalog.add import Add

mdata = {
"dataset_id": "deabeb9b-7a37-4062-a1e0-8fcef7909609",
"dataset_version": "0321dbde969d2f5d6b533e35b5c5c51ac0b15758",
Expand All @@ -127,6 +141,7 @@ def test_add_from_dict(demo_catalog):
"sources": [{"source_name": "", "source_version": ""}],
},
}
catalog_add = Add()
res = catalog_add(
catalog=demo_catalog,
metadata=mdata,
Expand Down

0 comments on commit 2b56754

Please sign in to comment.