def test__add_tags_in_spec(self, apispec_version_major, func_to_call): tag = { 'name': 'tag_name', 'description': 'tag_description', 'add_in_spec': True } mock_self = Mock() mock_self.spec_tag = {tag['name']: tag} with patch(f'{module_path}.APISPEC_VERSION_MAJOR', new=apispec_version_major): ApiSpecPlugin._add_tags_in_spec(mock_self, tag) # do nothing if already added in spec getattr(mock_self.spec, func_to_call).assert_not_called() assert mock_self.spec_tag[tag['name']]['add_in_spec'] is True # else add and mark tag['add_in_spec'] = False ApiSpecPlugin._add_tags_in_spec(mock_self, tag) getattr(mock_self.spec, func_to_call).assert_called_once_with({ "name": tag["name"], "description": tag["description"] }) assert mock_self.spec_tag[tag['name']]['add_in_spec'] is True
def register_extensions(app): db.init_app(app) migrate.init_app(app, db, compare_type=True) csrf.init_app(app) admin.init_app(app) api.plugins = [ EventPlugin(), PermissionPlugin(), ApiSpecPlugin(app=app, tags={ 'Tag': 'Tag API', 'User': '******', 'Author': 'Author API', 'Article': 'Article API', }), ] api.init_app(app) login_manager.login_view = 'auth.login' login_manager.init_app(app) @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id))
def plugin(self): def some_decorator(func): return func instance = ApiSpecPlugin(app=app, decorators=[some_decorator], tags=self.tags) return instance
def test__update_parameter_for_field_spec(self): new_param = {'foo': 'bar'} spec = { 'items': { 'type': 'some_type', 'enum': [1, 2, 3], 'spam': 'eggs', } } ApiSpecPlugin._update_parameter_for_field_spec(new_param, spec) assert new_param == { 'foo': 'bar', 'items': { 'type': 'some_type', 'enum': [1, 2, 3] } }
def test__get_parameters_for_sparse_fieldsets(self): description = "List that refers to the name(s) of the fields to be returned `{}`" result = ApiSpecPlugin._ApiSpecPlugin__get_parameters_for_sparse_fieldsets( SomeResourceDetail, description) assert result == { "name": f"fields[{SomeResourceDetail.schema.Meta.type_}]", "in": "query", "type": "array", "required": False, "description": description.format(SomeResourceDetail.schema.Meta.type_), "items": { "type": "string", "enum": list(SomeResourceDetail.schema._declared_fields.keys()) }, }
def test__get_parameters_for_include_models(self): result = ApiSpecPlugin._ApiSpecPlugin__get_parameters_for_include_models( SomeResourceDetail) assert result == { 'default': 'related_model_id', "name": "include", 'in': 'query', 'format': 'string', "required": False, 'description': 'Related relationships to include.\nAvailable:\n`related_model_id`' }
def test_after_init_plugin(self): mock_self = Mock() mock_self._fields = mock_self._converters = [] ApiSpecPlugin.after_init_plugin(mock_self) mock_self._register_doc_blueprint.assert_called_once_with()
class ComputerDetail(ResourceDetail): schema = ComputerSchema data_layer = { "session": db.session, "model": Computer, "permission_get": [ComputersPermission], } api_spec_plugin = ApiSpecPlugin( app=app, # Declaring tags list with their descriptions, so API gets organized into groups. # This is optional: when there are no tags, # api will be grouped automatically by type schemas names (type_) tags={ "Person": "Person API", "Computer": "Computer API", }, ) # Create endpoints api = Api( app, plugins=[ api_spec_plugin, PermissionPlugin(strict=False), ], ) api.route(PersonList, "person_list", "/persons", tag="Person")