Skip to content

Commit

Permalink
Update constraints to also allow python dictionaries to be passed to …
Browse files Browse the repository at this point in the history
…catalog_add (and catalog_create by extension) and catalog_validate; add relevant tests
  • Loading branch information
jsheunis committed Dec 11, 2023
1 parent 40a722d commit 4afb12c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions datalad_catalog/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
WithDescription,
)

from datalad_next.constraints.basic import (
EnsureDType,
)

__docformat__ = "restructuredtext"


Expand All @@ -31,6 +35,10 @@
# - a JSON serialized string
metadata_constraint = WithDescription(
AnyOf(
WithDescription(
EnsureDType(dict),
error_message="not a valid Python dictionary",
),
WithDescription(
EnsureJSON(),
error_message="not valid JSON content",
Expand Down
16 changes: 16 additions & 0 deletions datalad_catalog/tests/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,19 @@ def test_add_from_json_str(demo_catalog, test_data):
status="ok",
path=demo_catalog.location,
)

def test_add_from_dict(demo_catalog):
"""Add catalog metadata from a json serialized string"""
mdata = {"dataset_id": "deabeb9b-7a37-4062-a1e0-8fcef7909609", "dataset_version": "0321dbde969d2f5d6b533e35b5c5c51ac0b15758", "type": "dataset", "metadata_sources": {"key_source_map": {}, "sources": [{"source_name": "", "source_version": ""}]}}
res = catalog_add(
catalog=demo_catalog,
metadata=mdata,
on_failure="ignore",
return_type="list",
)
assert_in_results(
res,
action="catalog_add",
status="ok",
path=demo_catalog.location,
)
15 changes: 15 additions & 0 deletions datalad_catalog/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@ def test_validate_without_catalog(demo_catalog, test_data):
status="error",
path=Path.cwd(),
)

def test_validate_metadata_dict(demo_catalog):
mdata = {"dataset_id": "deabeb9b-7a37-4062-a1e0-8fcef7909609", "dataset_version": "0321dbde969d2f5d6b533e35b5c5c51ac0b15758", "type": "dataset", "metadata_sources": {"key_source_map": {}, "sources": [{"source_name": "", "source_version": ""}]}}
res = catalog_validate(
catalog=demo_catalog,
metadata=mdata,
on_failure="ignore",
return_type="list",
)
assert_in_results(
res,
action="catalog_validate",
status="ok",
path=demo_catalog.location,
)

0 comments on commit 4afb12c

Please sign in to comment.