Skip to content

Commit

Permalink
Merge pull request #130 from DataONEorg/feature-125-improve-init
Browse files Browse the repository at this point in the history
Feature-125: Improve HashStore Init
  • Loading branch information
doulikecookiedough authored Sep 10, 2024
2 parents 00c9dcd + 78e8af7 commit 641cf7d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/hashstore/filehashstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ def _validate_properties(self, properties):
logging.debug(exception_string)
raise ValueError(exception_string)

# New dictionary for validated properties
checked_properties = {}

for key in self.property_required_keys:
if key not in properties:
exception_string = (
Expand All @@ -400,14 +403,33 @@ def _validate_properties(self, properties):
)
logging.debug(exception_string)
raise KeyError(exception_string)
if properties.get(key) is None:

value = properties.get(key)
if value is None:
exception_string = (
"FileHashStore - _validate_properties: Value for key:"
+ f" {key} is none."
)
logging.debug(exception_string)
raise ValueError(exception_string)
return properties

# Add key and values to checked_properties
if key == "store_depth" or key == "store_width":
# Ensure store depth and width are integers
try:
checked_properties[key] = int(value)
except Exception as err:
exception_string = (
"FileHashStore - _validate_properties: Unexpected exception when"
" attempting to ensure store depth and width are integers. Details: "
+ str(err)
)
logging.debug(exception_string)
raise ValueError(exception_string)
else:
checked_properties[key] = value

return checked_properties

def _set_default_algorithms(self):
"""Set the default algorithms to calculate when storing objects."""
Expand Down
31 changes: 31 additions & 0 deletions tests/test_hashstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,34 @@ def test_factory_get_hashstore_filehashstore_nonconflicting_dir(factory, tmp_pat
}

factory.get_hashstore(module_name, class_name, properties)


def test_factory_get_hashstore_filehashstore_string_int_prop(factory, tmp_path):
"""Check factory does not raise exception when an integer is passed as a string in a
properties object."""
module_name = "hashstore.filehashstore"
class_name = "FileHashStore"

directory = tmp_path / "douhs" / "inttest"
directory.mkdir(parents=True)
douhspath = (tmp_path / "douhs").as_posix()

properties = {
"store_path": douhspath,
"store_depth": "3",
"store_width": "2",
"store_algorithm": "SHA-256",
"store_metadata_namespace": "https://ns.dataone.org/service/types/v2.0#SystemMetadata",
}

factory.get_hashstore(module_name, class_name, properties)

properties = {
"store_path": douhspath,
"store_depth": str(3),
"store_width": str(2),
"store_algorithm": "SHA-256",
"store_metadata_namespace": "https://ns.dataone.org/service/types/v2.0#SystemMetadata",
}

factory.get_hashstore(module_name, class_name, properties)

0 comments on commit 641cf7d

Please sign in to comment.