Exemplo n.º 1
0
    def test_similarly_named_deeply_customized_member_params(self):
        routes = list(resource(
            UserResource, member_param='<string:slug>', subresources=[
                resource(RoleResource, member_param='<int:id>', subresources=[
                    resource(FooResource, member_param='<string:slug>', subresources=[
                        resource(BarResource, member_param='<int:id>')
                    ])
                ])
            ]))

        assert routes[0].endpoint == 'user_resource.list'
        assert routes[0].rule == '/users'
        assert routes[1].endpoint == 'user_resource.get'
        assert routes[1].rule == routes[0].rule + '/<string:slug>'
        assert routes[2].endpoint == 'role_resource.list'
        assert routes[2].rule == '/users/<string:user_slug>/roles'
        assert routes[3].endpoint == 'role_resource.get'
        assert routes[3].rule == routes[2].rule + '/<int:id>'
        assert routes[4].endpoint == 'foo_resource.list'
        assert routes[4].rule == '/users/<string:user_slug>/roles/<int:role_id>/foos'
        assert routes[5].endpoint == 'foo_resource.get'
        assert routes[5].rule == routes[4].rule + '/<string:slug>'
        assert routes[6].endpoint == 'bar_resource.list'
        assert routes[6].rule == \
           '/users/<string:user_slug>/roles/<int:role_id>/foos/<string:foo_slug>/bars'
        assert routes[7].endpoint == 'bar_resource.get'
        assert routes[7].rule == routes[6].rule + '/<int:id>'
Exemplo n.º 2
0
    def test_it_requires_a_controller(self):
        with pytest.raises(ValueError):
            list(resource('/fail'))

        with pytest.raises(ValueError):
            list(resource('/fail', None))

        with pytest.raises(ValueError):
            list(resource(None))

        with pytest.raises(ValueError):
            list(resource(None, UserResource))
Exemplo n.º 3
0
    def test_it_renames_with_deeply_customized_unique_member_params(self):
        routes = list(resource('/baz', BazResource,
                               subresources=[resource(BarResource)]))

        assert routes[0].endpoint == 'baz_resource.list'
        assert routes[0].rule == '/baz'
        assert routes[1].endpoint == 'baz_resource.get'
        assert routes[1].rule == '/baz/<int:id>'
        assert routes[2].endpoint == 'bar_resource.list'
        assert routes[2].rule == '/baz/<string:baz>/bars'
        assert routes[3].endpoint == 'bar_resource.get'
        assert routes[3].rule == '/baz/<string:baz>/bars/<int:id>'
Exemplo n.º 4
0
 def test_it_works_with_automatic_unique_member_params(self):
     routes = list(resource('/foo', FooResource, subresources=[
         resource(BarResource),
     ]))
     assert routes[0].endpoint == 'foo_resource.list'
     assert routes[0].rule == '/foo'
     assert routes[1].endpoint == 'foo_resource.get'
     assert routes[1].rule == '/foo/<int:id>'
     assert routes[2].endpoint == 'bar_resource.list'
     assert routes[2].rule == '/foo/<int:foo_id>/bars'
     assert routes[3].endpoint == 'bar_resource.get'
     assert routes[3].rule == '/foo/<int:foo_id>/bars/<int:id>'
Exemplo n.º 5
0
    def test_member_param_overrides_unique_member_params(self):
        routes = list(resource('/baz', BazResource, unique_member_param='<int:pk>',
                               subresources=[
                                   resource(BarResource, member_param='<int:pk>')]))

        assert routes[0].endpoint == 'baz_resource.list'
        assert routes[0].rule == '/baz'
        assert routes[1].endpoint == 'baz_resource.get'
        assert routes[1].rule == '/baz/<int:id>'
        assert routes[2].endpoint == 'bar_resource.list'
        assert routes[2].rule == '/baz/<int:pk>/bars'
        assert routes[3].endpoint == 'bar_resource.get'
        assert routes[3].rule == '/baz/<int:baz_pk>/bars/<int:pk>'
Exemplo n.º 6
0
    def test_differently_named_deeply_customized_member_params(self):
        routes = list(resource(UserResource,
                               member_param='<string:slug>',
                               subresources=[resource(RoleResource,
                                                      member_param='<string:slug2>')]))

        assert routes[0].endpoint == 'user_resource.list'
        assert routes[0].rule == '/users'
        assert routes[1].endpoint == 'user_resource.get'
        assert routes[1].rule == '/users/<string:slug>'
        assert routes[2].endpoint == 'role_resource.list'
        assert routes[2].rule == '/users/<string:user_slug>/roles'
        assert routes[3].endpoint == 'role_resource.get'
        assert routes[3].rule == '/users/<string:user_slug>/roles/<string:slug2>'
Exemplo n.º 7
0
    def test_it_does_not_mutate_subresource_routes(self):
        routes = list(resource('/one', UserResource, subresources=[
            resource('/two', RoleResource)
        ]))
        orig_routes = [route
                       for routes in getattr(RoleResource,
                                             CONTROLLER_ROUTES_ATTR).values()
                       for route in routes]
        assert orig_routes[0].endpoint == routes[2].endpoint
        assert orig_routes[1].endpoint == routes[3].endpoint

        assert orig_routes[0].rule == '/'
        assert orig_routes[1].rule == '/<int:id>'
        assert routes[2].rule == '/one/<int:user_id>/two'
        assert routes[3].rule == '/one/<int:user_id>/two/<int:id>'
Exemplo n.º 8
0
 def test_it_works_with_a_customized_member_param(self):
     routes = list(resource(UserResource, member_param='<string:slug>'))
     assert len(routes) == 2
     assert routes[0].endpoint == 'user_resource.list'
     assert routes[0].rule == '/users/'
     assert routes[1].endpoint == 'user_resource.get'
     assert routes[1].rule == '/users/<string:slug>'
Exemplo n.º 9
0
 def test_it_works_with_a_prefix(self):
     routes = list(resource('/prefix', UserResource))
     assert len(routes) == 2
     assert routes[0].endpoint == 'user_resource.list'
     assert routes[0].rule == '/prefix/'
     assert routes[1].endpoint == 'user_resource.get'
     assert routes[1].rule == '/prefix/<int:id>'
Exemplo n.º 10
0
 def test_it_works_with_only_resource(self):
     routes = list(resource(UserResource))
     assert len(routes) == 2
     assert routes[0].endpoint == 'user_resource.list'
     assert routes[0].rule == '/users/'
     assert routes[1].endpoint == 'user_resource.get'
     assert routes[1].rule == '/users/<int:id>'
Exemplo n.º 11
0
 def test_it_does_not_mutate_existing_routes(self):
     routes = list(resource('/prefix', UserResource))
     orig_routes = [route
                    for routes in getattr(UserResource,
                                          CONTROLLER_ROUTES_ATTR).values()
                    for route in routes]
     assert orig_routes[0].endpoint == routes[0].endpoint
     assert orig_routes[0].rule == '/'
     assert routes[0].rule == '/prefix'