Commit e1ba39be authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Test that unicity of CompoundMapper's name applies within a given entity type

And make the error message a bit more explicit.

Closes #17103219.
parent fd81943ceab3
......@@ -104,7 +104,8 @@ class CompoundMapper(JSONSchemaMapper):
# Make sure 'name' is unique amongst objects with
# 'jsonschema.object' regid.
raise ValueError('a class with name "{}" is already '
'registered'.format(cls.name))
'registered for {} entity type'.format(
cls.name, cls.etype))
# Prevent duplicate mapping of the same etype/rtype.
common_rtypes = set(obj._relations) & set(cls._relations)
if common_rtypes:
......
......@@ -922,6 +922,40 @@ class CompoundMapperTC(CubicWebTC):
except ValueError as e:
self.fail("unexpected {}".format(e))
def test_name_unicity_constraints_per_entity_type(self):
"""Several CompoundMapper with the same name may be registered if they
concern distinct entity types.
"""
class c1(CompoundMapper):
etype = 'Book'
relations = (
'author',
)
class c2(CompoundMapper):
name = 'c1'
etype = 'CWUser'
relations = (
'in_group',
)
class c3(CompoundMapper):
name = 'c1'
etype = 'Book'
relations = (
('author', 'object')
)
registry = self.vreg['mappers']
with patch.dict(registry, clear=True):
registry.register(c1)
self.assertIn(c1, self.vreg['mappers']['jsonschema.object'])
registry.register(c2)
self.assertIn(c2, self.vreg['mappers']['jsonschema.object'])
expected_regexp = 'a class with name "c1" is already registered for Book entity type' # noqa: E501
with self.assertRaisesRegex(ValueError, expected_regexp):
c3.__registered__(self.vreg['mappers'])
def test_mapped_attributes_hidden(self):
"""Make sure attributes mapped to CompoundMapper are in "hidden" uicfg
section.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment