def test_additional_properties(self): obj = Object({'test': 'example'}, schema=Schema( {'additionalProperties': { 'type': 'string' }})) self.assertEqual(obj['test'].schema, {'type': 'string'})
def test_properties_non_matching(self): obj = Object({'test': 'example'}, schema=Schema( {'properties': { 'tast': { 'type': 'string' } }})) self.assertEqual(obj['test'].schema, {})
def test_additional_items(self): obj = Array([1, 2, 3], schema=Schema({ 'items': ({ 'type': 'number' }, { 'type': 'string' }), 'additionalItems': { 'type': 'integer' } })) self.assertEqual(obj[0].schema, {'type': 'number'}) self.assertEqual(obj[1].schema, {'type': 'string'}) self.assertEqual(obj[2].schema, {'type': 'integer'})
def test_items_tuple(self): obj = Array([{ 'test': 1 }, { 'test': 2 }, { 'test': 3 }], schema=Schema( {'items': ({ 'type': 'number' }, { 'type': 'string' })})) self.assertEqual(obj[1].schema, {'type': 'string'}) self.assertEqual(obj[0].schema, {'type': 'number'})
def test_items(self): obj = Array([{ 'test': 1 }, { 'test': 2 }, { 'test': '3' }], schema=Schema({ 'items': { 'properties': { 'test': { 'type': 'number', 'pattern': '1' } } } })) self.assertTrue('properties' in obj[1].schema) self.assertEqual(obj[1]['test'].schema, { 'type': 'number', 'pattern': '1' })
class MessageCollection(api.Collection): schema = Schema({'id': 'message-collection'}) objects = Message.objects
class Message(api.Resource): schema = Schema({'id': 'message'})
class StoredTestResource(Resource): schema = Schema({'id': 'stored-test'}) @classmethod def db(cls): return MongoClient()['test']
import unittest import os from pymongo import MongoClient from json_resource import Resource, Schema, ResourceNotFound, ResourceExists, \ ValidationError Schema.register_schema_dir(os.path.join(os.path.dirname(__file__), 'schemas')) class StoredTestResource(Resource): schema = Schema({'id': 'stored-test'}) @classmethod def db(cls): return MongoClient()['test'] class StoredResourceTest(unittest.TestCase): def setUp(self): self.resource = StoredTestResource({'id': 1}) def tearDown(self): StoredTestResource.objects.collection.remove({}) def test_insert(self): self.resource.save(create=True) self.assertTrue(self.resource.exists)
schema = Schema({ 'id': 'test', 'description': 'Test Description', 'title': 'Test Title', 'properties': { 'id': { 'type': 'number' }, 'sub-resource': { 'properties': { 'test': { 'type': 'string', 'format': 'email' } } }, 'type': { 'type': 'string' } }, 'required': ['id'], 'links': [{ 'rel': 'self', 'mediaType': 'application/json', 'href': '/test/{id}' }, { 'rel': 'self', 'mediaType': 'application/jpeg', 'href': '/test/{id}.jpg' }] })
class TestCollection(Collection): schema = Schema({'id': 'collection'}) objects = TestResource.objects