diff --git a/HACKING.rst b/HACKING.rst index 706b7f28..71fabfa0 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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 diff --git a/ovn_octavia_provider/hacking/checks.py b/ovn_octavia_provider/hacking/checks.py index bb5bb23b..3a5cc938 100644 --- a/ovn_octavia_provider/hacking/checks.py +++ b/ovn_octavia_provider/hacking/checks.py @@ -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) diff --git a/ovn_octavia_provider/tests/functional/base.py b/ovn_octavia_provider/tests/functional/base.py index dbdbd1f1..462771d2 100644 --- a/ovn_octavia_provider/tests/functional/base.py +++ b/ovn_octavia_provider/tests/functional/base.py @@ -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): diff --git a/tox.ini b/tox.ini index 6582ecf7..fad5a83f 100644 --- a/tox.ini +++ b/tox.ini @@ -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]