Skip to content

Commit

Permalink
Merge pull request #484 from karantan/add/traverse-support-483
Browse files Browse the repository at this point in the history
Add traverse support.
  • Loading branch information
leplatrem committed Apr 12, 2018
2 parents 7ae72e9 + d3aa20d commit 769402f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ CHANGELOG
3.4.0 (unreleased)
==================

- Nothing changed yet.
- Add traverse support. For more information regarding Hybrid Applications see
`Pyramid documentation <https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/hybrid.html>`_
(#483).


3.3.0 (2018-04-11)
Expand Down
3 changes: 0 additions & 3 deletions cornice/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ def __init__(self, name, path=None, description=None, cors_policy=None,
if hasattr(self, 'acl'):
raise ConfigurationError("'acl' is not supported")

if hasattr(self, 'traverse'):
raise ConfigurationError("'traverse' is not supported")

# instantiate some variables we use to keep track of what's defined for
# this service.
self.defined_methods = []
Expand Down
5 changes: 0 additions & 5 deletions tests/test_pyramidhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,6 @@ def test_route_with_prefix(self):
services = config.registry.cornice_services
self.assertTrue('/prefix/wrapperservice' in services)

def test_route_with_traverse_fails(self):
with self.assertRaises(ConfigurationError):
bad_service = Service(name="jardinet", path="/jardinet", traverse='/')
self.fail("Shouldn't happen")


class TestRouteFromPyramid(TestCase):

Expand Down
70 changes: 70 additions & 0 deletions tests/test_resource_traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from pyramid import testing
from webtest import TestApp

from cornice.resource import resource, view

from .support import TestCase, CatchErrors


FRUITS = {'1': {'name': 'apple'}, '2': {'name': 'orange'}}


class FruitFactory(object):

def __init__(self, request):
self.request = request

def __getitem__(self, key):
return FRUITS[key]


@resource(
collection_path='/fruits/',
collection_factory=FruitFactory,
collection_traverse='',
path='/fruits/{fruit_id}/',
factory=FruitFactory,
name='fruit_service',
traverse='/{fruit_id}'
)
class Fruit(object):

def __init__(self, request, context):
self.request = request
self.context = context

def collection_get(self):
return {'fruits': list(FRUITS.keys())}

@view(renderer='json')
def get(self):
return self.context


class TestResourceTraverse(TestCase):

def setUp(self):
self.config = testing.setUp()
self.config.include('cornice')

self.config.scan('tests.test_resource_traverse')
self.app = TestApp(CatchErrors(self.config.make_wsgi_app()))

def tearDown(self):
testing.tearDown()

def test_collection_traverse(self):
resp = self.app.get('/fruits/').json
self.assertEqual(sorted(resp['fruits']), ['1', '2'])

def test_traverse(self):
resp = self.app.get('/fruits/1/')
self.assertEqual(resp.json, {'name': 'apple'})

resp = self.app.get('/fruits/2/')
self.assertEqual(resp.json, {'name': 'orange'})

self.app.get('/fruits/3/', status=404)

0 comments on commit 769402f

Please sign in to comment.