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

U/jrbogart/allowing duplicate relpath #161

Merged
merged 3 commits into from
Oct 27, 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
49 changes: 33 additions & 16 deletions src/dataregistry/registrar/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
_bump_version,
_copy_data,
_form_dataset_path,
_name_from_relpath,
_parse_version_string,
_read_configuration_file,
get_directory_info,
Expand Down Expand Up @@ -68,7 +67,7 @@ def _validate_register_inputs(
if name is None or version is None:
raise ValueError("A valid `name` and `version` are required")
for att in [name, version]:
if type(att) != str:
if not isinstance(att, str):
raise ValueError(f"{att} is not a valid string")

# Make sure `name` is legal (i.e., no illegal characters)
Expand Down Expand Up @@ -99,7 +98,7 @@ def _validate_register_inputs(

# Make sure owner type is valid
if kwargs_dict["owner_type"] not in self._OWNER_TYPES:
raise ValueError(f"{owner_type} is not a valid owner_type")
raise ValueError(f"{kwargs_dict['owner_type']} is not a valid owner_type")

# Checks for production datasets
if kwargs_dict["owner_type"] == "production":
Expand Down Expand Up @@ -414,6 +413,7 @@ def register(

# Make sure the relative_path in the `root_dir` is avaliable
if kwargs_dict["location_type"] in ["dataregistry", "dummy"]:
will_copy = kwargs_dict["old_location"]
previous_datasets = self._find_previous(
None,
None,
Expand All @@ -433,17 +433,34 @@ def register(
root_dir=self._root_dir,
)

warned = False
if get_dataset_status(previous_datasets[-1].status, "archived"):
raise ValueError(
f"Relative path {dest} is reserved "
f"for archived datasetid={previous_datasets[-1].dataset_id}"
)
if will_copy:
raise ValueError(
f"Relative path {dest} is reserved "
f"for archived datasetid={previous_datasets[-1].dataset_id}"
)
else:
warnings.warn(
"Warning: found existing entry with path "
f"{kwargs_dict['relative_path']}",
UserWarning,
)
warned = True

if not get_dataset_status(previous_datasets[-1].status, "deleted"):
raise ValueError(
f"Relative path {dest} is taken by "
f"datasetid={previous_datasets[-1].dataset_id}"
)
if will_copy:
raise ValueError(
"Relative path {dest} is taken by "
f"datasetid={previous_datasets[-1].dataset_id}"
)
else:
if not warned:
warnings.warn(
"Warning: found existing entry with path "
f"{kwargs_dict['relative_path']}",
UserWarning,
)

# Make sure there is not already a database entry with this
# name/version combination
Expand Down Expand Up @@ -548,7 +565,7 @@ def replace(
raise ValueError(f"Dataset {full_name} does not exist")

# Cannot replace (valid) non-overwritable datasets
if previous_datasets[-1].is_overwritable == False and get_dataset_status(
if previous_datasets[-1].is_overwritable is False and get_dataset_status(
previous_datasets[-1].status, "valid"
):
raise ValueError(
Expand Down Expand Up @@ -842,7 +859,7 @@ def _validate_keywords(self, keywords):

for k in keywords:
# Make sure keyword is a string
if type(k) != str:
if not isinstance(k, str):
raise ValueError(f"{k} is not a valid keyword string")

# Make sure keywords are all in the keywords table
Expand All @@ -862,7 +879,7 @@ def _validate_keywords(self, keywords):

# Keyword not found
if len(keyword_ids) != len(keywords):
raise ValueError(f"Not all keywords selected are registered")
raise ValueError("Not all keywords selected are registered")

return keyword_ids

Expand All @@ -881,11 +898,11 @@ def add_keywords(self, dataset_id, keywords):
"""

# Make sure things are valid
if type(keywords) != list:
if not isinstance(keywords, list):
raise ValueError("Passed keywords object must be a list")

for k in keywords:
if type(k) != str:
if not isinstance(k, str):
raise ValueError(f"Keyword {k} is not a valid string")

if len(keywords) == 0:
Expand Down
8 changes: 6 additions & 2 deletions tests/end_to_end_tests/test_register_dataset_real_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_copy_data(dummy_file, data_org):
[
("file", "file1.txt"),
("directory", "dummy_dir"),
("same_directory", "dummy_dir")
],
)
def test_on_location_data(dummy_file, data_org, data_path):
Expand Down Expand Up @@ -84,9 +85,12 @@ def test_on_location_data(dummy_file, data_org, data_path):
[f],
)

assert len(results["dataset.data_org"]) == 1
if data_org == "same_directory":
assert len(results["dataset.data_org"]) == 2
else:
assert len(results["dataset.data_org"]) == 1

assert results["dataset.data_org"][0] == data_org
assert data_org.endswith(results["dataset.data_org"][0])
assert results["dataset.nfiles"][0] == 1
assert results["dataset.total_disk_space"][0] > 0

Expand Down
Loading