py3: Port remaining unit tests to Python 3

* Replace filter() and map() with list-comprehension when a list is
  expected. filter() and map() now return a generator on Python 3
* Use "six.moves.builtins" instead of "__builtin__" to mock the
  builtin open() function.
* On error, json.loads() fails with ValueError, but the error message
  is different on Python 3. Accept Python 2 and Python 3 error
  messages.
* tox.ini: remove py34 section, the whitelist is no more needed since
  all unit tests now pass on Python 3.4.

Change-Id: Iaa66d906d8b503d548a03ba9a3b0d9c5b6d49813
This commit is contained in:
Victor Stinner 2015-09-16 14:09:25 +02:00
parent 0e025d9fa7
commit a8d7506eec
10 changed files with 28 additions and 39 deletions

View File

@ -59,10 +59,8 @@ class NotificationsAction(Action):
notifications = Notifications.get_all_data()
if not params.all:
notifications = filter(
lambda notification: notification['status'] == 'unread',
notifications
)
notifications = [notification for notification in notifications
if notification['status'] == 'unread']
self.serializer.print_to_output(
notifications,

View File

@ -54,7 +54,8 @@ class ArrayAction(argparse.Action):
:returns: list of ids
"""
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, map(int, chain(*values)))
list_ids = [int(value) for value in chain(*values)]
setattr(namespace, self.dest, list_ids)
class NodeAction(argparse.Action):
@ -91,7 +92,8 @@ class NodeAction(argparse.Action):
'Node with mac endfix "{0}" was not found.'
.format(short_mac)
)
setattr(namespace, self.dest, map(int, only_ids))
node_ids = [int(node_id) for node_id in only_ids]
setattr(namespace, self.dest, node_ids)
class SetAction(argparse.Action):

View File

@ -80,10 +80,7 @@ def quote_and_join(words):
if len(words) > 1:
return '{0} and "{1}"'.format(
", ".join(
map(
lambda x: '"{0}"'.format(x),
words
)[0:-1]
['"{0}"'.format(x) for x in words][:-1]
),
words[-1]
)

View File

@ -213,10 +213,7 @@ class Parser:
def prepare_args(self):
# replace some args from dict substitutions
self.args = map(
lambda x: substitutions.get(x, x),
self.args
)
self.args = [substitutions.get(arg, arg) for arg in self.args]
# move general used flags before actions, otherwise they will be used
# as a part of action by action_generator

View File

@ -448,12 +448,11 @@ class Plugins(base.BaseObject):
if not cls.is_updatable(metadata['package_version']):
return
plugins = filter(
lambda p: p['name'] == metadata['name'] and
cls.is_updatable(p['package_version']) and
utils.major_plugin_version(metadata['version']) ==
utils.major_plugin_version(p['version']),
cls.get_all_data())
plugins = [p for p in cls.get_all_data()
if (p['name'] == metadata['name'] and
cls.is_updatable(p['package_version']) and
utils.major_plugin_version(metadata['version']) ==
utils.major_plugin_version(p['version']))]
plugin = None
if plugins:
@ -485,9 +484,8 @@ class Plugins(base.BaseObject):
:returns: dictionary with plugin data
:raises: error.BadDataException if no plugin was found
"""
plugins = filter(
lambda p: (p['name'], p['version']) == (name, version),
cls.get_all_data())
plugins = [p for p in cls.get_all_data()
if (p['name'], p['version']) == (name, version)]
if not plugins:
raise error.BadDataException(

View File

@ -43,7 +43,7 @@ class BaseSettings(base.UnitTestCase):
def check_upload_action(self, mrequests, test_command, test_url):
m = mock_open(read_data=YAML_SETTINGS_DATA)
with patch('__builtin__.open', m, create=True):
with patch('six.moves.builtins.open', m, create=True):
self.execute(test_command)
request = mrequests.put.call_args_list[0]
@ -61,7 +61,7 @@ class BaseSettings(base.UnitTestCase):
mresponse.json.return_value = JSON_SETTINGS_DATA
mrequests.get.return_value = mresponse
with patch('__builtin__.open', m, create=True):
with patch('six.moves.builtins.open', m, create=True):
self.execute(test_command)
request = mrequests.get.call_args_list[0]
@ -77,7 +77,7 @@ class BaseSettings(base.UnitTestCase):
mresponse.json.return_value = JSON_SETTINGS_DATA
mrequests.get.return_value = mresponse
with patch('__builtin__.open', m, create=True):
with patch('six.moves.builtins.open', m, create=True):
self.execute(test_command)
request = mrequests.get.call_args_list[0]

View File

@ -81,8 +81,9 @@ class TestFuelVersion(base_tests.UnitTestCase):
with self.assertRaises(SystemExit):
self.execute(['fuel', '--fuel-version', '--yaml'])
args, _ = mstdout.write.call_args_list[0]
with self.assertRaisesRegexp(
ValueError, 'No JSON object could be decoded'):
regex = ('No JSON object could be decoded'
'|Expecting value: line 1 column 1')
with self.assertRaisesRegexp(ValueError, regex):
json.loads(args[0])
self.assertEqual(self.VERSION, yaml.load(args[0]))

View File

@ -31,8 +31,9 @@ class TestFuelVersion(base.UnitTestCase):
with mock.patch('sys.stdout') as mstdout:
self.execute(['fuel', 'fuel-version', '--yaml'])
args, _ = mstdout.write.call_args_list[0]
with self.assertRaisesRegexp(
ValueError, 'No JSON object could be decoded'):
regex = ('No JSON object could be decoded'
'|Expecting value: line 1 column 1')
with self.assertRaisesRegexp(ValueError, regex):
json.loads(args[0])
self.assertEqual(
fake_fuel_version.get_fake_fuel_version(),

View File

@ -14,7 +14,6 @@
from collections import namedtuple
import copy
from functools import partial
from fuelclient.cli import error
from fuelclient import objects
@ -41,11 +40,12 @@ class NodeClient(base_v1.BaseV1Client):
result = self._entity_wrapper.get_all_data()
if environment_id is not None:
result = filter(lambda n: n['cluster'] == environment_id, result)
result = [item for item in result
if item['cluster'] == environment_id]
if labels:
result = filter(
partial(self._check_label, labels), result)
result = [item for item in result
if self._check_label(labels, item)]
return result

View File

@ -31,11 +31,6 @@ deps = -r{toxinidir}/requirements.txt
commands =
py.test -vv --junit-xml $FUELCLIENT_JUNIT {posargs:fuelclient/tests/unit}
[testenv:py34]
commands =
py.test {posargs} \
fuelclient/tests/unit/common/test_utils.py
[testenv:functional]
commands =
bash {toxinidir}/tools/cleanup.sh