Commit 37d0ac4e authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Add a test about selection score for jsonschema.item mappers for relations

parent 74f866411ac8
......@@ -34,6 +34,7 @@ from cubicweb.predicates import match_kwargs
from cubicweb_jsonschema import CREATION_ROLE, EDITION_ROLE, VIEW_ROLE
from cubicweb_jsonschema.mappers import (
JSONSchemaDeserializer,
JSONSchemaMapper,
JSONSchemaSerializer,
CompoundMapper,
yams_component_target,
......@@ -394,6 +395,45 @@ class RelatedCollectionMapperTC(CubicWebTC):
self.assertEqual(instance, expected)
assert_jsonschema_validate(instance, schema)
def test_custom_mapper(self):
"""Make sure a custom mapper with a yams_match selector wins selection
over default one.
"""
class reverse_author_mapper(JSONSchemaMapper):
__regid__ = 'jsonschema.item'
__select__ = yams_match(rtype='author', role='object')
def schema_and_definitions(self, schema_role=None):
return {
'type': 'object',
'properties': {
'name': {'type': 'string'},
},
}, None
def serialize(self, entity):
return {
'name': entity.name,
}
with self.admin_access.cnx() as cnx:
author = cnx.create_entity('Author', name=u'bob')
cnx.commit()
with self.temporary_appobjects(reverse_author_mapper):
item_mapper = cnx.vreg['mappers'].select(
'jsonschema.item', cnx, entity=author,
rtype='author', role='object')
collection_mapper = cnx.vreg['mappers'].select(
'jsonschema.collection', cnx, etype='Author',
rtype='author', role='object')
self.assertIsInstance(item_mapper, reverse_author_mapper)
self.assertEqual(item_mapper.serialize(author),
{'name': 'bob'})
authors = collection_mapper.serialize([author])
self.assertEqual(authors,
[{'name': 'bob'}])
class RelationMapperTC(CubicWebTC):
......
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