]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Python 3: fix test_utils
authorCyril Roelandt <cyril@redhat.com>
Fri, 14 Aug 2015 12:40:51 +0000 (14:40 +0200)
committerCyril Roelandt <cyril@redhat.com>
Tue, 18 Aug 2015 13:13:18 +0000 (15:13 +0200)
In Python 3, the error message returned when unpacking too many values is a bit
different from the one we see in Python 2:

Python 2:
  ValueError: too many values to unpack

Python 3:
  ValueError: too many values to unpack (expected <number of values>)

Blueprint: neutron-python3
Change-Id: Ib607a526c007567a370c521fd7e2e4f8b504b934

neutron/common/utils.py
neutron/tests/unit/common/test_utils.py
tox.ini

index 00b615b77730c3a4a3955f91ed7b32f00dab668b..f628904719b8a922008d7be88b2c0b1cbd470d6d 100644 (file)
@@ -263,7 +263,7 @@ def str2dict(string):
 
 
 def dict2tuple(d):
-    items = d.items()
+    items = list(d.items())
     items.sort()
     return tuple(items)
 
index b604bbb27aebfe83b123a197a44c42487a642713..f6aee3da935b92e054f00bc96027caf8d152c170 100644 (file)
@@ -137,7 +137,7 @@ class TestVxlanTunnelRangeVerifyValid(TestParseTunnelRangesMixin,
 class UtilTestParseVlanRanges(base.BaseTestCase):
     _err_prefix = "Invalid network VLAN range: '"
     _err_too_few = "' - 'need more than 2 values to unpack'"
-    _err_too_many = "' - 'too many values to unpack'"
+    _err_too_many_prefix = "' - 'too many values to unpack"
     _err_not_int = "' - 'invalid literal for int() with base 10: '%s''"
     _err_bad_vlan = "' - '%s is not a valid VLAN tag'"
     _err_range = "' - 'End of VLAN range is less than start of VLAN range'"
@@ -145,8 +145,8 @@ class UtilTestParseVlanRanges(base.BaseTestCase):
     def _range_too_few_err(self, nv_range):
         return self._err_prefix + nv_range + self._err_too_few
 
-    def _range_too_many_err(self, nv_range):
-        return self._err_prefix + nv_range + self._err_too_many
+    def _range_too_many_err_prefix(self, nv_range):
+        return self._err_prefix + nv_range + self._err_too_many_prefix
 
     def _vlan_not_int_err(self, nv_range, vlan):
         return self._err_prefix + nv_range + (self._err_not_int % vlan)
@@ -267,10 +267,13 @@ class TestParseOneVlanRange(UtilTestParseVlanRanges):
 
     def test_parse_one_net_range_too_many(self):
         config_str = "net1:100:150:200"
-        expected_msg = self._range_too_many_err(config_str)
+        expected_msg_prefix = self._range_too_many_err_prefix(config_str)
         err = self.assertRaises(n_exc.NetworkVlanRangeError,
                                 self.parse_one, config_str)
-        self.assertEqual(str(err), expected_msg)
+        # The error message is not same in Python 2 and Python 3. In Python 3,
+        # it depends on the amount of values used when unpacking, so it cannot
+        # be predicted as a fixed string.
+        self.assertTrue(str(err).startswith(expected_msg_prefix))
 
     def test_parse_one_net_vlan1_not_int(self):
         config_str = "net1:foo:199"
@@ -463,8 +466,8 @@ class TestCachingDecorator(base.BaseTestCase):
 
 class TestDict2Tuples(base.BaseTestCase):
     def test_dict(self):
-        input_dict = {'foo': 'bar', 42: 'baz', 'aaa': 'zzz'}
-        expected = ((42, 'baz'), ('aaa', 'zzz'), ('foo', 'bar'))
+        input_dict = {'foo': 'bar', '42': 'baz', 'aaa': 'zzz'}
+        expected = (('42', 'baz'), ('aaa', 'zzz'), ('foo', 'bar'))
         output_tuple = utils.dict2tuple(input_dict)
         self.assertEqual(expected, output_tuple)
 
diff --git a/tox.ini b/tox.ini
index db15ec3dafdd1247247a456895e13880cd0ab774..2d24598b7d008203a4e06e57d4047df34c213201 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -236,6 +236,7 @@ commands = python -m testtools.run \
     neutron.tests.unit.extensions.test_providernet \
     neutron.tests.unit.callbacks.test_manager \
     neutron.tests.unit.hacking.test_checks \
+    neutron.tests.unit.common.test_utils \
     neutron.tests.unit.common.test_config \
     neutron.tests.unit.common.test_rpc \
     neutron.tests.unit.common.test_ipv6_utils \