From d184279e5bbccc33d9af5a1ebf7c2427a00ee071 Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Fri, 9 Dec 2022 13:58:12 +0000 Subject: [PATCH 1/2] Fix custom slugs to read from the right config property + add tests --- iiif_prezi3/helpers/auto_fields.py | 4 ++-- tests/test_helpers_autofields.py | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/iiif_prezi3/helpers/auto_fields.py b/iiif_prezi3/helpers/auto_fields.py index 630dfb3..be28184 100644 --- a/iiif_prezi3/helpers/auto_fields.py +++ b/iiif_prezi3/helpers/auto_fields.py @@ -82,7 +82,7 @@ def generate_id(self, what, auto_type=None): slug = self._auto_id_int elif auto_type == "int-per-type": t = type(what).__name__ - t = self._type_translation.get(t, t) + t = self.config.translation.get(t, t) curr = self._auto_id_types.get(t, -1) curr += 1 self._auto_id_types[t] = curr @@ -91,7 +91,7 @@ def generate_id(self, what, auto_type=None): slug = str(uuid.uuid4()) elif auto_type == "uuid-per-type": t = type(what).__name__ - t = self._type_translation.get(t, t) + t = self.config.translation.get(t, t) slug = f"{t}/{uuid.uuid4()}" else: raise ValueError(f"Unknown auto-id type: {auto_type}") diff --git a/tests/test_helpers_autofields.py b/tests/test_helpers_autofields.py index bd24ea6..9c769ec 100644 --- a/tests/test_helpers_autofields.py +++ b/tests/test_helpers_autofields.py @@ -1,6 +1,6 @@ import unittest -from iiif_prezi3 import Manifest, config +from iiif_prezi3 import Manifest, Collection, config class AutoFieldsHelpersTests(unittest.TestCase): @@ -43,3 +43,26 @@ def test_id_config(self): m = Manifest(label="string") self.assertEqual(len(m.id), 60) config.configs['helpers.auto_fields.AutoId'].auto_type = curr + + def test_int_per_type(self): + """Test that the int-per-type helper works.""" + curr = config.configs['helpers.auto_fields.AutoId'].auto_type + config.configs['helpers.auto_fields.AutoId'].auto_type = "int-per-type" + m = Manifest(label="autoint") + self.assertEqual('http://example.org/iiif/Manifest/0', m.id) + m2 = Manifest(label="autoint2") + self.assertEqual('http://example.org/iiif/Manifest/1', m2.id) + c = Collection() + self.assertEqual('http://example.org/iiif/Collection/0', c.id) + config.configs['helpers.auto_fields.AutoId'].auto_type = curr + + def test_custom_slug(self): + """Test that setting a custom slug for a specific type works.""" + curr = config.configs['helpers.auto_fields.AutoId'].auto_type + config.configs['helpers.auto_fields.AutoId'].auto_type = "int-per-type" + config.configs['helpers.auto_fields.AutoId'].translation["Manifest"] = "mani" + m = Manifest(label="custom slug") + self.assertEqual('http://example.org/iiif/mani/0', m.id) + config.configs['helpers.auto_fields.AutoId'].auto_type = curr + + From c9cb01dd5b8b4ed1207347943498d0bf94d8710b Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Fri, 9 Dec 2022 14:07:31 +0000 Subject: [PATCH 2/2] Fix broken tests --- tests/test_helpers_autofields.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_helpers_autofields.py b/tests/test_helpers_autofields.py index 9c769ec..23b60fe 100644 --- a/tests/test_helpers_autofields.py +++ b/tests/test_helpers_autofields.py @@ -1,6 +1,6 @@ import unittest -from iiif_prezi3 import Manifest, Collection, config +from iiif_prezi3 import Collection, Manifest, config class AutoFieldsHelpersTests(unittest.TestCase): @@ -58,11 +58,10 @@ def test_int_per_type(self): def test_custom_slug(self): """Test that setting a custom slug for a specific type works.""" - curr = config.configs['helpers.auto_fields.AutoId'].auto_type + curr_type = config.configs['helpers.auto_fields.AutoId'].auto_type config.configs['helpers.auto_fields.AutoId'].auto_type = "int-per-type" config.configs['helpers.auto_fields.AutoId'].translation["Manifest"] = "mani" m = Manifest(label="custom slug") self.assertEqual('http://example.org/iiif/mani/0', m.id) - config.configs['helpers.auto_fields.AutoId'].auto_type = curr - - + config.configs['helpers.auto_fields.AutoId'].auto_type = curr_type + del (config.configs['helpers.auto_fields.AutoId'].translation["Manifest"])