@@ -177,7 +177,7 @@ def test_defaults(self):
177
177
def test_action (request ):
178
178
"""Description"""
179
179
180
- assert test_action .bind_to_methods == [ 'get' ]
180
+ assert test_action .mapping == { 'get' : 'test_action' }
181
181
assert test_action .detail is True
182
182
assert test_action .name == 'Test action'
183
183
assert test_action .url_path == 'test_action'
@@ -191,15 +191,69 @@ def test_detail_required(self):
191
191
with pytest .raises (AssertionError ) as excinfo :
192
192
@action ()
193
193
def test_action (request ):
194
- pass
194
+ raise NotImplementedError
195
195
196
196
assert str (excinfo .value ) == "@action() missing required argument: 'detail'"
197
197
198
+ def test_method_mapping_http_methods (self ):
199
+ # All HTTP methods should be mappable
200
+ @action (detail = False , methods = [])
201
+ def test_action ():
202
+ raise NotImplementedError
203
+
204
+ for name in APIView .http_method_names :
205
+ def method ():
206
+ return name
207
+
208
+ # Python 2.x compatibility - cast __name__ to str
209
+ method .__name__ = str (name )
210
+ getattr (test_action .mapping , name )(method )
211
+
212
+ # ensure the mapping returns the correct method name
213
+ for name in APIView .http_method_names :
214
+ assert test_action .mapping [name ] == name
215
+
216
+ def test_method_mapping (self ):
217
+ @action (detail = False )
218
+ def test_action (request ):
219
+ raise NotImplementedError
220
+
221
+ @test_action .mapping .post
222
+ def test_action_post (request ):
223
+ raise NotImplementedError
224
+
225
+ # The secondary handler methods should not have the action attributes
226
+ for name in ['mapping' , 'detail' , 'name' , 'url_path' , 'url_name' , 'kwargs' ]:
227
+ assert hasattr (test_action , name ) and not hasattr (test_action_post , name )
228
+
229
+ def test_method_mapping_already_mapped (self ):
230
+ @action (detail = True )
231
+ def test_action (request ):
232
+ raise NotImplementedError
233
+
234
+ msg = "Method 'get' has already been mapped to '.test_action'."
235
+ with self .assertRaisesMessage (AssertionError , msg ):
236
+ @test_action .mapping .get
237
+ def test_action_get (request ):
238
+ raise NotImplementedError
239
+
240
+ def test_method_mapping_overwrite (self ):
241
+ @action (detail = True )
242
+ def test_action ():
243
+ raise NotImplementedError
244
+
245
+ msg = ("Method mapping does not behave like the property decorator. You "
246
+ "cannot use the same method name for each mapping declaration." )
247
+ with self .assertRaisesMessage (AssertionError , msg ):
248
+ @test_action .mapping .post
249
+ def test_action ():
250
+ raise NotImplementedError
251
+
198
252
def test_detail_route_deprecation (self ):
199
253
with pytest .warns (PendingDeprecationWarning ) as record :
200
254
@detail_route ()
201
255
def view (request ):
202
- pass
256
+ raise NotImplementedError
203
257
204
258
assert len (record ) == 1
205
259
assert str (record [0 ].message ) == (
@@ -212,7 +266,7 @@ def test_list_route_deprecation(self):
212
266
with pytest .warns (PendingDeprecationWarning ) as record :
213
267
@list_route ()
214
268
def view (request ):
215
- pass
269
+ raise NotImplementedError
216
270
217
271
assert len (record ) == 1
218
272
assert str (record [0 ].message ) == (
@@ -226,7 +280,7 @@ def test_route_url_name_from_path(self):
226
280
with pytest .warns (PendingDeprecationWarning ):
227
281
@list_route (url_path = 'foo_bar' )
228
282
def view (request ):
229
- pass
283
+ raise NotImplementedError
230
284
231
285
assert view .url_path == 'foo_bar'
232
286
assert view .url_name == 'foo-bar'
0 commit comments