Switch from unittest2 compat methods to Python 3.x methods
With the removal of Python 2.x we can remove the unittest2 compat wrappers and switch to assertCountEqual instead of assertItemsEqual. Change-Id: I682997a4fceb139419a4b12d9c8fc6c504c329d5
This commit is contained in:
parent
481f0b3b3c
commit
884d5e1578
@ -16,3 +16,4 @@ Below you can find a list of checks specific to this repository.
|
|||||||
- [N344] Python 3: Do not use filter(lambda obj: test(obj), data). Replace it
|
- [N344] Python 3: Do not use filter(lambda obj: test(obj), data). Replace it
|
||||||
with [obj for obj in data if test(obj)].
|
with [obj for obj in data if test(obj)].
|
||||||
- [N347] Test code must not import mock library
|
- [N347] Test code must not import mock library
|
||||||
|
- [N348] Detect usage of assertItemsEqual
|
||||||
|
@ -181,3 +181,15 @@ def check_no_import_mock(logical_line, filename, noqa):
|
|||||||
for regex in import_mock, import_from_mock:
|
for regex in import_mock, import_from_mock:
|
||||||
if re.match(regex, logical_line):
|
if re.match(regex, logical_line):
|
||||||
yield(0, msg)
|
yield(0, msg)
|
||||||
|
|
||||||
|
|
||||||
|
@core.flake8ext
|
||||||
|
def check_assertcountequal(logical_line, filename):
|
||||||
|
"""N348 - Enforce using assertCountEqual."""
|
||||||
|
|
||||||
|
msg = ("N348: Use assertCountEqual(expected, observed) "
|
||||||
|
"instead of assertItemsEqual(observed, expected)")
|
||||||
|
|
||||||
|
if 'ovn_octavia_provider/tests/' in filename:
|
||||||
|
if re.search(r"assertItemsEqual\([^,]*,\s*(,[^,]*)?", logical_line):
|
||||||
|
yield (0, msg)
|
||||||
|
@ -192,11 +192,10 @@ class TestOvnOctaviaBase(base.TestOVNFunctionalBase,
|
|||||||
|
|
||||||
def _validate_loadbalancers(self, expected_lbs):
|
def _validate_loadbalancers(self, expected_lbs):
|
||||||
observed_lbs = self._get_loadbalancers()
|
observed_lbs = self._get_loadbalancers()
|
||||||
# NOTE (mjozefcz): assertItemsEqual works only on first level
|
# NOTE (mjozefcz): assertCountEqual works only on first level
|
||||||
# of comparison, if dicts inside dics are in diffrent
|
# of comparison, if dicts inside dicts are in different
|
||||||
# order it would fail.
|
# order it would fail.
|
||||||
self.assertEqual(len(expected_lbs),
|
self.assertEqual(len(expected_lbs), len(observed_lbs))
|
||||||
len(observed_lbs))
|
|
||||||
for expected_lb in expected_lbs:
|
for expected_lb in expected_lbs:
|
||||||
# search for LB with same name and protocol
|
# search for LB with same name and protocol
|
||||||
found = False
|
found = False
|
||||||
@ -308,7 +307,7 @@ class TestOvnOctaviaBase(base.TestOVNFunctionalBase,
|
|||||||
calls_found.append(expected_status)
|
calls_found.append(expected_status)
|
||||||
break
|
break
|
||||||
# Validate if we found all expected calls.
|
# Validate if we found all expected calls.
|
||||||
self.assertItemsEqual(expected_statuses, calls_found)
|
self.assertCountEqual(expected_statuses, calls_found)
|
||||||
|
|
||||||
def _wait_for_status_and_validate(self, lb_data, expected_status,
|
def _wait_for_status_and_validate(self, lb_data, expected_status,
|
||||||
check_call=True):
|
check_call=True):
|
||||||
|
1
tox.ini
1
tox.ini
@ -118,6 +118,7 @@ extension =
|
|||||||
N343 = checks:check_no_imports_from_tests
|
N343 = checks:check_no_imports_from_tests
|
||||||
N344 = checks:check_python3_no_filter
|
N344 = checks:check_python3_no_filter
|
||||||
N347 = checks:check_no_import_mock
|
N347 = checks:check_no_import_mock
|
||||||
|
N348 = checks:check_assertcountequal
|
||||||
paths =./ovn_octavia_provider/hacking
|
paths =./ovn_octavia_provider/hacking
|
||||||
|
|
||||||
[testenv:genconfig]
|
[testenv:genconfig]
|
||||||
|
Loading…
Reference in New Issue
Block a user