Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zimeon committed Nov 21, 2024
1 parent ae2742a commit 77b11c5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 5 deletions.
21 changes: 21 additions & 0 deletions ocfl/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ def find_logical_path(self, logical_path):
of the object. In the case that the logical file path doesn't
exist in any version then (None, None) will be returned.
The method searchs backward from the latest version through to the
first version. The latest version that the logical path exists in
will be return, not any possible earlier version which might correspond
with different content.
Example:
>>> import ocfl
>>> obj = ocfl.Object(path="fixtures/1.1/good-objects/spec-ex-full")
Expand Down Expand Up @@ -745,6 +750,13 @@ def delete_logical_path(self, path):
Will remove the logical path, and possibly the state entry for its
digest if there was only one logical path for the given digest.
If this is the last reference to a given digest in the current version
state then delete any content files in this version for the given
digest. (There is the possibility of odd behavior if dedupe is set
False so that multiple copies are created within the version -- all
copies will be left until the last reference is deleted, then all
copies will be deleted.)
Arguments:
path: path within the state for this version
Expand All @@ -763,5 +775,14 @@ def delete_logical_path(self, path):
else:
# Just this path, remove digest from state
del self.state[digest]
# No more references from this version, delete manifest
# entries in this verion
if digest in self.inv.manifest:
cpaths = []
for cpath in self.inv.manifest[digest]:
if not cpath.startswith(self.vdir + "/"):
cpaths.append(cpath)
self.inv.manifest[digest] = cpaths
# FIXME - Also adjust fixity!
return digest
raise InventoryException("Logical path to delete %s not found!" % (path))
16 changes: 11 additions & 5 deletions ocfl/new_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NewVersion():
def __init__(self, *,
inventory=None,
objdir=None,
srcdir=None,
srcdir=".",
metadata=None,
digest_algorithm=None,
content_directory=None,
Expand Down Expand Up @@ -176,6 +176,16 @@ def add(self, src_path, logical_path, content_path=None):
creation
logical_path (str): logical filepath that this content should
have within the version of the object
content_path (str or None): if None (default) then will generate a
content path based on the src_path and the
content_path_normalization strategy selected. Otherwise will
use the speficied content path provided is in the current
versions content directory (must start with
"vdir/content_directory/") and doesn't already exist in the
object
Raises:
NewVersionException: if the specifies content path is not allowed
"""
inventory = self.inventory
if content_path is None:
Expand Down Expand Up @@ -219,10 +229,6 @@ def delete(self, logical_path):
This is likely to be used when constructing a new version starting from
the previous state (initialization with carry_content_forward=True).
Assumes that the content is used in a previous version to will not
check to delete content from the manifest. Thus add() followed
but delete_content() could leave the new version in a bad state.
Arguments:
logical_path (str): logical path that should not appear in the new
version state
Expand Down
29 changes: 29 additions & 0 deletions tests/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,32 @@ def test_add_file(self):
self.assertEqual(inv.manifest["digest1"], ["v1/content/file1", "v2/content/file1_duped"])
# Error
self.assertRaises(InventoryException, v2.add_file, digest="digest5", logical_path="file1_deduped")

def test_find_logical_path(self):
"""Test find_local_path method."""
inv = Inventory()
inv.manifest = {"abc123": ["v1/content/file1", "v2/content/file2"],
"def456": ["v2/content/file3"]}
v1 = inv.add_version(state={"abc123": ["file1_added_v1"]})
v2 = inv.add_version(state={"abc123": ["file1_added_v1_moved", "file2_added_v2"],
"def456": ["file3_added_v2"]})
self.assertEqual(inv.find_logical_path("not there"), (None, None))
self.assertEqual(inv.find_logical_path("file1_added_v1_moved"), ("v2", "v1/content/file1"))
self.assertEqual(inv.find_logical_path("file3_added_v2"), ("v2", "v2/content/file3"))

def test_delete_logical_path(self):
"""Test delete_logical_path method."""
inv = Inventory()
inv.manifest = {"d1": ["v1/content/file1", "v2/content/file3"],
"d2": ["v1/content/file2"]}
v1 = inv.add_version(state={"d1": ["file1"],
"d2": ["file2"]})
v2 = inv.add_version(state={"d1": ["file1_moved", "file3"],
"d2": ["file2"]})
self.assertRaises(InventoryException, inv.current_version.delete_logical_path, "file1")
self.assertEqual(inv.current_version.delete_logical_path("file1_moved"), "d1")
self.assertEqual(inv.manifest["d1"], ["v1/content/file1", "v2/content/file3"])
self.assertEqual(inv.current_version.delete_logical_path("file3"), "d1")
self.assertEqual(inv.manifest["d1"], ["v1/content/file1"])
self.assertEqual(inv.current_version.delete_logical_path("file2"), "d2")
self.assertEqual(inv.manifest["d2"], ["v1/content/file2"])
44 changes: 44 additions & 0 deletions tests/test_new_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""NewVersion tests."""
import unittest

from ocfl.inventory import Inventory
from ocfl.new_version import NewVersion, NewVersionException
from ocfl.pyfs import pyfs_openfs

Expand All @@ -12,3 +13,46 @@ def test_init(self):
"""Test initialization."""
nv = NewVersion(objdir="", srcdir="")
self.assertEqual(nv.inventory.head, "v1")

def test__map_filepath(self):
"""Test _map_filepath method."""
nv = NewVersion()
# default is uri
nv.head = "v1"
self.assertEqual(nv._map_filepath("a"), "v1/content/a")
self.assertEqual(nv._map_filepath(".a?"), "v1/content/%2Ea%3F")
nv.inventory.manifest = {"digest1": ["v1/content/a"]}
self.assertEqual(nv._map_filepath("a"), "v1/content/a__2")
# md5
nv = NewVersion()
nv.head = "v1"
nv.content_path_normalization = "md5"
self.assertEqual(nv._map_filepath("a"), "v1/content/0cc175b9c0f1b6a8")
nv.inventory.manifest = {"digest1": ["v1/content/0cc175b9c0f1b6a8"]}
self.assertEqual(nv._map_filepath("a"), "v1/content/0cc175b9c0f1b6a8__2")
# error case
nv = NewVersion()
nv.head = "v1"
nv.content_path_normalization = "???"
self.assertRaises(NewVersionException, nv._map_filepath, "a")

def test_add(self):
"""Test add method."""
inv = Inventory(filepath="fixtures/1.1/good-objects/updates_three_versions_one_file/inventory.json")
nv = NewVersion(inventory=inv)
# Bad content paths
self.assertRaises(NewVersionException, nv.add, "src1", "log1", "vBAD/content/something")
self.assertRaises(NewVersionException, nv.add, "src1", "log1", "v4/BAD/something")
# Content path already exists
nv.add("fixtures/1.1/content/README.md", "log1", "v4/content/a_file.txt")
self.assertRaises(NewVersionException, nv.add, "src1", "log1", "v4/content/a_file.txt")
# Generate content path


def test_delete(self):
"""Test delete method."""
pass

def test_rename(self):
"""Test rename method."""
pass

0 comments on commit 77b11c5

Please sign in to comment.