def test_adjacent_reference(self): register = trafaret_schema.Register() addresses = trafaret_schema.json_schema( { "$id": "http://yuhuhu.com/address", "type": "object", "properties": { "billing_address": { "$ref": "#/definitions/address" }, "shipping_address": { "$ref": "#/definitions/address" }, }, "definitions": { "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" }, }, "required": ["city"], }, }, }, context=register, ) person = trafaret_schema.json_schema( { "type": "object", "properties": { "name": { "type": "string" }, "address": { "$ref": "http://yuhuhu.com/address#/definitions/address" }, }, }, context=register, ) data = { 'name': 'Peotr', 'address': { 'city': 'Moscow' }, } assert person.check(data) == data register.validate_references()
def test_schemas(self): for root, dirs, files in os.walk(self.schema_dir): for filename in files: path = op.join(root, filename) with open(path) as f: schema = json.load(f) print('Check', path) trafaret_schema.json_schema(schema)
def test_const(self): check = trafaret_schema.json_schema({ 'const': 'blabla', }) self.assertEqual(check('blabla'), 'blabla') check = trafaret_schema.json_schema({ 'const': 100, }) self.assertEqual(check(100), 100)
def test_maximum(self): check = trafaret_schema.json_schema({ 'type': 'number', 'maximum': 5, }) with self.assertRaises(t.DataError): check(10) check = trafaret_schema.json_schema({ 'type': 'number', 'exclusiveMaximum': 5, }) with self.assertRaises(t.DataError): check(5)
def test_dependencies(self): check = trafaret_schema.json_schema({ 'type': 'object', 'properties': { 'a': { 'type': 'number' } }, 'dependencies': { 'a': ['b', 'c'] }, }) self.assertEqual(check({ 'bla': 1, 'blabla': 3 }), { 'bla': 1, 'blabla': 3 }) with self.assertRaises(t.DataError): check({'a': 'b'}) self.assertEqual(check({ 'a': 1, 'b': 3, 'c': 4 }), { 'a': 1, 'b': 3, 'c': 4 })
def test_number(self): check = trafaret_schema.json_schema({ 'type': 'integer', }) self.assertEqual(check(100), 100) with self.assertRaises(t.DataError): check(100.4)
def test_max_length(self): check = trafaret_schema.json_schema({ 'type': 'string', 'maxLength': 5, }) with self.assertRaises(t.DataError): check('blabla')
def test_ipv6(self): schema = trafaret_schema.json_schema({ "type": "object", "properties": { "ip": {"format": "ipv6"} }, }) schema({'ip': '::1'})
def test_uniq(self): check = trafaret_schema.json_schema({ 'type': 'array', 'uniqueItems': True, }) with self.assertRaises(t.DataError): check([1, 2, 3, 4, 5, 5]) self.assertEqual(check([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
def test_max_items(self): check = trafaret_schema.json_schema({ 'type': 'array', 'maxItems': 5, }) with self.assertRaises(t.DataError): check([1, 2, 3, 4, 5, 6]) self.assertEqual(check([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
def test_enum(self): check = trafaret_schema.json_schema({ 'enum': ['blabla', 200], }) self.assertEqual(check('blabla'), 'blabla') self.assertEqual(check(200), 200) with self.assertRaises(t.DataError): check(300)
def test_multiple_of(self): check = trafaret_schema.json_schema({ 'type': 'number', 'multipleOf': 5, }) self.assertEqual(check(10), 10) with self.assertRaises(t.DataError): check(11)
def test_pattern(self): check = trafaret_schema.json_schema({ 'type': 'string', 'pattern': 'bla+', 'maxLength': 10, 'minLength': 5, }) self.assertEqual(check('blablabla'), 'blablabla')
def test_time(self): schema = trafaret_schema.json_schema({ "type": "object", "properties": { "time": {"format": "time"} }, }) schema({'time': '19:59'})
def test_phone(self): schema = trafaret_schema.json_schema({ "type": "object", "properties": { "phone": {"format": "phone"} }, }) schema({'phone': '+7 927 728 67 67'})
def test_date(self): schema = trafaret_schema.json_schema({ "type": "object", "properties": { "datetime": {"format": "date"} }, }) schema({'datetime': '2017-09-02'})
def test_required(self): check = trafaret_schema.json_schema({ 'type': 'object', 'required': ['a', 'b'], }) with self.assertRaises(t.DataError): check({'a': 1}) self.assertEqual(check({'a': 1, 'b': 2}), {'a': 1, 'b': 2})
def test_email(self): schema = trafaret_schema.json_schema({ "type": "object", "properties": { "email": {"format": "email"} }, }) schema({'email': '*****@*****.**'})
def test_min_props(self): check = trafaret_schema.json_schema({ 'type': 'object', 'minProperties': 2, }) with self.assertRaises(t.DataError): check({'a': 1}) self.assertEqual(check({'a': 1, 'b': 2}), {'a': 1, 'b': 2})
def test_not(self): check = trafaret_schema.json_schema({ 'not': { 'minLength': 5 }, }) self.assertEqual(check('bla'), 'bla') with self.assertRaises(t.DataError): check('blabla')
def test_simple_items(self): check = trafaret_schema.json_schema({ 'type': 'array', 'items': { 'type': 'number' }, }) with self.assertRaises(t.DataError): check([1, 2, 'a', 4, 5, 5]) self.assertEqual(check([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
def test_number(): reg = trafaret_schema.Register() schema = trafaret_schema.json_schema( { "type": "number", }, reg, ) assert schema('100.0') == '100.0' assert schema(decimal.Decimal('100.0')) == decimal.Decimal('100.0')
def test_contains(self): check = trafaret_schema.json_schema({ 'type': 'array', 'contains': { 'type': 'number' }, }) with self.assertRaises(t.DataError): check(['a', 'b', 'c']) self.assertEqual(check(['a', 'b', 'c', 5]), ['a', 'b', 'c', 5])
def test_number_as_decimal(): reg = trafaret_schema.Register() reg.reg_format("decimal", decimal.Decimal) schema = trafaret_schema.json_schema( { "type": "number", "format": "decimal", }, reg, ) assert schema('100.0') == decimal.Decimal('100.0')
def test_properties(self): check = trafaret_schema.json_schema({ 'type': 'object', 'properties': { 'a': { 'type': 'number' } }, }) with self.assertRaises(t.DataError): check({'a': 'b'}) self.assertEqual(check({'a': 1, 'b': 2}), {'a': 1, 'b': 2})
def test_reg_format(self): register = trafaret_schema.Register() register.reg_format('any_ip', t.IP) schema = trafaret_schema.json_schema({ "type": "object", "properties": { "ip_addr": {"format": "any_ip"} }, }, context=register, ) schema({'ip_addr': '192.168.0.1'}) schema({'ip_addr': '::1'})
def test_all_of(self): check = trafaret_schema.json_schema({ 'allOf': [ { 'minLength': 5 }, { 'maxLength': 10 }, ], }) self.assertEqual(check('blabla'), 'blabla') with self.assertRaises(t.DataError): check('bla')
def test_positional_items(self): check = trafaret_schema.json_schema({ 'type': 'array', 'items': [{ 'type': 'number' }, { 'type': 'string' }], }) with self.assertRaises(t.DataError): # bad 2nd position check([1, None]) with self.assertRaises(t.DataError): # too long array check([1, 'a', 4, 5, 5]) self.assertEqual(check([1, 'a']), [1, 'a'])
def test_property_names(self): check = trafaret_schema.json_schema({ 'type': 'object', 'propertyNames': { 'type': 'string', 'pattern': 'bla+', }, }) with self.assertRaises(t.DataError): check({'a': 'b'}) self.assertEqual(check({ 'bla': 1, 'blabla': 3 }), { 'bla': 1, 'blabla': 3 })
def test_additional_items(self): check = trafaret_schema.json_schema({ 'type': 'array', 'items': [{ 'type': 'number' }, { 'type': 'string' }], 'additionalItems': { 'type': 'number' }, }) with self.assertRaises(t.DataError): check([1, None, 4, 5, 5]) with self.assertRaises(t.DataError): check([1, 'a', 'a', 5, 5]) self.assertEqual(check([1, 'a', 5, 5, 5]), [1, 'a', 5, 5, 5])