Skip to content

Commit

Permalink
update the remove function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Dec 3, 2024
1 parent 93ec3e5 commit 67065da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 17 additions & 3 deletions src/pybiocfilecache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,28 @@ def update(
raise BiocCacheError("Failed to update resource") from e

def remove(self, rname: str) -> None:
"""Remove a resource from the cache."""
"""Remove a resource from cache by name.
Args:
rname: Name of the resource to remove
Raises:
BiocCacheError: If resource removal fails
"""
with self.get_session() as session:
resource = self._get(session, rname, validate=False)
resource = session.query(Resource).filter(Resource.rname == rname).first()

if resource is not None:
try:
Path(resource.rpath).unlink(missing_ok=True)
# Try to remove the file first
rpath = Path(resource.rpath)
if rpath.exists():
rpath.unlink()

# Then remove from database
session.delete(resource)
session.commit()

except Exception as e:
session.rollback()
raise BiocCacheError(f"Failed to remove resource '{rname}'") from e
Expand Down
6 changes: 4 additions & 2 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil

from pybiocfilecache import BiocFileCache

Expand Down Expand Up @@ -35,10 +36,11 @@ def test_add_get_operations():
frec2 = open(rec2.rpath, "r").read().strip()
assert frec2 == "test2"

bfc.add("test3_asis", os.getcwd() + "/tests/data/test2.txt", action="asis")
shutil.copy(os.getcwd() + "/tests/data/test2.txt", os.getcwd() + "/tests/data/test3.txt")
bfc.add("test3_asis", os.getcwd() + "/tests/data/test3.txt", action="asis")
rec3 = bfc.get("test3_asis")
assert rec3 is not None
assert rec3.rpath == os.getcwd() + "/tests/data/test2.txt"
assert rec3.rpath == os.getcwd() + "/tests/data/test3.txt"

frec3 = open(rec3.rpath, "r").read().strip()
assert frec3 == "test2"
Expand Down

0 comments on commit 67065da

Please sign in to comment.