From 4b3252dfb6fa024a52971061dbde04a9579e7d60 Mon Sep 17 00:00:00 2001 From: Stephan Heunis Date: Mon, 11 Dec 2023 15:22:26 +0100 Subject: [PATCH] Update constraints to also allow python dictionaries to be passed to catalog_add (and catalog_create by extension) and catalog_validate; add relevant tests --- datalad_catalog/constraints.py | 8 ++++++++ datalad_catalog/tests/test_add.py | 25 +++++++++++++++++++++++++ datalad_catalog/tests/test_validate.py | 24 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/datalad_catalog/constraints.py b/datalad_catalog/constraints.py index b5734b73..4f7d4678 100644 --- a/datalad_catalog/constraints.py +++ b/datalad_catalog/constraints.py @@ -22,6 +22,10 @@ WithDescription, ) +from datalad_next.constraints.basic import ( + EnsureDType, +) + __docformat__ = "restructuredtext" @@ -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", diff --git a/datalad_catalog/tests/test_add.py b/datalad_catalog/tests/test_add.py index e84aaa63..ae90d892 100644 --- a/datalad_catalog/tests/test_add.py +++ b/datalad_catalog/tests/test_add.py @@ -114,3 +114,28 @@ 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, + ) diff --git a/datalad_catalog/tests/test_validate.py b/datalad_catalog/tests/test_validate.py index 7e56e6bd..4dbe9de2 100644 --- a/datalad_catalog/tests/test_validate.py +++ b/datalad_catalog/tests/test_validate.py @@ -89,3 +89,27 @@ 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, + )