diff --git a/src/hashstore/filehashstore.py b/src/hashstore/filehashstore.py index 2be657c4..c72dfb8a 100644 --- a/src/hashstore/filehashstore.py +++ b/src/hashstore/filehashstore.py @@ -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 = ( @@ -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.""" diff --git a/tests/test_hashstore.py b/tests/test_hashstore.py index 140d473a..34ba6e15 100644 --- a/tests/test_hashstore.py +++ b/tests/test_hashstore.py @@ -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)