Skip to content

Commit 8cda1cc

Browse files
committed
add functionality and testing around addition of links when publishing a dataset
1 parent 294c001 commit 8cda1cc

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

foundry/foundry.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ def publish_dataset(
374374
dataset_doi (str): The DOI for this dataset (not an associated paper).
375375
related_dois (list): DOIs related to this dataset,
376376
not including the dataset's own DOI (for example, an associated paper's DOI).
377+
links (list[dict]): List of dicts describing links associated with the dataset; in the format:
378+
{"type":str, "doi":str, "url":str, "description":str, "bibtex":str}
377379
378380
Returns
379381
-------
@@ -404,6 +406,17 @@ def publish_dataset(
404406
self.connect_client.set_project_block(
405407
self.config.metadata_key, foundry_metadata)
406408

409+
# add links
410+
links = kwargs.get("links")
411+
if links is not None:
412+
# make sure it's a list
413+
links = links if isinstance(links, list) else [links]
414+
for link in links:
415+
# validate links
416+
if self.validate_link(link):
417+
# add valid links
418+
self.connect_client.add_links(link)
419+
407420
# upload via HTTPS if specified
408421
if https_data_path:
409422
# gather auth'd clients necessary for publication to endpoint
@@ -787,6 +800,15 @@ def validate_metadata(self, metadata):
787800
field_name = ".".join([item for item in error['loc'] if isinstance(item, str)])
788801
error_description = error['msg']
789802
error_message = f"""There is an issue validating the metadata for the field '{field_name}':
790-
The error message returned is: '{error_description}'."""
803+
The error message returned is: '{error_description}'."""
791804
logger.error(error_message)
792805
raise e
806+
807+
def validate_link(self, link):
808+
valid_keys = ["type", "doi", "url", "description", "bibtex"]
809+
link_keys = [*link]
810+
if not all(key in valid_keys for key in link_keys):
811+
raise ValueError(f"A key in one of the submitted link ({link_keys}) is not of the valid options: "
812+
f"{valid_keys}")
813+
else:
814+
return True

tests/test_foundry.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,62 @@ def test_publish_with_https():
366366
_write_test_data(local_path)
367367

368368
res = f.publish_dataset(pub_test_metadata, title, authors, https_data_path=local_path, short_name=short_name)
369+
assert res['success']
370+
assert res['source_id'] == f"_test_{short_name}_v1.1"
371+
372+
373+
@pytest.mark.skipif(bool(is_gha), reason="Not run as part of GHA CI")
374+
def test_publish_bad_links_with_https():
375+
"""System test: Assess the end-to-end publication of a dataset via HTTPS
376+
"""
377+
378+
f = Foundry(index="mdf-test", authorizers=auths)
379+
timestamp = datetime.now().timestamp()
380+
title = "https_publish_test_{:.0f}".format(timestamp)
381+
short_name = "https_pub_{:.0f}".format(timestamp)
382+
authors = ["A Scourtas"]
383+
local_path = "./data/https_test"
384+
links = {"horse": "link", "doi": "3", "url": "www.test.com", "description": "string", "bibtex": "bib"}
385+
386+
# create test JSON to upload (if it doesn't already exist)
387+
_write_test_data(local_path)
388+
389+
with pytest.raises(Exception) as exc_info:
390+
f.publish_dataset(pub_test_metadata,
391+
title,
392+
authors,
393+
https_data_path=local_path,
394+
short_name=short_name,
395+
links=links)
396+
# err = exc_info.value
397+
# assert hasattr(err, '__cause__')
398+
# assert isinstance(err.__cause__, ValueError)
399+
assert isinstance(exc_info.type(), ValueError)
400+
_delete_test_data(f)
401+
402+
403+
@pytest.mark.skipif(bool(is_gha), reason="Not run as part of GHA CI")
404+
def test_publish_links_with_https():
405+
"""System test: Assess the end-to-end publication of a dataset via HTTPS
406+
"""
407+
408+
f = Foundry(index="mdf-test", authorizers=auths)
409+
timestamp = datetime.now().timestamp()
410+
title = "https_publish_test_{:.0f}".format(timestamp)
411+
short_name = "https_pub_{:.0f}".format(timestamp)
412+
authors = ["A Scourtas"]
413+
local_path = "./data/https_test"
414+
links = {"type": "link", "doi": "3", "url": "www.test.com", "description": "string", "bibtex": "bib"}
415+
416+
# create test JSON to upload (if it doesn't already exist)
417+
_write_test_data(local_path)
418+
419+
res = f.publish_dataset(pub_test_metadata,
420+
title,
421+
authors,
422+
https_data_path=local_path,
423+
short_name=short_name,
424+
links=links)
369425

370426
assert res['success']
371427
assert res['source_id'] == f"_test_{short_name}_v1.1"

0 commit comments

Comments
 (0)