예제 #1
0
def test_simple_parse_5_with_query_params():
    docstring = """This is the simple description.
With a second line.

Long description.
With a second line.

Query Parameters:
    param1 (int) -- The param 1.
    param2 (Model) -- Required. The param 2.
"""

    path_spec = parse_from_docstring(docstring)
    assert path_spec.summary == "This is the simple description.\nWith a second line.\n"
    assert path_spec.description == "Long description.\nWith a second line.\n"

    qp = path_spec.query_params.get("param1")
    assert qp is not None
    assert qp.name == "param1"
    assert qp.type.name == "integer"
    assert qp.ptype == "query"
    assert qp.required == False
    assert qp.description == "The param 1."

    qp2 = path_spec.query_params.get("param2")
    assert qp2 is not None
    assert qp2.name == "param2"
    assert qp2.type.name == "Model"
    assert qp2.required
    assert qp2.description == "The param 2."
예제 #2
0
def test_docstring_test():
    docstring = """Test get

        Hiho

        Cookie:
            x (string) -- some foo

        Path Params:
            emp_uid (int) -- test
            date (date) -- test

        200 Response:
            test (string) -- Test data
        
        Error Response:
            400  -- Fudge
        """
    path_spec = parse_from_docstring(docstring)
    assert path_spec.summary == "Test get\n"
    assert path_spec.description == "Hiho\n"
    assert path_spec.cookie_params
    assert path_spec.cookie_params.get('x')
    assert path_spec.path_params
    assert path_spec.path_params.get("emp_uid")
    assert path_spec.responses
    assert path_spec.responses.get('200')
    assert path_spec.responses.get('400')
예제 #3
0
def schema(cls):
    """REST API schema decorator"""
    name = cls.__name__

    #determine super class
    mro = inspect.getmro(cls)
    if not mro:
        mro = (cls,)

    mro = list(mro)
    mro.reverse()
    cls.schema_spec = []
    for item in mro:
        if item.__name__ == 'object':
            continue
        if item.__name__ == name:
            doc = inspect.getdoc(item)
            try:
                model_spec = docparser.parse_from_docstring(doc, spec='schema')
                if model_spec:
                    cls.schema_spec.append(model_spec)
                    settings.add_schema(name, cls)
                    if hasattr(cls, 'Meta'):
                        if hasattr(cls.Meta, 'example'):
                            model_spec.example = cls.Meta.example
                        if hasattr(cls.Meta, 'examples'):
                            model_spec.examples = cls.Meta.examples
            except:
                pass
        else: #if class name is a superclass append a ref.
            cls.schema_spec.append(Ref('#/components/schemas/{}'.format(item.__name__)))
    
    return cls
예제 #4
0
def test_simple_parse_2():
    docstring = """This is the simple description.

    Long description.
    """

    path_spec = parse_from_docstring(docstring)
    assert path_spec.summary == "This is the simple description.\n"
    assert path_spec.description == "Long description.\n"
예제 #5
0
def schema(cls):
    """REST API schema decorator"""
    name = cls.__name__
    doc = inspect.getdoc(cls)
    model_spec = docparser.parse_from_docstring(doc, spec='schema')
    if model_spec:
        cls.schema_spec = model_spec
        settings.add_schema(name, cls)
    return cls
예제 #6
0
def test_response_201():
    docstring = """Response 200

    201 Response:
        None  -- ACCEPTED
    """
    path_spec = parse_from_docstring(docstring)

    assert path_spec.responses
    response = path_spec.responses.get("201")  # response ids are the http code
    assert response
    assert response.description == "ACCEPTED"
예제 #7
0
def test_response_200_alternate_format():
    docstring = """Response 200

    200 Response:
        x (Model) -- Response 200
    """
    path_spec = parse_from_docstring(docstring)

    assert path_spec.responses
    response = path_spec.responses.get("200")  # response ids are the http code
    assert response
    assert response.description == "Response 200"
예제 #8
0
def test_square_brackets_in_name():
    docstring = """Test doc

    This is something

    Query Parameters:
        filter[job_id] (string) -- filter for job_id


    """
    path_spec = parse_from_docstring(docstring)
    assert path_spec.query_params
    assert path_spec.query_params.get('filter[job_id]')
예제 #9
0
def test_schema_properties():
    docstring = """Test schema

    This is something

    Properties:
        name (string) -- required.  The name.
        age (int) -- The age.

    """
    path_spec = parse_from_docstring(docstring)
    assert path_spec.properties

    assert path_spec.properties.get('name')
예제 #10
0
def test_cookie_section():
    docstring = """Cookie Monster

    Cookie:
        x (string) -- required.  Cookie monster raaa
    """
    path_spec = parse_from_docstring(docstring)

    assert path_spec.cookie_params
    cookie = path_spec.cookie_params.get("x")
    assert cookie is not None
    assert cookie.name == 'x'
    assert cookie.type.name == 'string'
    assert cookie.required
    assert cookie.description == 'Cookie monster raaa'
예제 #11
0
def test_error_responses():
    docstring = """Response 200

    Error Responses:
        400 -- {Not A Good Request}
        500 -- Hello
    """
    path_spec = parse_from_docstring(docstring)

    assert path_spec.responses
    response = path_spec.responses.get("400")  # response ids are the http code
    assert response
    assert response.description == "{Not A Good Request}"
    response = path_spec.responses.get("500")  # response ids are the http code
    assert response
    assert response.description == "Hello"
예제 #12
0
    def _real_decorator(cls):
        cls.rest_api = True
        cls.tagged_api_comps = []
        members = inspect.getmembers(cls, is_rest_api_method)

        for name, member in members:
            doc = inspect.getdoc(member)
            if not doc:
                continue
            path_spec = docparser.parse_from_docstring(str(doc))
            if path_spec:
                setattr(member, 'path_spec', path_spec)
                cls.tagged_api_comps.append(name)
        settings.add_api_handler(cls)
        settings.add_route(url, cls, **kwargs)
        return cls
예제 #13
0
def test_simple_parse_6_with_body_params_and_headers_array_of_ints():
    docstring = """This is the simple description.
With a second line.

Long description.
With a second line.

Headers:
    Authorization -- Required. the login.

Query Parameters:
    param1 ([int]) -- The param 1.
    param2 (Model) -- Required. The param 2.

Request Body:
    test (Model) -- Required.  This is the bomb.
"""

    path_spec = parse_from_docstring(docstring)

    qp = path_spec.query_params.get("param1")
    assert qp is not None
    assert qp.name == "param1"
    assert qp.type.name == "array"
    assert qp.type.items_type.name == "integer"
    assert qp.ptype == "query"

    assert qp.required == False
    assert qp.description == "The param 1."

    qp2 = path_spec.query_params.get("param2")
    assert qp2 is not None
    assert qp2.name == "param2"
    assert qp2.type.name == "Model"
    assert qp2.required
    assert qp2.description == "The param 2."

    body_params = path_spec.body_params
    assert body_params is not None

    hp = path_spec.header_params.get("Authorization")
    assert hp is not None
    assert hp.name == "Authorization"
    assert hp.type.name == "string"
    assert hp.required
예제 #14
0
def test_param_props():
    docstring = """Test schema

    This is something

    Properties:
        name (string) -- required.  The name.
        age (int) -- The age.
            minimum: 1
            maximum: 200


    """
    path_spec = parse_from_docstring(docstring)
    assert path_spec.properties

    assert path_spec.properties.get('name')
    assert path_spec.properties.get('age')
    assert path_spec.properties.get('age').description.strip() == 'The age.'
    assert path_spec.properties.get('age').type.kwargs.get('minimum') == 1
예제 #15
0
def test_simple_parse_4_with_path_params():
    docstring = """This is the simple description.
With a second line.

Long description.
With a second line.

Path Parameters:
    employee_uid (int) -- The employee ID.
"""

    path_spec = parse_from_docstring(docstring)
    assert path_spec.summary == "This is the simple description.\nWith a second line.\n"
    assert path_spec.description == "Long description.\nWith a second line.\n"

    pp = path_spec.path_params.get('employee_uid')
    assert pp.name == 'employee_uid'
    assert pp.type.name == 'integer'
    assert pp.ptype == "path"
    assert pp.description == 'The employee ID.'
    assert pp.required == True
예제 #16
0
def test_simple_parse_1():
    docstring = """This is the simple description"""

    path_spec = parse_from_docstring(docstring)
    assert isinstance(path_spec, PathSpec)
    assert path_spec.summary == "This is the simple description"