Commits

Carles Barrobés committed e44fb18

Go back to using 'fields' instead of 'field_types', since now we do not call filter_response for OPTIONS, which was failing. Refactored OPTIONS tests

  • Participants
  • Parent commits fe18b7f

Comments (0)

Files changed (2)

djangorestframework/tests/views.py

         parser = JSONParser(None)
         (data, files) = parser.parse(StringIO(response.content))
         self.assertIn('application/json', data['renders']) 
-        self.assertEqual('Mock', data['name']) 
-        self.assertEqual('This is a basic mock view', data['description']) 
-        self.assertEqual(data.get('field_types'), None)
+        self._verify_options_response(response, 
+                                      name='Mock', 
+                                      description='This is a basic mock view')
         
     def test_options_method_resource_view(self):
         response = self.client.options('/resourcemock/')
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response['Content-Type'].split(';')[0], 'application/json')        
-        parser = JSONParser(None)
-        (data, files) = parser.parse(StringIO(response.content))
-        self.assertIn('application/json', data['renders']) 
-        self.assertEqual('Resource Mock', data['name']) 
-        self.assertEqual('This is a resource-based mock view', data['description']) 
-        fields = data['field_types']
-        self.assertEqual(len(fields), 3) 
-        self.assertEqual(fields['foo'], 'BooleanField')
-        self.assertEqual(fields['bar'], 'IntegerField') 
-        self.assertEqual(fields['baz'], 'CharField') 
+        self._verify_options_response(response, 
+                                      name='Resource Mock', 
+                                      description='This is a resource-based mock view', 
+                                      fields={'foo':'BooleanField', 
+                                              'bar':'IntegerField', 
+                                              'baz':'CharField', 
+                                              })
         
     def test_options_method_model_resource_list_view(self):
         response = self.client.options('/model/')
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response['Content-Type'].split(';')[0], 'application/json')
-        parser = JSONParser(None)
-        (data, files) = parser.parse(StringIO(response.content))
-
-        self.assertIn('application/json', data['renders']) 
-        self.assertEqual('Mock List', data['name']) 
-        self.assertEqual('This is a mock model-based resource', data['description']) 
-        fields = data['field_types']
-        self.assertEqual(len(fields), 3) 
-        self.assertEqual(fields['foo'], 'BooleanField')
-        self.assertEqual(fields['bar'], 'IntegerField') 
-        self.assertEqual(fields['baz'], 'CharField') 
+        self._verify_options_response(response, 
+                                      name='Mock List', 
+                                      description='This is a mock model-based resource', 
+                                      fields={'foo':'BooleanField', 
+                                              'bar':'IntegerField', 
+                                              'baz':'CharField', 
+                                              })
 
     def test_options_method_model_resource_detail_view(self):
         response = self.client.options('/model/0/')
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response['Content-Type'].split(';')[0], 'application/json')
+        self._verify_options_response(response, 
+                                      name='Mock Instance', 
+                                      description='This is a mock model-based resource', 
+                                      fields={'foo':'BooleanField', 
+                                              'bar':'IntegerField', 
+                                              'baz':'CharField', 
+                                              })
+
+    def _verify_options_response(self, response, name, description, fields=None):
         parser = JSONParser(None)
         (data, files) = parser.parse(StringIO(response.content))
-
         self.assertIn('application/json', data['renders']) 
-        self.assertEqual('Mock Instance', data['name']) 
-        self.assertEqual('This is a mock model-based resource', data['description']) 
-        fields = data['field_types']
-        self.assertEqual(len(fields), 3) 
-        self.assertEqual(fields['foo'], 'BooleanField')
-        self.assertEqual(fields['bar'], 'IntegerField') 
-        self.assertEqual(fields['baz'], 'CharField') 
+        self.assertEqual(name, data['name']) 
+        self.assertEqual(description, data['description']) 
+        if fields is None:
+            self.assertFalse(hasattr(data, 'fields'))
+        else:
+            self.assertEqual(data['fields'], fields) 
 
 
 class ExtraViewsTests(TestCase):

djangorestframework/views.py

             field_name_types = {}
             for name, field in form.fields.iteritems():
                 field_name_types[name] = field.__class__.__name__
-            response_obj['field_types'] = field_name_types
+            response_obj['fields'] = field_name_types
         return response_obj