Skip to content

Commit

Permalink
Resolve todo item in '_untag_object' for PidRefsDoesNotExist and add …
Browse files Browse the repository at this point in the history
…new pytests
  • Loading branch information
doulikecookiedough committed Sep 17, 2024
1 parent 0a15f88 commit 8403bf6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/hashstore/filehashstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -1820,8 +1820,23 @@ def _untag_object(self, pid, cid):
logging.warning(warn_msg)

except PidRefsDoesNotExist as prdne:
# TODO: Handle cid refs to ensure pid not found in it
return
# `find_object` throws this exception if the pid refs file is not found
# Check to see if pid is in the 'cid refs file' and attempt to remove it
self._check_object_locked_cids(cid)

# Remove pid from cid refs
cid_refs_path = self._get_hashstore_cid_refs_path(cid)
self._remove_pid_and_handle_cid_refs_deletion(
pid, untag_obj_delete_list, cid_refs_path
)
# Remove all files confirmed for deletion
self._delete_marked_files(untag_obj_delete_list)

warn_msg = (
f"Pid refs file not found, removed pid from cid refs file for cid: {cid}"
+ str(prdne)
)
logging.warning(warn_msg)

def _put_metadata(self, metadata, pid, metadata_doc_name):
"""Store contents of metadata to `[self.root]/metadata` using the hash of the
Expand Down
67 changes: 64 additions & 3 deletions tests/test_filehashstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ def test_untag_object_cid_not_locked(pids, store):
store._release_reference_locked_pids(pid)


def test_untag_object_orphan_pid_refs_file_found(pids, store):
def test_untag_object_orphan_pid_refs_file_found(store):
"""Test _untag_object removes an orphan pid refs file"""
test_dir = "tests/testdata/"
pid = "jtao.1700.1"
Expand All @@ -1575,7 +1575,7 @@ def test_untag_object_orphan_pid_refs_file_found(pids, store):
assert store._count("pid") == 0


def test_untag_object_orphan_refs_exist_but_data_object_not_found(pids, store):
def test_untag_object_orphan_refs_exist_but_data_object_not_found(store):
"""Test _untag_object removes orphaned pid and cid refs files"""
test_dir = "tests/testdata/"
pid = "jtao.1700.1"
Expand Down Expand Up @@ -1603,7 +1603,7 @@ def test_untag_object_orphan_refs_exist_but_data_object_not_found(pids, store):
assert store._count("cid") == 0


def test_untag_object_refs_found_but_pid_not_in_cid_refs(pids, store):
def test_untag_object_refs_found_but_pid_not_in_cid_refs(store):
"""Test _untag_object removes pid refs file whose pid is not found in the cid refs file."""
test_dir = "tests/testdata/"
pid = "jtao.1700.1"
Expand Down Expand Up @@ -1634,6 +1634,67 @@ def test_untag_object_refs_found_but_pid_not_in_cid_refs(pids, store):
assert store._count("cid") == 1


def test_untag_object_pid_refs_file_does_not_exist(store):
"""Test _untag_object removes pid from cid refs file since the pid refs file does not exist,
and does not delete the cid refs file because a reference is still present."""
test_dir = "tests/testdata/"
pid = "jtao.1700.1"
pid_two = pid + ".dou"
path = test_dir + pid
object_metadata = store.store_object(pid, path)
_object_metadata_two = store.store_object(pid_two, path)
cid = object_metadata.cid

assert store._count("pid") == 2
assert store._count("cid") == 1

# Remove pid from cid refs
pid_refs_file = store._get_hashstore_pid_refs_path(pid)
os.remove(pid_refs_file)

with pytest.raises(PidRefsDoesNotExist):
store._find_object(pid)

store._synchronize_referenced_locked_pids(pid)
store._synchronize_object_locked_cids(cid)
store._untag_object(pid, cid)
store._release_reference_locked_pids(pid)
store._release_object_locked_cids(cid)

assert store._count("pid") == 1
assert store._count("cid") == 1


def test_untag_object_pid_refs_file_does_not_exist_and_cid_refs_is_empty(store):
"""Test '_untag_object' removes pid from cid refs file since the pid refs file does not exist,
and deletes the cid refs file because it contains no more references (after the pid called
with '_untag_object' is removed from the cid refs)."""
test_dir = "tests/testdata/"
pid = "jtao.1700.1"
path = test_dir + pid
object_metadata = store.store_object(pid, path)
cid = object_metadata.cid

assert store._count("pid") == 1
assert store._count("cid") == 1

# Remove pid from cid refs
pid_refs_file = store._get_hashstore_pid_refs_path(pid)
os.remove(pid_refs_file)

with pytest.raises(PidRefsDoesNotExist):
store._find_object(pid)

store._synchronize_referenced_locked_pids(pid)
store._synchronize_object_locked_cids(cid)
store._untag_object(pid, cid)
store._release_reference_locked_pids(pid)
store._release_object_locked_cids(cid)

assert store._count("pid") == 0
assert store._count("cid") == 0


def test_create_path(pids, store):
"""Test makepath creates folder successfully."""
for pid in pids:
Expand Down

0 comments on commit 8403bf6

Please sign in to comment.