From 8fbd175b0e5e077c9ca28bb48e7837039916946a Mon Sep 17 00:00:00 2001 From: Simeon Warner Date: Thu, 12 Dec 2024 13:46:57 -0500 Subject: [PATCH] Tidy layout_nnnn_uuid_quadtree --- ocfl/layout_nnnn_uuid_quadtree.py | 42 +++++++++++++++++++++---- tests/test_layout_nnnn_uuid_quadtree.py | 19 +++++++++-- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/ocfl/layout_nnnn_uuid_quadtree.py b/ocfl/layout_nnnn_uuid_quadtree.py index ba1414d..7139738 100644 --- a/ocfl/layout_nnnn_uuid_quadtree.py +++ b/ocfl/layout_nnnn_uuid_quadtree.py @@ -29,26 +29,56 @@ def config(self): def check_prefix(self, value): """Check prefix paremeter. - Any string is allowed. + Argument: + value (str): prefix string + + Raises: + LayoutException: if the prefix is not allowed + + Any non-empty string is allowed. Sets the prefix property as a side-effect. """ if value is None: raise LayoutException("prefix parameter must be specified") - if not isinstance(value, str): - raise LayoutException("prefix parameter must be a string") + if not isinstance(value, str) or value == "": + raise LayoutException("prefix parameter must be a non-empty string") self.prefix = value def encode(self, identifier): - """NOOP encode identifier.""" + """NOOP encode identifier. + + Arguments: + identifier (str): identifier + + Returns: + str: unchanged identifier + """ return identifier def decode(self, identifier): - """NOOP decode identifier.""" + """NOOP decode identifier. + + Arguments: + identifier (str): identifier + + Returns: + str: unchanged identifier + """ return identifier def identifier_to_path(self, identifier): """Convert identifier to path relative to root. - Must match prefix:6ba7b810-9dad-11d1-80b4-00c04fd430c8 + Argument: + identifier (str): object identifier + + Returns: + str: object path for this layout + + Raises: + LayoutException: if the identifier cannot be converted to a valid + object path. + + Format is "prefix:6ba7b810-9dad-11d1-80b4-00c04fd430c8" """ if identifier.startswith(self.prefix): identifier = identifier[len(self.prefix):] diff --git a/tests/test_layout_nnnn_uuid_quadtree.py b/tests/test_layout_nnnn_uuid_quadtree.py index 9d98a39..a9ba5ec 100644 --- a/tests/test_layout_nnnn_uuid_quadtree.py +++ b/tests/test_layout_nnnn_uuid_quadtree.py @@ -1,19 +1,34 @@ """Digest tests.""" import unittest +from ocfl.layout import LayoutException from ocfl.layout_nnnn_uuid_quadtree import Layout_NNNN_UUID_Quadtree class TestAll(unittest.TestCase): """TestAll class to run tests.""" - def test01_encode(self): + def test_config(self): + """Test config property.""" + layout = Layout_NNNN_UUID_Quadtree() + self.assertEqual(set(layout.config.keys()), set(("extensionName", "prefix"))) + + def test_check_prefix(self): + """Test check_prefix method.""" + layout = Layout_NNNN_UUID_Quadtree() + self.assertRaises(LayoutException, layout.check_prefix, None) + self.assertRaises(LayoutException, layout.check_prefix, 53) + self.assertRaises(LayoutException, layout.check_prefix, "") # stil not! + self.assertEqual(layout.check_prefix("Pref"), None) + self.assertEqual(layout.prefix, "Pref") + + def test_encode(self): """Test NOOP encode.""" uuqt = Layout_NNNN_UUID_Quadtree() self.assertEqual(uuqt.encode(""), "") self.assertEqual(uuqt.encode("a"), "a") self.assertEqual(uuqt.encode("a/b:?"), "a/b:?") - def test02_decode(self): + def test_decode(self): """Test NOOP decode.""" uuqt = Layout_NNNN_UUID_Quadtree() self.assertEqual(uuqt.decode(""), "")