Skip to content

Commit

Permalink
Add avu get func and its integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claravox committed Oct 8, 2024
1 parent 9a59a96 commit c8b4775
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
27 changes: 27 additions & 0 deletions integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ def _test_avu_rmw_collection(ctx, rmw_attributes):
return result


def _test_avu_get_attr_val_of_coll(ctx, attr, value):
# Test getting the value of an attribute on a collection
tmp_coll = _create_tmp_collection(ctx)
ctx.msi_add_avu('-c', tmp_coll, attr, value, "baz")
result = avu.get_attr_val_of_coll(ctx, tmp_coll, attr)
collection.remove(ctx, tmp_coll)
return result


def _test_avu_get_attr_val_of_coll_exception(ctx):
# Test that getting a non existing attribute on a collection raises an exception (True for exception raised)
tmp_coll = _create_tmp_collection(ctx)
result = False
try:
result = avu.get_attr_val_of_coll(ctx, tmp_coll, "foo")
except Exception:
result = True
collection.remove(ctx, tmp_coll)
return result


def _test_folder_set_retry_avus(ctx):
tmp_coll = _create_tmp_collection(ctx)
folder.folder_secure_set_retry_avus(ctx, tmp_coll, 2)
Expand Down Expand Up @@ -482,6 +503,12 @@ def _test_folder_secure_func(ctx, func):
"check": lambda x: (("aap", "noot", "mies") in x
and len([a for a in x if a[0] not in ["org_replication_scheduled"]]) == 1
)},
{"name": "avu.get_attr_val_of_coll.exists.yes",
"test": lambda ctx: _test_avu_get_attr_val_of_coll(ctx, "foo", "bar"),
"check": lambda x: x == "bar"},
{"name": "avu.get_attr_val_of_coll.exists.no",
"test": lambda ctx: _test_avu_get_attr_val_of_coll_exception(ctx),
"check": lambda x: x},
{"name": "avu.apply_atomic_operations.collection",
"test": lambda ctx: _test_msvc_apply_atomic_operations_collection(ctx),
"check": lambda x: (("foo", "bar", "baz") in x and len(x) == 1)},
Expand Down
23 changes: 9 additions & 14 deletions troubleshoot_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,21 @@ def check_datacite_doi_registration(ctx, data_package, offline, write_stdout):
base_doi_check = False

try:
version_doi = get_attribute_value(ctx, data_package, "versionDOI")
version_doi = get_val_for_attr_with_pub_prefix(ctx, data_package, "versionDOI")
status_code = datacite.metadata_get(ctx, version_doi)
version_doi_check = status_code == 200
except ValueError as e:
log.write(ctx, "check_datacite_doi_registration: Error while trying to get versionDOI - {}".format(e), write_stdout)

previous_version = ''
try:
previous_version = get_attribute_value(ctx, data_package, "previous_version")
previous_version = get_val_for_attr_with_pub_prefix(ctx, data_package, "previous_version")
except Exception:
pass

if previous_version:
try:
base_doi = get_attribute_value(ctx, data_package, "baseDOI")
base_doi = get_val_for_attr_with_pub_prefix(ctx, data_package, "baseDOI")
status_code = datacite.metadata_get(ctx, base_doi)
base_doi_check = status_code == 200
except ValueError as e:
Expand All @@ -142,7 +142,7 @@ def check_datacite_doi_registration(ctx, data_package, offline, write_stdout):
return (version_doi_check, base_doi_check)


def get_attribute_value(ctx, data_package, attribute_suffix):
def get_val_for_attr_with_pub_prefix(ctx, data_package, attribute_suffix):
"""
Retrieves the value given the suffix of the attribute from a data package.
Expand All @@ -154,21 +154,16 @@ def get_attribute_value(ctx, data_package, attribute_suffix):
:raises ValueError: If the attribute is not found in the data package's AVU.
"""

# TODO extract to avu.py? need this?
attr = constants.UUORGMETADATAPREFIX + "publication_" + attribute_suffix
try:
return next(m.value for m in avu.of_coll(ctx, data_package) if m.attr == attr)
except Exception:
raise ValueError("get_attribute_value: Attribute {} not found in AVU".format(attr))
return avu.get_attr_val_of_coll(ctx, data_package, attr)


def get_landingpage_paths(ctx, data_package, write_stdout):
"""Given a data package get what the path and remote url should be"""
file_path = ''
try:
file_path = get_attribute_value(ctx, data_package, "landingPagePath")
url = get_attribute_value(ctx, data_package, "landingPageUrl")
file_path = get_val_for_attr_with_pub_prefix(ctx, data_package, "landingPagePath")
url = get_val_for_attr_with_pub_prefix(ctx, data_package, "landingPageUrl")
return file_path, url

except Exception:
Expand Down Expand Up @@ -259,7 +254,7 @@ def check_combi_json(ctx, data_package, publication_config, offline, write_stdou
# Check that the combi json in irods exists
file_path = ''
try:
file_path = get_attribute_value(ctx, data_package, "combiJsonPath")
file_path = get_val_for_attr_with_pub_prefix(ctx, data_package, "combiJsonPath")
except Exception:
pass
exists = data_object.exists(ctx, file_path)
Expand All @@ -275,7 +270,7 @@ def check_combi_json(ctx, data_package, publication_config, offline, write_stdou
# Get the version doi
version_doi = ''
try:
version_doi = get_attribute_value(ctx, data_package, "versionDOI")
version_doi = get_val_for_attr_with_pub_prefix(ctx, data_package, "versionDOI")
except Exception:
pass
url = "https://{}/oai/oai?verb=GetRecord&metadataPrefix=oai_datacite&identifier=oai:{}".format(publication_config["publicVHost"], version_doi)
Expand Down
12 changes: 12 additions & 0 deletions util/avu.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ def of_coll(ctx, coll):
"COLL_NAME = '{}'".format(coll)))


def get_attr_val_of_coll(ctx, coll, attr):
"""Get the value corresponding to an attr for a given collection."""
iter = genquery.Query(
ctx,
"META_COLL_ATTR_VALUE",
"META_COLL_ATTR_NAME = '{}' AND COLL_NAME = '{}'".format(attr, coll))

for row in iter:
return row
raise ValueError("Attribute {} not found in AVUs of collection {}".format(attr, coll))


def inside_coll(ctx, path, recursive=False):
"""Get a list of all AVUs inside a collection with corresponding paths.
Expand Down

0 comments on commit c8b4775

Please sign in to comment.