Beispiel #1
0
    def test_create_routes_reuse_logic_different_title(self):
        """
        This tests that if we want to reuse a logic function with a different
        route, and give it a unique title that it persists.  Previously if you
        defined a title for 2 different routes using the same logic function,
        it would use the last defined title on both routes.
        """
        routes = (
            Route('^/foo/?$',
                  methods=[get(get_foos, title='Retrieve List')],
                  heading='Foo'),
            Route('^/bar/?$',
                  methods=[get(get_foos, title='Retrieve Other List')],
                  heading='Foo'),
        )
        actual = create_routes(routes, handle_http, Resource)

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

        # verify the second route
        route, handler = actual[1]
        assert r'^/bar/?$' == route
        assert 'Retrieve Other List' == handler.get._doctor_title
Beispiel #2
0
 def test_get(self):
     expected = HTTPMethod('get',
                           get_foo,
                           allowed_exceptions=[ValueError],
                           title='Title')
     actual = get(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]
Beispiel #3
0
    def get_routes(self):
        """Gets the routes for the app.

        This should return a tuple of tuples.  Each tuple contains the
        route as a string and the instnace of a resource to route to.  e.g.

        (
            ('/some/url/', doctor.router.Handler1),
        )

        This method can be overridden in subclasses to change the routes and
        add new ones.

        :returns: The tuple described above.
        """
        def logic_func(item_id: ItemId, name: Name=None):
            return (item_id, name)

        routes = (
            Route('/test/', methods=(
                get(logic_func),), heading='Test'),
        )
        return create_routes(routes)

def delete_note(note_id: NoteId):
    """Delete an existing note."""
    if note_id != 1:
        raise NotFoundError('Note does not exist')


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):
Beispiel #5
0
 def test_get_handler_name_from_logic_function_name(self):
     """Tests handler names is generated from logic func name."""
     route = Route('/', (get(get_foos), ))
     assert 'GetFoosHandler' == get_handler_name(route, get_foos)
Beispiel #6
0
 def test_get_handler_name_route_has_handler_name(self):
     """Tests handler name comes from one defined on Route"""
     route = Route('/', (get(get_foos), ), handler_name='FooFooHandler')
     assert 'FooFooHandler' == get_handler_name(route, get_foos)
Beispiel #7
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__