]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Finish small unit test refactor of API v2 tests
authorKevin Benton <blak111@gmail.com>
Thu, 11 Sep 2014 11:26:03 +0000 (04:26 -0700)
committerKevin Benton <kevinbenton@buttewifi.com>
Tue, 23 Sep 2014 09:23:26 +0000 (09:23 +0000)
Leverage a utility function for all of the tests
that create something in the API and expect a failure.

Change-Id: Iedb6ba35d637dda5ae9f553d0c7ffcb7c526f3c6

neutron/tests/unit/test_api_v2.py

index e2ee2aacb486077a4debc48ffef11e21710714f8..b3a3378b1ba64eeff537670698add8ff73f0c717 100644 (file)
@@ -791,11 +791,7 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase):
 
     def test_create_no_keystone_env(self):
         data = {'name': 'net1'}
-        res = self.api.post(_get_path('networks', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True)
-        self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
+        self._test_create_failure_bad_request('networks', data)
 
     def test_create_with_keystone_env(self):
         tenant_id = _uuid()
@@ -827,45 +823,25 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase):
         tenant_id = _uuid()
         data = {'network': {'name': 'net1', 'tenant_id': tenant_id}}
         env = {'neutron.context': context.Context('', tenant_id + "bad")}
-        res = self.api.post(_get_path('networks', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True,
-                            extra_environ=env)
-        self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
+        self._test_create_failure_bad_request('networks', data,
+                                              extra_environ=env)
 
     def test_create_no_body(self):
         data = {'whoa': None}
-        res = self.api.post(_get_path('networks', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True)
-        self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
+        self._test_create_failure_bad_request('networks', data)
 
     def test_create_no_resource(self):
         data = {}
-        res = self.api.post(_get_path('networks', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True)
-        self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
+        self._test_create_failure_bad_request('networks', data)
 
     def test_create_missing_attr(self):
         data = {'port': {'what': 'who', 'tenant_id': _uuid()}}
-        res = self.api.post(_get_path('ports', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True)
-        self.assertEqual(res.status_int, 400)
+        self._test_create_failure_bad_request('ports', data)
 
     def test_create_readonly_attr(self):
         data = {'network': {'name': 'net1', 'tenant_id': _uuid(),
                             'status': "ACTIVE"}}
-        res = self.api.post(_get_path('networks', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True)
-        self.assertEqual(res.status_int, 400)
+        self._test_create_failure_bad_request('networks', data)
 
     def test_create_bulk(self):
         data = {'networks': [{'name': 'net1',
@@ -888,38 +864,28 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase):
                             content_type='application/' + self.fmt)
         self.assertEqual(res.status_int, exc.HTTPCreated.code)
 
-    def _test_create_bulk_failure(self, resource, data):
-        # TODO(kevinbenton): update the rest of the failure cases to use
-        # this.
+    def _test_create_failure_bad_request(self, resource, data, **kwargs):
         res = self.api.post(_get_path(resource, fmt=self.fmt),
                             self.serialize(data),
                             content_type='application/' + self.fmt,
-                            expect_errors=True)
+                            expect_errors=True, **kwargs)
         self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
 
     def test_create_bulk_networks_none(self):
-        self._test_create_bulk_failure('networks', {'networks': None})
+        self._test_create_failure_bad_request('networks', {'networks': None})
 
     def test_create_bulk_networks_empty_list(self):
-        self._test_create_bulk_failure('networks', {'networks': []})
+        self._test_create_failure_bad_request('networks', {'networks': []})
 
     def test_create_bulk_missing_attr(self):
         data = {'ports': [{'what': 'who', 'tenant_id': _uuid()}]}
-        res = self.api.post(_get_path('ports', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True)
-        self.assertEqual(res.status_int, 400)
+        self._test_create_failure_bad_request('ports', data)
 
     def test_create_bulk_partial_body(self):
         data = {'ports': [{'device_id': 'device_1',
                            'tenant_id': _uuid()},
                           {'tenant_id': _uuid()}]}
-        res = self.api.post(_get_path('ports', fmt=self.fmt),
-                            self.serialize(data),
-                            content_type='application/' + self.fmt,
-                            expect_errors=True)
-        self.assertEqual(res.status_int, 400)
+        self._test_create_failure_bad_request('ports', data)
 
     def test_create_attr_not_specified(self):
         net_id = _uuid()