You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by an...@apache.org on 2017/04/13 09:33:25 UTC
[19/46] libcloud git commit: fix the use of unittest.TestCase as a
mixin for MockHttp
fix the use of unittest.TestCase as a mixin for MockHttp
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/f33c126c
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/f33c126c
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/f33c126c
Branch: refs/heads/trunk
Commit: f33c126cd2acd6f6d143f4768d59aefe5aa32471
Parents: b1dde4e
Author: Anthony Shaw <an...@apache.org>
Authored: Wed Apr 12 10:34:15 2017 +1000
Committer: Anthony Shaw <an...@apache.org>
Committed: Wed Apr 12 10:34:15 2017 +1000
----------------------------------------------------------------------
libcloud/common/base.py | 1 -
libcloud/test/__init__.py | 41 ++++++++--------------------
libcloud/test/compute/__init__.py | 4 +--
libcloud/test/compute/test_openstack.py | 13 ++++-----
libcloud/test/loadbalancer/test_slb.py | 2 +-
setup.py | 2 --
6 files changed, 20 insertions(+), 43 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/libcloud/blob/f33c126c/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index 244ccfe..9c5731e 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -236,7 +236,6 @@ class XmlResponse(Response):
# lxml wants a bytes and tests are basically hard-coded to str
body = ET.XML(self.body.encode('utf-8'))
except:
- import pdb; pdb.set_trace()
raise MalformedResponseError('Failed to parse XML',
body=self.body,
driver=self.connection.driver)
http://git-wip-us.apache.org/repos/asf/libcloud/blob/f33c126c/libcloud/test/__init__.py
----------------------------------------------------------------------
diff --git a/libcloud/test/__init__.py b/libcloud/test/__init__.py
index c1a98d6..3d0ec17 100644
--- a/libcloud/test/__init__.py
+++ b/libcloud/test/__init__.py
@@ -103,32 +103,18 @@ class MockHttp(LibcloudConnection):
Each of these mock methods should return a tuple of:
(int status, str body, dict headers, str reason)
-
- >>> mock = MockHttp('localhost', 8080)
- >>> mock.request('GET', '/example/')
- >>> response = mock.getresponse()
- >>> response.body.read()
- 'Hello World!'
- >>> response.status
- 200
- >>> response.getheaders()
- [('X-Foo', 'libcloud')]
- >>> MockHttp.type = 'fail'
- >>> mock.request('GET', '/example/')
- >>> response = mock.getresponse()
- >>> response.body.read()
- 'Oh Noes!'
- >>> response.status
- 403
- >>> response.getheaders()
- [('X-Foo', 'fail')]
-
"""
type = None
use_param = None # will use this param to namespace the request function
test = None # TestCase instance which is using this mock
proxy_url = None
+ def __init__(self, *args, **kwargs):
+ # Load assertion methods into the class, incase people want to assert
+ # within a response
+ if isinstance(self, unittest.TestCase):
+ unittest.TestCase.__init__(self, '__init__')
+ super(MockHttp, self).__init__(*args, **kwargs)
def _get_request(self, method, url, body=None, headers=None, raw=False, stream=False):
# Find a method we can use for this request
@@ -150,8 +136,10 @@ class MockHttp(LibcloudConnection):
def request(self, method, url, body=None, headers=None, raw=False, stream=False):
r_status, r_body, r_headers, r_reason = self._get_request(method, url, body, headers)
+ if r_body is None:
+ r_body = ''
with requests_mock.mock() as m:
- m.register_uri(method, url, text=r_body.replace('\n',' '), reason=r_reason,
+ m.register_uri(method, url, text=r_body, reason=r_reason,
headers=r_headers, status_code=r_status)
super(MockHttp, self).request(
method=method, url=url, body=body, headers=headers,
@@ -196,11 +184,6 @@ class MockHttp(LibcloudConnection):
return meth_name
-
-class MockHttpTestCase(unittest.TestCase):
- def runTest(self):
- pass
-
def assertUrlContainsQueryParams(self, url, expected_params, strict=False):
"""
Assert that provided url contains provided query parameters.
@@ -223,11 +206,11 @@ class MockHttpTestCase(unittest.TestCase):
params = dict(parse_qsl(url))
if strict:
- self.assertDictEqual(params, expected_params)
+ assert params == expected_params
else:
for key, value in expected_params.items():
- self.assertIn(key, params)
- self.assertEqual(params[key], value)
+ assert key in params
+ assert params[key] == value
class MockConnection(object):
http://git-wip-us.apache.org/repos/asf/libcloud/blob/f33c126c/libcloud/test/compute/__init__.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/__init__.py b/libcloud/test/compute/__init__.py
index 50500a2..3b66846 100644
--- a/libcloud/test/compute/__init__.py
+++ b/libcloud/test/compute/__init__.py
@@ -16,11 +16,9 @@
from libcloud.compute.base import Node, NodeImage, NodeLocation, StorageVolume
from libcloud.pricing import get_pricing
from libcloud.test import unittest
-import pytest
-@pytest.mark.skipif(True, reason='this fails in pytest')
-class TestCaseMixin(unittest.TestCase):
+class TestCaseMixin():
should_list_locations = True
should_have_pricing = False
should_list_volumes = False
http://git-wip-us.apache.org/repos/asf/libcloud/blob/f33c126c/libcloud/test/compute/test_openstack.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_openstack.py b/libcloud/test/compute/test_openstack.py
index 54737aa..fab2d7e 100644
--- a/libcloud/test/compute/test_openstack.py
+++ b/libcloud/test/compute/test_openstack.py
@@ -60,16 +60,16 @@ class OpenStack_1_0_ResponseTestCase(unittest.TestCase):
XML = """<?xml version="1.0" encoding="UTF-8"?><root/>"""
def test_simple_xml_content_type_handling(self):
- http_response = Response(
- 200, OpenStack_1_0_ResponseTestCase.XML, headers={'content-type': 'application/xml'})
+ http_response = Response(200,
+ OpenStack_1_0_ResponseTestCase.XML, headers={'content-type': 'application/xml'})
body = OpenStack_1_0_Response(http_response, None).parse_body()
self.assertTrue(hasattr(body, 'tag'), "Body should be parsed as XML")
def test_extended_xml_content_type_handling(self):
http_response = Response(200,
- OpenStack_1_0_ResponseTestCase.XML,
- headers={'content-type': 'application/xml; charset=UTF-8'})
+ OpenStack_1_0_ResponseTestCase.XML,
+ headers={'content-type': 'application/xml; charset=UTF-8'})
body = OpenStack_1_0_Response(http_response, None).parse_body()
self.assertTrue(hasattr(body, 'tag'), "Body should be parsed as XML")
@@ -77,15 +77,14 @@ class OpenStack_1_0_ResponseTestCase(unittest.TestCase):
def test_non_xml_content_type_handling(self):
RESPONSE_BODY = "Accepted"
- http_response = Response(
- 202, RESPONSE_BODY, headers={'content-type': 'text/html'})
+ http_response = Response(202, RESPONSE_BODY, headers={'content-type': 'text/html'})
body = OpenStack_1_0_Response(http_response, None).parse_body()
self.assertEqual(
body, RESPONSE_BODY, "Non-XML body should be returned as is")
-class OpenStack_1_0_Tests(unittest.TestCase, TestCaseMixin):
+class OpenStack_1_0_Tests(TestCaseMixin):
should_list_locations = False
should_list_volumes = False
http://git-wip-us.apache.org/repos/asf/libcloud/blob/f33c126c/libcloud/test/loadbalancer/test_slb.py
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/test_slb.py b/libcloud/test/loadbalancer/test_slb.py
index 7743361..c20e42d 100644
--- a/libcloud/test/loadbalancer/test_slb.py
+++ b/libcloud/test/loadbalancer/test_slb.py
@@ -272,7 +272,7 @@ class SLBDriverTestCases(unittest.TestCase):
self.cert_name))
-class SLBMockHttp(MockHttp):
+class SLBMockHttp(MockHttp, unittest.TestCase):
fixtures = LoadBalancerFileFixtures('slb')
def _DescribeLoadBalancers(self, method, url, body, headers):
http://git-wip-us.apache.org/repos/asf/libcloud/blob/f33c126c/setup.py
----------------------------------------------------------------------
diff --git a/setup.py b/setup.py
index 2d3b063..1e05352 100644
--- a/setup.py
+++ b/setup.py
@@ -271,8 +271,6 @@ setup(
author='Apache Software Foundation',
author_email='dev@libcloud.apache.org',
install_requires=install_requires,
- tests_require=['pytest'],
- setup_requires=['pytest-runner'],
packages=get_packages('libcloud'),
package_dir={
'libcloud': 'libcloud',