Debian Codegraph @

pecan/tests/test_generic.py

raw
1 from json import dumps
2 from webtest import TestApp
3
4 from pecan import Pecan, expose, abort
5 from pecan.tests import PecanTestCase
6
7
8 class TestGeneric(PecanTestCase):
9
10 def test_simple_generic(self):
11 class RootController(object):
12 @expose(generic=True)
13 def index(self):
14 pass
15
16 @index.when(method='POST', template='json')
17 def do_post(self):
18 return dict(result='POST')
19
20 @index.when(method='GET')
21 def do_get(self):
22 return 'GET'
23
24 app = TestApp(Pecan(RootController()))
25 r = app.get('/')
26 assert r.status_int == 200
27 assert r.body == b'GET'
28
29 r = app.post('/')
30 assert r.status_int == 200
31 assert r.body == dumps(dict(result='POST')).encode('utf-8')
32
33 r = app.get('/do_get', status=404)
34 assert r.status_int == 404
35
36 def test_generic_allow_header(self):
37 class RootController(object):
38 @expose(generic=True)
39 def index(self):
40 abort(405)
41
42 @index.when(method='POST', template='json')
43 def do_post(self):
44 return dict(result='POST')
45
46 @index.when(method='GET')
47 def do_get(self):
48 return 'GET'
49
50 @index.when(method='PATCH')
51 def do_patch(self):
52 return 'PATCH'
53
54 app = TestApp(Pecan(RootController()))
55 r = app.delete('/', expect_errors=True)
56 assert r.status_int == 405
57 assert r.headers['Allow'] == 'GET, PATCH, POST'
58
59 def test_nested_generic(self):
60
61 class SubSubController(object):
62 @expose(generic=True)
63 def index(self):
64 return 'GET'
65
66 @index.when(method='DELETE', template='json')
67 def do_delete(self, name, *args):
68 return dict(result=name, args=', '.join(args))
69
70 class SubController(object):
71 sub = SubSubController()
72
73 class RootController(object):
74 sub = SubController()
75
76 app = TestApp(Pecan(RootController()))
77 r = app.get('/sub/sub/')
78 assert r.status_int == 200
79 assert r.body == b'GET'
80
81 r = app.delete('/sub/sub/joe/is/cool')
82 assert r.status_int == 200
83 assert r.body == dumps(
84 dict(result='joe', args='is, cool')
85 ).encode('utf-8')
86
87
88 class TestGenericWithSpecialMethods(PecanTestCase):
89
90 def test_generics_not_allowed(self):
91
92 class C(object):
93
94 def _default(self):
95 pass
96
97 def _lookup(self):
98 pass
99
100 def _route(self):
101 pass
102
103 for method in (C._default, C._lookup, C._route):
104 self.assertRaises(
105 ValueError,
106 expose(generic=True),
107 getattr(method, '__func__', method)
108 )