Skip to content

Commit

Permalink
Merge pull request #136 from iiif-prezi/autoid_fix
Browse files Browse the repository at this point in the history
Fix issues with AutoId
  • Loading branch information
glenrobson authored Dec 9, 2022
2 parents cfce666 + c9cb01d commit 2c933da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions iiif_prezi3/helpers/auto_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand Down
24 changes: 23 additions & 1 deletion tests/test_helpers_autofields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from iiif_prezi3 import Manifest, config
from iiif_prezi3 import Collection, Manifest, config


class AutoFieldsHelpersTests(unittest.TestCase):
Expand Down Expand Up @@ -43,3 +43,25 @@ 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_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_type
del (config.configs['helpers.auto_fields.AutoId'].translation["Manifest"])

0 comments on commit 2c933da

Please sign in to comment.