コード例 #1
0
 def test_get_handler_name_route_heading(self):
     """
     Tests that we get the handler name using the route heading for a non
     list endpoint.
     """
     route = Route('/', (put(update_foo), ), heading='Dinosaur (v1)')
     assert 'DinosaurV1Handler' == get_handler_name(route, update_foo)
コード例 #2
0
 def test_put(self):
     expected = HTTPMethod('put',
                           get_foo,
                           allowed_exceptions=[ValueError],
                           title='Title')
     actual = put(get_foo, allowed_exceptions=[ValueError], title='Title')
     assert actual.method == expected.method
     assert actual.logic._doctor_title == 'Title'
     assert actual.logic._doctor_allowed_exceptions == [ValueError]
コード例 #3
0

def status() -> Status:
    return 'Notes API v1.0.0'


# -- mark-routes

routes = (
    Route('/', methods=(get(status), ), heading='API Status'),
    Route('/note/',
          methods=(get(get_notes, title='Retrieve List'), post(create_note)),
          handler_name='NoteListHandler',
          heading='Notes (v1)'),
    Route('/note/<int:note_id>/',
          methods=(delete(delete_note), get(get_note), put(update_note)),
          heading='Notes (v1)'),
)

# -- mark-app

app = Flask('Doctor example')

api = Api(app)
for route, resource in create_routes(routes):
    api.add_resource(resource, route)

if __name__ == '__main__':
    app.run(debug=True)

# -- mark-end
コード例 #4
0
    def test_create_routes(self):
        class MyHandler(Resource):
            pass

        routes = (
            Route('^/foo/?$',
                  (get(get_foos, title='Retrieve List'), post(create_foo)),
                  base_handler_class=MyHandler,
                  handler_name='MyHandler',
                  heading='Foo'),
            Route('^/foo/<int:foo_id>/?$',
                  (delete(delete_foo), get(get_foo), put(update_foo)),
                  heading='Foo'),
            Route('^/foos/?$', (put(lambda: 'put'), ), heading='Foo'),
        )
        actual = create_routes(routes, handle_http, Resource)

        # 2 routes created
        assert 3 == len(actual)

        # verify the first route
        route, handler = actual[0]
        assert r'^/foo/?$' == route

        # verify it's an instance of our base handler class.
        assert issubclass(handler, MyHandler)
        # verify it used our custom handler name
        assert 'MyHandler' == handler.__name__

        # verify each http method was added
        assert hasattr(handler, 'get')
        assert hasattr(handler, 'post')

        # verify heading attr was added to handler
        assert handler._doctor_heading == 'Foo'

        # verify params for get
        params = handler.get._doctor_params
        expected = Params(all=['is_alive'],
                          required=[],
                          optional=['is_alive'],
                          logic=['is_alive'])
        assert expected == params

        # verify signature
        sig = handler.get._doctor_signature
        expected = inspect.signature(get_foos)
        assert expected == sig

        # verify custom title
        assert 'Retrieve List' == handler.get._doctor_title

        # verify params for post
        params = handler.post._doctor_params
        expected = Params(all=['name'],
                          required=['name'],
                          optional=[],
                          logic=['name'])
        assert expected == params

        # verify signature
        sig = handler.post._doctor_signature
        expected = inspect.signature(create_foo)
        assert expected == sig

        # verify the second route
        route, handler = actual[1]
        assert '^/foo/<int:foo_id>/?$' == route
        # verify each http method was added
        assert hasattr(handler, 'get')
        assert hasattr(handler, 'delete')
        assert hasattr(handler, 'put')
        # verify it generated an appropriate class handler name
        assert 'FooHandler' == handler.__name__

        # Verify the 3rd handler which would have had a conflicting handler
        # name has a number appended to the end of it.
        route, handler = actual[2]
        assert 'FooHandler2' == handler.__name__