Commit 6657df14 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Factor out JSON Schema validation in mappers tests

We introduce a assert_jsonschema_validate() helper function to reduce
code duplication.
parent 736bcbde24c5
......@@ -60,6 +60,13 @@ class AbstractBaseClassesTC(unittest.TestCase):
self.assertNotIsInstance([], JSONSchemaSerializer)
def assert_jsonschema_validate(instance, schema):
try:
jsonschema.validate(instance, schema)
except jsonschema.ValidationError as exc:
raise AssertionError(str(exc))
class JSONSchemaMapperTC(CubicWebTC):
class EntityLink(AppObject):
......@@ -368,10 +375,7 @@ class RelatedCollectionMapperTC(CubicWebTC):
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))
assert_jsonschema_validate(instance, schema)
def test_collection_serialize_composite_relation(self):
with self.admin_access.cnx() as cnx:
......@@ -388,10 +392,7 @@ class RelatedCollectionMapperTC(CubicWebTC):
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))
assert_jsonschema_validate(instance, schema)
class RelationMapperTC(CubicWebTC):
......@@ -1033,10 +1034,7 @@ class CompoundMapperTC(CubicWebTC):
schema = mapper.json_schema(VIEW_ROLE)
self._check_schema(schema)
instance = mapper.serialize()
try:
jsonschema.validate(instance, schema)
except jsonschema.ValidationError as exc:
self.fail(str(exc))
assert_jsonschema_validate(instance, schema)
def test_entity_create_no_compound_data(self):
with self.admin_access.cnx() as cnx:
......@@ -1591,10 +1589,7 @@ class WorkflowableEntityMapperTC(CubicWebTC):
instance = mapper.serialize()
self.assertEqual(instance['in_state'], 'activated')
schema = mapper.json_schema()
try:
jsonschema.validate(instance, schema)
except jsonschema.ValidationError as exc:
self.fail(str(exc))
assert_jsonschema_validate(instance, schema)
class WfobjsMapperTC(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