def _assert_raise_mke_with_keys(exception_keys: List[str], *args, **kwargs): try: meta_add(*args, **kwargs) raise RuntimeError("MetadataKeyException not raised") except MetadataKeyException as mke: eq_(mke.keys, exception_keys)
def test_object_parameter(): with \ patch("datalad_metalad.add.add_file_metadata") as fp, \ patch("datalad_metalad.add.add_dataset_metadata") as dp: meta_add( metadata={ **metadata_template, "type": "file", "path": "d1/d1.1./f1.1.1" }) assert_true(fp.call_count == 1) assert_true(dp.call_count == 0)
def test_add_file_end_to_end(file_name): test_path = "d_0/d_0.0/f_0.0.0" json.dump({ **metadata_template, "type": "file", "path": test_path }, open(file_name, "tw")) with tempfile.TemporaryDirectory() as temp_dir: git_repo = GitRepo(temp_dir) res = meta_add(metadata=file_name, metadata_store=git_repo.path) assert_result_count(res, 1) assert_result_count(res, 1, type='file') assert_result_count(res, 0, type='dataset') # Verify file level metadata was added tree_version_list, uuid_set, mrr = _get_top_nodes( git_repo, UUID(metadata_template["dataset_id"]), metadata_template["dataset_version"]) file_tree = mrr.get_file_tree() assert_is_not_none(file_tree) assert_true(test_path in file_tree) metadata = file_tree.get_metadata(MetadataPath(test_path)) metadata_content = _get_metadata_content(metadata) eq_(metadata_content, metadata_template["extracted_metadata"])
def test_add_dataset_end_to_end(file_name): json.dump({ **metadata_template, "type": "dataset" }, open(file_name, "tw")) with tempfile.TemporaryDirectory() as temp_dir: git_repo = GitRepo(temp_dir) res = meta_add(metadata=file_name, metadata_store=git_repo.path) assert_result_count(res, 1) assert_result_count(res, 1, type='dataset') assert_result_count(res, 0, type='file') # Verify dataset level metadata was added tree_version_list, uuid_set, mrr = _get_top_nodes( git_repo, UUID(metadata_template["dataset_id"]), metadata_template["dataset_version"]) metadata = mrr.get_dataset_level_metadata() metadata_content = _get_metadata_content(metadata) eq_(metadata_content, metadata_template["extracted_metadata"])
def test_unknown_key_allowed(file_name): json.dump({ **metadata_template, "type": "dataset", "strange_key_name": "some value" }, open(file_name, "tw")) with \ patch("datalad_metalad.add.add_file_metadata") as fp, \ patch("datalad_metalad.add.add_dataset_metadata") as dp: meta_add( metadata=file_name, allow_unknown=True) assert_true(fp.call_count == 0) assert_true(dp.call_count == 1)
def test_override_key_allowed(file_name): json.dump({ **metadata_template, "type": "dataset" }, open(file_name, "tw")) with \ patch("datalad_metalad.add.add_file_metadata") as fp, \ patch("datalad_metalad.add.add_dataset_metadata") as dp: meta_add( metadata=file_name, additionalvalues=json.dumps( {"dataset_id": "a1010203-1011-2021-3031-404142434445"}), allow_override=True) assert_true(fp.call_count == 0) assert_true(dp.call_count == 1)
def test_optional_keys(file_name): json.dump({ **metadata_template, "type": "file", "path": "d1/d1.1./f1.1.1" }, open(file_name, "tw")) with \ patch("datalad_metalad.add.add_file_metadata") as fp, \ patch("datalad_metalad.add.add_dataset_metadata") as dp: meta_add( metadata=file_name, allow_unknown=True) assert_true(fp.call_count == 1) assert_true(dp.call_count == 0)
def test_subdataset_add_file_end_to_end(file_name): test_path = "d_1/d_1.0/f_1.0.0" json.dump({ **metadata_template, **additional_keys_template, "type": "file", "path": test_path }, open(file_name, "tw")) with tempfile.TemporaryDirectory() as temp_dir: git_repo = GitRepo(temp_dir) res = meta_add(metadata=file_name, metadata_store=git_repo.path) assert_result_count(res, 1) assert_result_count(res, 1, type='file') assert_result_count(res, 0, type='dataset') # Verify dataset level metadata was added root_dataset_id = UUID(additional_keys_template["root_dataset_id"]) root_dataset_version = additional_keys_template["root_dataset_version"] dataset_tree_path = MetadataPath( additional_keys_template["dataset_path"]) tree_version_list, uuid_set, mrr = _get_top_nodes( git_repo, root_dataset_id, root_dataset_version) _, dataset_tree = tree_version_list.get_dataset_tree( root_dataset_version) mrr = dataset_tree.get_metadata_root_record(dataset_tree_path) eq_(mrr.dataset_identifier, UUID(metadata_template["dataset_id"])) file_tree = mrr.get_file_tree() assert_is_not_none(file_tree) assert_true(test_path in file_tree) metadata = file_tree.get_metadata(MetadataPath(test_path)) metadata_content = _get_metadata_content(metadata) eq_(metadata_content, metadata_template["extracted_metadata"])