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

Add tests for serialization of item of composite non-inlined relations

Prior to introduction of CompositeRelationItemMapper in previous
changests, serialization of items of a composite relation that is not
inlined did not work. By making use of plain "jsonschema.item" mapper
for targets of such relations we make this possible.

We here add unit tests in test_mappers.py along with a functional test
in test_api_misc.py.
parent 463795b65da8
......@@ -169,6 +169,26 @@ class EntitiesTC(BaseTC):
headers={'Accept': 'application/json'})
self.assertEqual(response.json, expected_instance)
def test_get_related_reverse_role(self):
with self.admin_access.cnx() as cnx:
author = cnx.create_entity('Author', name=u'bob')
books = [
cnx.create_entity('Book', title=u'1', author=author),
cnx.create_entity('Book', title=u'2', author=author),
]
cnx.commit()
self.login()
url = '/author/%s/reverse-author' % author.eid
response = self.webapp.get(url, status=200,
headers={'Accept': 'application/json'})
expected = [
{'id': str(entity.eid),
'title': entity.dc_title(),
'type': entity.cw_etype.lower()}
for entity in books
]
self.assertCountEqual(response.json, expected)
def test_post_related_entity_notfound(self):
self.login()
url = '/book/999/topics'
......
......@@ -347,6 +347,52 @@ class RelatedCollectionMapperTC(CubicWebTC):
expected = [{'id': str(t.eid)} for t in topics]
self.assertEqual(instance, expected)
@staticmethod
def _item_serialize(entity):
return {
'id': str(entity.eid),
'title': entity.dc_title(),
'type': entity.cw_etype.lower(),
}
def test_item_serialize_composite_relation(self):
with self.admin_access.cnx() as cnx:
author = cnx.create_entity('Author', name=u'bob')
book = cnx.create_entity('Book', title=u'1', author=author)
cnx.commit()
mapper = cnx.vreg['mappers'].select(
'jsonschema.item', cnx, entity=author,
rtype='author', role='object')
instance = mapper.serialize(book)
schema = mapper.json_schema()
expected = self._item_serialize(book)
self.assertEqual(instance, expected)
self.assertEqual(instance, expected)
try:
jsonschema.validate(instance, schema)
except jsonschema.ValidationError as exc:
self.fail(str(exc))
def test_collection_serialize_composite_relation(self):
with self.admin_access.cnx() as cnx:
author = cnx.create_entity('Author', name=u'bob')
books = [
cnx.create_entity('Book', title=u'1', author=author),
cnx.create_entity('Book', title=u'2', author=author),
]
cnx.commit()
mapper = cnx.vreg['mappers'].select(
'jsonschema.collection', cnx, entity=author,
rtype='author', role='object')
instance = mapper.serialize(books)
schema = mapper.json_schema()
expected = [self._item_serialize(entity) for entity in books]
self.assertEqual(instance, expected)
try:
jsonschema.validate(instance, schema)
except jsonschema.ValidationError as exc:
self.fail(str(exc))
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