def test_add_rule_with_exsiting_endpoint(self): class Dummy(Resource): name = 'dummy' api = RESTArt() api.add_rule(Dummy, '/dummy', 'dummy') with pytest.raises(AssertionError): api.add_rule(Dummy, '/dummy', 'dummy')
def test_add_rule_with_invalid_format_suffix(self): class Dummy(Resource): name = 'dummy' api = RESTArt() with pytest.raises(AssertionError) as exc: api.add_rule_with_format_suffix(Dummy, '/dummy', 'dummy', format_suffix='enabled') assert str(exc.value) == ("`format_suffix` must be one of " "('disabled', 'optional', 'mandatory')")
def test_add_rule(self): class Dummy(Resource): name = 'dummy' api = RESTArt() api.add_rule(Dummy, '/dummy', 'dummy') assert 'dummy' in api.rules rule = api.rules['dummy'] self.assert_rule(rule, ('/dummy', config.ACTION_MAP.keys(), Dummy))
def test_add_rule_with_mandatory_format_suffix(self): class Dummy(Resource): name = 'dummy' api = RESTArt() api.add_rule_with_format_suffix(Dummy, '/dummy', 'dummy', format_suffix='mandatory') assert 'dummy' not in api.rules assert 'dummy_format' in api.rules rule = api.rules['dummy_format'] self.assert_rule(rule, ('/dummy.<format>', config.ACTION_MAP.keys(), Dummy))
from __future__ import absolute_import from restart import status from restart.api import RESTArt from restart.resource import Resource from restart.exceptions import NotFound from restart.utils import make_location_header api = RESTArt() todos = {1: {'id': 1, 'name': 'work'}, 2: {'id': 2, 'name': 'sleep'}} @api.register(pk='<int:todo_id>') class Todos(Resource): name = 'todos' def index(self, request): return todos.values() def create(self, request): todo_id = max(todos.keys()) + 1 item = dict(id=todo_id, **request.data) todos[todo_id] = item headers = {'Location': make_location_header(request, todo_id)} return {'id': todo_id}, status.HTTP_201_CREATED, headers def read(self, request, todo_id): try: return todos[todo_id] except KeyError: