From ce6859133e62772d47b76a564e0318f8faf28aaa Mon Sep 17 00:00:00 2001 From: Nataliia Uvarova Date: Fri, 13 Jun 2014 20:15:58 +0300 Subject: [PATCH] Fix tests to be Python 3 compatible This commit includes small fixes in the test suite, such as proper integer division, dealing with difference in iterators. Partially-implements: blueprint py3k-support Change-Id: I4140258f564a639697a130a213d8a7733bee7713 --- marconi/tests/base.py | 3 ++- marconi/tests/functional/base.py | 2 +- .../tests/queues/transport/wsgi/v1_1/test_claims.py | 10 +++++----- tests/functional/wsgi/v1/test_messages.py | 6 ++++-- tests/unit/common/test_decorators.py | 2 +- tests/unit/queues/storage/test_impl_mongodb.py | 5 ++++- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/marconi/tests/base.py b/marconi/tests/base.py index 26889590b..ad809c090 100644 --- a/marconi/tests/base.py +++ b/marconi/tests/base.py @@ -17,6 +17,7 @@ import os import fixtures from oslo.config import cfg +import six import testtools @@ -83,7 +84,7 @@ class TestBase(testtools.TestCase): All overrides are automatically cleared at the end of the current test by the tearDown() method. """ - for k, v in kw.iteritems(): + for k, v in six.iteritems(kw): self.conf.set_override(k, v, group) def _my_dir(self): diff --git a/marconi/tests/functional/base.py b/marconi/tests/functional/base.py index c4e93c814..93b02b1fb 100644 --- a/marconi/tests/functional/base.py +++ b/marconi/tests/functional/base.py @@ -162,7 +162,7 @@ class FunctionalTestBase(testing.TestBase): expected_keys = ['age', 'created', 'href'] response_keys = message.keys() - response_keys.sort() + response_keys = sorted(response_keys) self.assertEqual(response_keys, expected_keys) # Verify that age has valid values diff --git a/marconi/tests/queues/transport/wsgi/v1_1/test_claims.py b/marconi/tests/queues/transport/wsgi/v1_1/test_claims.py index 992486dcc..992f56970 100644 --- a/marconi/tests/queues/transport/wsgi/v1_1/test_claims.py +++ b/marconi/tests/queues/transport/wsgi/v1_1/test_claims.py @@ -23,7 +23,7 @@ from marconi.tests.queues.transport.wsgi import base import mock from testtools import matchers - +from marconi.openstack.common import jsonutils from marconi.openstack.common import timeutils from marconi import tests as testing @@ -108,7 +108,7 @@ class ClaimsBaseTest(base.V1_1Base): headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_201) - claimed = json.loads(body[0]) + claimed = jsonutils.loads(body[0]) claim_href = self.srmock.headers_dict['Location'] message_href, params = claimed[0]['href'].split('?') @@ -153,7 +153,7 @@ class ClaimsBaseTest(base.V1_1Base): query_string='include_claimed=true' '&echo=true', headers=self.headers) - listed = json.loads(body[0]) + listed = jsonutils.loads(body[0]) self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(len(listed['messages']), len(claimed)) @@ -163,7 +163,7 @@ class ClaimsBaseTest(base.V1_1Base): mock_utcnow.return_value = now body = self.simulate_get(claim_href, headers=self.headers) - claim = json.loads(body[0]) + claim = jsonutils.loads(body[0]) self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(self.srmock.headers_dict['Content-Location'], @@ -204,7 +204,7 @@ class ClaimsBaseTest(base.V1_1Base): # Get the claimed messages (again) body = self.simulate_get(claim_href, headers=self.headers) query = timeutils.utcnow() - claim = json.loads(body[0]) + claim = jsonutils.loads(body[0]) message_href, params = claim['messages'][0]['href'].split('?') self.assertEqual(claim['ttl'], 60) diff --git a/tests/functional/wsgi/v1/test_messages.py b/tests/functional/wsgi/v1/test_messages.py index 1e54b09f0..2b89fa1be 100644 --- a/tests/functional/wsgi/v1/test_messages.py +++ b/tests/functional/wsgi/v1/test_messages.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import division + import uuid import ddt @@ -52,7 +54,7 @@ class TestMessages(base.FunctionalTestBase): doc = '[{{"body": "{0}", "ttl": 300}}, {{"body": "{1}", "ttl": 120}}]' overhead = len(doc.format('', '')) - half_size = (self.limits.max_message_size - overhead) / 2 + half_size = (self.limits.max_message_size - overhead) // 2 doc = doc.format(helpers.generate_random_string(half_size), helpers.generate_random_string(half_size + offset)) @@ -271,7 +273,7 @@ class TestMessages(base.FunctionalTestBase): doc = '[{{"body": "{0}", "ttl": 300}}, {{"body": "{1}", "ttl": 120}}]' overhead = len(doc.format('', '')) - half_size = (self.limits.max_message_size - overhead) / 2 + half_size = (self.limits.max_message_size - overhead) // 2 doc = doc.format(helpers.generate_random_string(half_size), helpers.generate_random_string(half_size + 1)) diff --git a/tests/unit/common/test_decorators.py b/tests/unit/common/test_decorators.py index 20bb21fce..92bcb816a 100644 --- a/tests/unit/common/test_decorators.py +++ b/tests/unit/common/test_decorators.py @@ -118,7 +118,7 @@ class TestDecorators(base.TestBase): self.assertEqual(instance.user_gets, 1) # Should be in the cache now. - user = msgpack.unpackb(cache.get(name)) + user = msgpack.unpackb(cache.get(name), encoding='utf-8') self.assertEqual(user, name) # Should read from the cache this time (counter will not diff --git a/tests/unit/queues/storage/test_impl_mongodb.py b/tests/unit/queues/storage/test_impl_mongodb.py index 1f6b342e3..0dee6cb17 100644 --- a/tests/unit/queues/storage/test_impl_mongodb.py +++ b/tests/unit/queues/storage/test_impl_mongodb.py @@ -21,6 +21,7 @@ import uuid import mock from pymongo import cursor import pymongo.errors +import six from testtools import matchers from marconi.openstack.common.cache import cache as oslo_cache @@ -179,7 +180,9 @@ class MongodbQueueTests(base.QueueControllerTest): def test_raises_connection_error(self): - with mock.patch.object(cursor.Cursor, 'next', autospec=True) as method: + with mock.patch.object(cursor.Cursor, + 'next' if six.PY2 else '__next__', + autospec=True) as method: error = pymongo.errors.ConnectionFailure() method.side_effect = error