def test_require_any_of(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}) schema.add_property('bar', {'type': 'string'}) schema.require_any_of(['foo', 'bar']) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': { 'foo': {'type': 'string'}, 'bar': {'type': 'string'}, }, 'allOf': [ { 'anyOf': [ {'required': ['foo']}, {'required': ['bar']} ], }, ], } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_constructor_accepts_type(self): schema = JSONSchema(type_='array') expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'array', } serialized = schema.serialize() self.assertEqual(expected, serialized) self.assertIsInstance(serialized, OrderedDict)
def test_constructs_minimal_schema(self): schema = JSONSchema() expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', } serialized = schema.serialize() self.assertEqual(expected, serialized) self.assertIsInstance(serialized, OrderedDict)
def test_drop_property_with_missing_property(self): schema = JSONSchema() schema.drop_property('doesnt-exist') expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_constructor_accepts_additional_properties_flag(self): schema = JSONSchema(additional_properties=False) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'additionalProperties': False, } serialized = schema.serialize() self.assertEqual(expected, serialized) self.assertIsInstance(serialized, OrderedDict)
def test_constructor_accepts_title(self): schema = JSONSchema(title=u'Foobar') expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'title': 'Foobar', } serialized = schema.serialize() self.assertEqual(expected, serialized) self.assertIsInstance(serialized, OrderedDict)
def test_dropping_last_property_removes_properties_key(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}) schema.drop_property('foo') expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_add_property_supports_required_kwarg(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}, required=True) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': { 'foo': {'type': 'string'}, }, 'required': ['foo'], } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_set_not_required_removes_empty_requires_list(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}, required=True) schema.set_not_required('foo') expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': { 'foo': {'type': 'string'}, }, } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_add_property(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': { 'foo': {'type': 'string'}, }, } serialized = schema.serialize() self.assertEqual(expected, serialized) self.assertIsInstance(serialized, OrderedDict) self.assertIsInstance(serialized['properties'], OrderedDict) self.assertIsInstance(serialized['properties']['foo'], OrderedDict)
def test_set_required_doesnt_add_property_twice_to_requireds(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}) schema.set_required('foo') schema.set_required('foo') expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': { 'foo': {'type': 'string'}, }, 'required': ['foo'], } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_make_optional_properties_nullable(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}, required=True) schema.add_property('bar', {'type': 'string'}) schema.make_optional_properties_nullable() expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': { 'foo': {'type': 'string'}, 'bar': {'type': ['null', 'string']}, }, 'required': ['foo'], } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_set_field_order(self): schema = JSONSchema() schema.add_property('foo', {'type': 'string'}) schema.add_property('bar', {'type': 'string'}) schema.set_field_order(['foo', 'bar']) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': { 'foo': {'type': 'string'}, 'bar': {'type': 'string'}, }, 'field_order': ['foo', 'bar'], } serialized = schema.serialize() self.assertEqual(expected, serialized)
def test_add_definition(self): schema = JSONSchema() subschema = JSONSchema() subschema.add_property('subfoo', {'type': 'string'}) schema.add_definition('sub', subschema) expected = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'definitions': { 'sub': { 'type': 'object', 'properties': { 'subfoo': {'type': 'string'}, }, } } } serialized = schema.serialize() self.assertDictEqual(expected, serialized)