/
tests.py
155 lines (119 loc) · 7.99 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import unittest
from pyramid import testing
from pyramid_handlers import includeme as handlers_includeme, action
from pyramid.config import Configurator
from pyramid_routehelper import includeme as routehelper_includeme, add_resource
from pyramid.url import route_path
class Test_add_resource(unittest.TestCase):
def _create_config(self, autocommit=True):
config = Configurator(autocommit=autocommit)
handlers_includeme(config)
routehelper_includeme(config)
return config
def setUp(self):
self.config = self._create_config()
def tearDown(self):
self.config.end()
del self.config
def test_basic_resources(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler', 'message', 'messages')
self.config.begin()
assert route_path('messages', testing.DummyRequest()) == '/messages'
assert route_path('formatted_messages', testing.DummyRequest(), format='html') == '/messages.html'
assert route_path('new_message', testing.DummyRequest()) == '/messages/new'
assert route_path('formatted_new_message', testing.DummyRequest(), format='html') == '/messages/new.html'
assert route_path('formatted_message', testing.DummyRequest(), id=1, format='html') == '/messages/1.html'
assert route_path('message', testing.DummyRequest(), id=1) == '/messages/1'
assert route_path('formatted_edit_message', testing.DummyRequest(), id=1, format='html') == '/messages/1/edit.html'
assert route_path('edit_message', testing.DummyRequest(), id=1) == '/messages/1/edit'
def test_resources_with_path_prefix(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler', 'message', 'messages', path_prefix='/category/:category_id')
self.config.begin()
assert route_path('messages', testing.DummyRequest(), category_id=2) == '/category/2/messages'
assert route_path('formatted_messages', testing.DummyRequest(), format='html', category_id=2) == '/category/2/messages.html'
assert route_path('new_message', testing.DummyRequest(), category_id=2) == '/category/2/messages/new'
assert route_path('formatted_new_message', testing.DummyRequest(), format='html', category_id=2) == '/category/2/messages/new.html'
assert route_path('formatted_message', testing.DummyRequest(), id=1, format='html', category_id=2) == '/category/2/messages/1.html'
assert route_path('message', testing.DummyRequest(), id=1, category_id=2) == '/category/2/messages/1'
assert route_path('formatted_edit_message', testing.DummyRequest(), id=1, format='html', category_id=2) == '/category/2/messages/1/edit.html'
assert route_path('edit_message', testing.DummyRequest(), id=1, category_id=2) == '/category/2/messages/1/edit'
def test_resources_with_path_prefix_with_trailing_slash(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler', 'message', 'messages', path_prefix='/category/:category_id/')
self.config.begin()
assert route_path('messages', testing.DummyRequest(), category_id=2) == '/category/2/messages'
assert route_path('formatted_messages', testing.DummyRequest(), format='html', category_id=2) == '/category/2/messages.html'
assert route_path('new_message', testing.DummyRequest(), category_id=2) == '/category/2/messages/new'
assert route_path('formatted_new_message', testing.DummyRequest(), format='html', category_id=2) == '/category/2/messages/new.html'
assert route_path('formatted_message', testing.DummyRequest(), id=1, format='html', category_id=2) == '/category/2/messages/1.html'
assert route_path('message', testing.DummyRequest(), id=1, category_id=2) == '/category/2/messages/1'
assert route_path('formatted_edit_message', testing.DummyRequest(), id=1, format='html', category_id=2) == '/category/2/messages/1/edit.html'
assert route_path('edit_message', testing.DummyRequest(), id=1, category_id=2) == '/category/2/messages/1/edit'
def test_resources_with_collection_action(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler', 'message', 'messages', collection=dict(sorted='GET'))
self.config.begin()
assert route_path('sorted_messages', testing.DummyRequest()) == '/messages/sorted'
def test_resources_with_member_action(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler', 'message', 'messages', member=dict(comment='GET'))
self.config.begin()
assert route_path('comment_message', testing.DummyRequest(), id=1) == '/messages/1/comment'
def test_resources_with_new_action(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler', 'message', 'messages', new=dict(preview='GET'))
self.config.begin()
assert route_path('preview_new_message', testing.DummyRequest(), id=1) == '/messages/new/preview'
def test_resources_with_name_prefix(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler', 'message', 'messages', name_prefix="special_")
self.config.begin()
assert route_path('special_message', testing.DummyRequest(), id=1) == '/messages/1'
def test_resources_with_parent_resource(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler',
'message', 'messages',
parent_resource = dict(member_name='category', collection_name='categories'))
self.config.begin()
assert route_path('category_messages', testing.DummyRequest(), category_id=2) == '/categories/2/messages'
assert route_path('category_message', testing.DummyRequest(), category_id=2, id=1) == '/categories/2/messages/1'
def test_resources_with_parent_resource_override_path_prefix(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler',
'message', 'messages',
parent_resource = dict(member_name='category', collection_name='categories'),
path_prefix = 'folders/:folder_id')
self.config.begin()
assert route_path('category_messages', testing.DummyRequest(), folder_id=2) == '/folders/2/messages'
assert route_path('category_message', testing.DummyRequest(), folder_id=2, id=1) == '/folders/2/messages/1'
def test_resources_with_parent_resource_override_name_prefix(self):
self.config.add_resource('pyramid_routehelper.tests:DummyCrudHandler',
'message', 'messages',
parent_resource = dict(member_name='category', collection_name='categories'),
name_prefix = '')
self.config.begin()
assert route_path('messages', testing.DummyRequest(), category_id=2) == '/categories/2/messages'
assert route_path('message', testing.DummyRequest(), category_id=2, id=1) == '/categories/2/messages/1'
class Test_includeme(unittest.TestCase):
def test_includme(self):
config = Configurator(autocommit=True)
handlers_includeme(config)
routehelper_includeme(config)
assert config.add_resource.im_func.__docobj__ is add_resource
class DummyCrudHandler(object):
def __init__(self, request):
self.request = request
@action(renderer='json')
def index(self):
return "index"
@action(renderer='json')
def create(self):
return "create"
@action(renderer='json')
def read(self):
return "read"
@action(renderer='json')
def update(self):
return "update"
@action(renderer='json')
def delete(self):
return "delete"
@action(renderer='json')
def new(self):
return "new"
@action(renderer='json')
def edit(self):
return "edit"