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:
Brian Haley 2020-09-24 17:27:09 -04:00
parent 481f0b3b3c
commit 884d5e1578
4 changed files with 18 additions and 5 deletions

View File

@ -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
with [obj for obj in data if test(obj)].
- [N347] Test code must not import mock library
- [N348] Detect usage of assertItemsEqual

View File

@ -181,3 +181,15 @@ def check_no_import_mock(logical_line, filename, noqa):
for regex in import_mock, import_from_mock:
if re.match(regex, logical_line):
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)

View File

@ -192,11 +192,10 @@ class TestOvnOctaviaBase(base.TestOVNFunctionalBase,
def _validate_loadbalancers(self, expected_lbs):
observed_lbs = self._get_loadbalancers()
# NOTE (mjozefcz): assertItemsEqual works only on first level
# of comparison, if dicts inside dics are in diffrent
# NOTE (mjozefcz): assertCountEqual works only on first level
# of comparison, if dicts inside dicts are in different
# order it would fail.
self.assertEqual(len(expected_lbs),
len(observed_lbs))
self.assertEqual(len(expected_lbs), len(observed_lbs))
for expected_lb in expected_lbs:
# search for LB with same name and protocol
found = False
@ -308,7 +307,7 @@ class TestOvnOctaviaBase(base.TestOVNFunctionalBase,
calls_found.append(expected_status)
break
# 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,
check_call=True):

View File

@ -118,6 +118,7 @@ extension =
N343 = checks:check_no_imports_from_tests
N344 = checks:check_python3_no_filter
N347 = checks:check_no_import_mock
N348 = checks:check_assertcountequal
paths =./ovn_octavia_provider/hacking
[testenv:genconfig]