Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature-125: Improve HashStore Init #130

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading