def test_possible_add_resource(self): spec = APISpecification(version='v1', base_url='http://api.glb.com') resource = Resource('comments') resource.add_path(Path('/comments')) spec.add_resource(resource) assert len(spec.resources) == 1 assert len(resource.paths) == 1 assert resource.name == 'comments' assert resource.paths[0].name == '/comments'
def add(self, path, handler): resource = Resource(path) basic_methods = list(self.get_basic_methods(handler)) if basic_methods: resource.add_path( Path('/{0}'.format(path), methods=basic_methods)) resource.add_path( Path('/{0}.{{type}}'.format(path), params=[Param('type', style='url')], methods=basic_methods)) instance_methods = list(self.get_instance_methods(handler)) if instance_methods: resource.add_path( Path('/{0}/{{key}}'.format(path), params=[Param('key', style='url')], methods=instance_methods)) resource.add_path( Path('/{0}/{{key}}.{{type}}'.format(path), params=[ Param('key', style='url'), Param('type', style='url') ], methods=instance_methods)) self.spec.add_resource(resource)
def test_generate_with_params(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource( Resource('dogs', paths=[ Path('/dogs/{key}', params=[Param('key')], methods=[Method('POST'), Method('GET')]), ])) result = self.gen(api) doc = ElementTree.fromstring(result) resources = doc.getchildren()[0] resource = resources.getchildren()[0] param = resource.getchildren()[0] assert param.tag.endswith('param') assert param.get('name') == 'key' assert param.get('required') == 'true' assert param.get('type') == 'xsd:string' assert param.get('style') == 'template' method_1 = resource.getchildren()[1] assert method_1.tag.endswith('method') assert method_1.get('name') == 'POST' method_2 = resource.getchildren()[2] assert method_2.tag.endswith('method') assert method_2.get('name') == 'GET'
def test_possible_specify_simple_api(self): api = APISpecification(version='', base_url='') api.add_resource( Resource('comments', paths=[ Path('/comments.{key}', params=[ Param('key', default_value='json', required=False, options=['json', 'js', 'xml']), ], methods=[ Method('GET', errors=[ APIError(code=301, description=''), APIError(code=404, description='') ]), Method('POST', errors=[ APIError(code=301, description=''), APIError(code=404, description='') ]), ]), ])) resource = api.resources[0] assert resource.paths[0].name == '/comments.{key}' assert resource.paths[0].params[0].name == 'key' assert resource.paths[0].params[0].default_value == 'json' assert resource.paths[0].params[0].required == False assert resource.paths[0].params[0].options == ['json', 'js', 'xml'] assert resource.paths[0].methods[0].name == 'GET' assert resource.paths[0].methods[1].name == 'POST'
def test_genetate_with_more_than_one_resource(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource( Resource('dogs', paths=[Path('/dogs', methods=[Method('PUT')])])) api.add_resource( Resource('cats', paths=[Path('/cats', methods=[Method('PUT')])])) result = self.gen(api) doc = ElementTree.fromstring(result) assert doc.tag.endswith('application') resource = doc.getchildren()[0] resources = resource.getchildren() assert len(resources) == 2 assert resources[0].tag.endswith('resource') assert resources[0].get('path') == '/dogs' method = resources[0].getchildren()[0] assert method.tag.endswith('method') assert method.get('name') == 'PUT' assert resources[1].tag.endswith('resource') assert resources[1].get('path') == '/cats' method = resources[1].getchildren()[0] assert method.tag.endswith('method') assert method.get('name') == 'PUT'
def add(self, path, handler): resource = Resource(path) basic_methods = list(self.get_basic_methods(handler)) if basic_methods: resource.add_path(Path('/{0}'.format(path), methods=basic_methods)) resource.add_path( Path('/{0}.{{type}}'.format(path), params=[Param('type', style='url')], methods=basic_methods)) instance_methods = list(self.get_instance_methods(handler)) if instance_methods: resource.add_path( Path('/{0}/{{key}}'.format(path), params=[Param('key', style='url')], methods=instance_methods)) resource.add_path( Path('/{0}/{{key}}.{{type}}'.format(path), params=[ Param('key', style='url'), Param('type', style='url') ], methods=instance_methods)) self.spec.add_resource(resource)
def test_generate_with_resource(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource( Resource('comments', paths=[Path('/comments', methods=[Method('GET')])])) result = self.gen(api) doc = ElementTree.fromstring(result) assert doc.tag.endswith('application') resources = doc.getchildren()[0] assert resources.tag.endswith('resources') assert resources.get('base') == 'http://api.globo.com/v1' resource = resources.getchildren()[0] assert resource.tag.endswith('resource') assert resource.get('path') == '/comments' method = resource.getchildren()[0] assert method.tag.endswith('method') assert method.get('name') == 'GET'
def test_create_resource(self): resource = Resource('comments', description='') assert resource.name == 'comments' assert resource.description == '' assert resource.paths == []