def test_multiple_schemas(self): s1 = { "required": [ "a", "c" ], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [ { "type": "integer" } ], "type": "array" } } } s2 = { "required": [ "c" ], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [ { "type": "integer" } ], "type": "array" } } } s = Schema() s.add_schema(s1) s.add_schema(s2) self.assertEqual(s.to_dict(), s2)
def test_three_deep(self): s = Schema().add_object( {"matryoshka": { "design": { "principle": "FTW!" } }}) self.assertEqual( s.to_dict(), { "required": ["matryoshka"], "type": "object", "properties": { "matryoshka": { "required": ["design"], "type": "object", "properties": { "design": { "required": ["principle"], "type": "object", "properties": { "principle": { "type": "string" } } } } } } })
def test_object_in_array(self): s = Schema().add_object([ {"name": "Sir Lancelot of Camelot", "quest": "to seek the Holy Grail", "favorite colour": "blue"}, {"name": "Sir Robin of Camelot", "quest": "to seek the Holy Grail", "capitol of Assyria": None}]) self.assertEqual(s.to_dict(), { "items": [ { "required": [ "name", "quest" ], "type": "object", "properties": { "quest": { "type": "string" }, "name": { "type": "string" }, "favorite colour": { "type": "string" }, "capitol of Assyria": { "type": "null" } } } ], "type": "array" })
def test_multitype_merge(self): s = Schema().add_object([1, "2", None, False]) self.assertEqual(s.to_dict(), { "type": "array", "items": [{ "type": ["boolean", "integer", "null", "string"]}] })
def test_multi_type(self): s = Schema() s.add_object("string") s.add_object(1.1) s.add_object(True) s.add_object(None) self.assertEqual(s.to_dict(), {"type": ["boolean", "null", "number", "string"]})
def test_object_in_array(self): s = Schema().add_object([{ "name": "Sir Lancelot of Camelot", "quest": "to seek the Holy Grail", "favorite colour": "blue" }, { "name": "Sir Robin of Camelot", "quest": "to seek the Holy Grail", "capitol of Assyria": None }]) self.assertEqual( s.to_dict(), { "items": [{ "required": ["name", "quest"], "type": "object", "properties": { "quest": { "type": "string" }, "name": { "type": "string" }, "favorite colour": { "type": "string" }, "capitol of Assyria": { "type": "null" } } }], "type": "array" })
def test_multi_type(self): s = Schema() s.add_object('string') s.add_object(1.1) s.add_object(True) s.add_object(None) self.assertEqual(s.to_dict(), {'type': ['boolean', 'null', 'number', 'string']})
def test_monotype(self): s = Schema().add_object(["spam", "spam", "spam", "egg", "spam"]) self.assertEqual(s.to_dict(), { "type": "array", "items": [{ "type": "string" }] })
def test_multi_type(self): s = Schema() s.add_object('string') s.add_object(1.1) s.add_object(True) s.add_object(None) self.assertSchema(s.to_dict(), {'type': ['boolean', 'null', 'number', 'string']})
def test_multitype_merge(self): s = Schema().add_object([1, "2", None, False]) self.assertEqual( s.to_dict(), { "type": "array", "items": [{ "type": ["boolean", "integer", "null", "string"] }] })
def test_multitype_sep(self): s = Schema(merge_arrays=False).add_object([1, "2", None, False]) self.assertEqual(s.to_dict(), { "type": "array", "items": [ {"type": "integer"}, {"type": "string"}, {"type": "null"}, {"type": "boolean"}] })
def test_array_reduce(self): s = Schema().add_object([["surprise"], ["fear", "surprise"], ["fear", "surprise", "ruthless efficiency"], ["fear", "surprise", "ruthless efficiency", "an almost fanatical devotion to the Pope"]]) self.assertEqual(s.to_dict(), { "type": "array", "items": [{ "type": "array", "items": [{"type": "string"}]}] })
class SchemaTestCase(unittest.TestCase): def setUp(self): self._schema = Schema() self._objects = [] self._schemas = [] def set_schema_options(self, **options): self._schema = Schema(**options) def add_object(self, obj): self._schema.add_object(obj) self._objects.append(obj) def add_schema(self, schema): self._schema.add_schema(schema) self._schemas.append(schema) def assertObjectValidates(self, obj): jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj) def assertObjectDoesNotValidate(self, obj): with self.assertRaises(jsonschema.exceptions.ValidationError): jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj) def assertResult(self, expected): self.assertEqual(self._schema.to_dict(), expected) self.assertUserContract() def assertUserContract(self): self._assertSchemaIsValid() self._assertComponentObjectsValidate() def _assertSchemaIsValid(self): jsonschema.Draft4Validator.check_schema(self._schema.to_dict()) def _assertComponentObjectsValidate(self): compiled_schema = self._schema.to_dict() for obj in self._objects: jsonschema.Draft4Validator(compiled_schema).validate(obj)
def test_basic_object(self): s = Schema().add_object({ "Red Windsor": "Normally, but today the van broke down.", "Stilton": "Sorry.", "Gruyere": False}) self.assertEqual(s.to_dict(), { "required": ["Gruyere", "Red Windsor", "Stilton"], "type": "object", "properties": { "Red Windsor": {"type": "string"}, "Gruyere": {"type": "boolean"}, "Stilton": {"type": "string"}} })
def test_three_deep(self): s = Schema().add_object( {"matryoshka": {"design": {"principle": "FTW!"}}}) self.assertEqual(s.to_dict(), { "required": ["matryoshka"], "type": "object", "properties": {"matryoshka": { "required": ["design"], "type": "object", "properties": {"design": { "required": ["principle"], "type": "object", "properties": {"principle": {"type": "string"}}}} }} })
def test_multitype_sep(self): s = Schema(merge_arrays=False).add_object([1, "2", None, False]) self.assertEqual( s.to_dict(), { "type": "array", "items": [{ "type": "integer" }, { "type": "string" }, { "type": "null" }, { "type": "boolean" }] })
def test_array_reduce(self): s = Schema().add_object([["surprise"], ["fear", "surprise"], ["fear", "surprise", "ruthless efficiency"], [ "fear", "surprise", "ruthless efficiency", "an almost fanatical devotion to the Pope" ]]) self.assertEqual( s.to_dict(), { "type": "array", "items": [{ "type": "array", "items": [{ "type": "string" }] }] })
def test_array_in_object(self): s = Schema().add_object({"a": "b", "c": [1, 2, 3]}) self.assertEqual( s.to_dict(), { "required": ["a", "c"], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [{ "type": "integer" }], "type": "array" } } })
def test_basic_object(self): s = Schema().add_object({ "Red Windsor": "Normally, but today the van broke down.", "Stilton": "Sorry.", "Gruyere": False }) self.assertEqual( s.to_dict(), { "required": ["Gruyere", "Red Windsor", "Stilton"], "type": "object", "properties": { "Red Windsor": { "type": "string" }, "Gruyere": { "type": "boolean" }, "Stilton": { "type": "string" } } })
def test_array_in_object(self): s = Schema().add_object({"a": "b", "c": [1, 2, 3]}) self.assertEqual(s.to_dict(), { "required": [ "a", "c" ], "type": "object", "properties": { "a": { "type": "string" }, "c": { "items": [ { "type": "integer" } ], "type": "array" } } })
def test_number(self): s = Schema().add_object(1.1) self.assertEqual(s.to_dict(), {"type": "number"})
def test_integer(self): s = Schema().add_object(1) self.assertEqual(s.to_dict(), {"type": "integer"})
def test_boolean(self): s = Schema().add_object(True) self.assertEqual(s.to_dict(), {"type": "boolean"})
def test_empty_object(self): s = Schema().add_object({}) self.assertEqual(s.to_dict(), {"type": "object", "properties": {}})
def test_null(self): s = Schema().add_object(None) self.assertEqual(s.to_dict(), {"type": "null"})
def test_single_type_unicode(self): schema = {u'type': u'string'} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_redundant_integer_type(self): s = Schema() s.add_object(1) s.add_object(1.1) self.assertEqual(s.to_dict(), {"type": "number"})
def test_single_type(self): s = Schema() s.add_object("bacon") s.add_object("egg") s.add_object("spam") self.assertEqual(s.to_dict(), {"type": "string"})
def test_to_dict_pending_deprecation_warning(self): with self.assertWarns(PendingDeprecationWarning): builder = Schema() with self.assertWarns(PendingDeprecationWarning): builder.add_object('I fart in your general direction!') builder.to_dict()
def test_single_type(self): schema = {'type': 'string'} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_string(self): s = Schema().add_object("string") self.assertEqual(s.to_dict(), {"type": "string"})
def test_single_type(self): schema = {'type': 'string'} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_recurse_deprecation_warning(self): with self.assertWarns(DeprecationWarning): builder = Schema() builder.add_object('Go away or I shall taunt you a second time!') builder.to_dict(recurse=True)
def test_preserves_existing_keys(self): schema = {'type': 'number', 'value': 5} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_single_type(self): s = Schema() s.add_object('bacon') s.add_object('egg') s.add_object('spam') self.assertEqual(s.to_dict(), {'type': 'string'})
def test_single_type_unicode(self): schema = {u'type': u'string'} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_no_schema(self): schema = {} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_preserves_existing_keys(self): schema = {'type': 'number', 'value': 5} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_multi_type(self): schema = {'type': ['boolean', 'null', 'number', 'string']} s = Schema() s.add_schema(schema) self.assertEqual(s.to_dict(), schema)
def test_empty(self): s = Schema().add_object([]) self.assertEqual(s.to_dict(), {"type": "array", "items": []})
def test_no_schema(self): schema = {} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_redundant_integer_type(self): s = Schema() s.add_object(1) s.add_object(1.1) self.assertEqual(s.to_dict(), {'type': 'number'})
def test_multi_type(self): schema = {'type': ['boolean', 'null', 'number', 'string']} s = Schema() s.add_schema(schema) self.assertSchema(s.to_dict(), schema)
def test_no_object(self): s = Schema() self.assertEqual(s.to_dict(), {})
def test_single_type(self): s = Schema() s.add_object('bacon') s.add_object('egg') s.add_object('spam') self.assertSchema(s.to_dict(), {'type': 'string'})
def test_redundant_integer_type(self): s = Schema() s.add_object(1) s.add_object(1.1) self.assertSchema(s.to_dict(), {'type': 'number'})
def test_monotype(self): s = Schema().add_object(["spam", "spam", "spam", "egg", "spam"]) self.assertEqual(s.to_dict(), {"type": "array", "items": [{"type": "string"}]})