From 2b5a92bd019c8241eab2a43baf4f5d5af9757412 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Fri, 9 Jan 2026 00:55:46 +0900 Subject: [PATCH] Drop redundant assertion method overrides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... and use the built-in ones instead. Also replace assertDictEqual because assertEqual should select an appropriate method automatically[1]. [1] https://docs.python.org/3.13/library/unittest.html The list of type-specific methods automatically used by assertEqual() are summarized in the following table. Note that it’s usually not necessary to invoke these methods directly. Change-Id: I4b7d3563e5a025cd3efa602a28c12865dcf3474d Signed-off-by: Takashi Kajinami --- taskflow/test.py | 71 ------------------- .../tests/unit/action_engine/test_scoping.py | 2 +- taskflow/tests/unit/jobs/test_zk_job.py | 4 +- .../tests/unit/patterns/test_graph_flow.py | 4 +- .../tests/unit/patterns/test_linear_flow.py | 4 +- .../unit/patterns/test_unordered_flow.py | 2 +- taskflow/tests/unit/test_exceptions.py | 2 +- taskflow/tests/unit/test_functor_task.py | 2 +- taskflow/tests/unit/test_listeners.py | 26 +++---- taskflow/tests/unit/test_mapfunctor_task.py | 4 +- .../tests/unit/test_reducefunctor_task.py | 4 +- .../tests/unit/worker_based/test_worker.py | 2 +- 12 files changed, 28 insertions(+), 99 deletions(-) diff --git a/taskflow/test.py b/taskflow/test.py index d57ab3028..e620aa396 100644 --- a/taskflow/test.py +++ b/taskflow/test.py @@ -24,19 +24,6 @@ from testtools import matchers from taskflow import exceptions from taskflow.tests import fixtures as taskflow_fixtures from taskflow.tests import utils -from taskflow.utils import misc - - -class GreaterThanEqual: - """Matches if the item is geq than the matchers reference object.""" - - def __init__(self, source): - self.source = source - - def match(self, other): - if other >= self.source: - return None - return matchers.Mismatch(f"{other} was not >= {self.source}") class FailureRegexpMatcher: @@ -60,32 +47,6 @@ class FailureRegexpMatcher: (failure, self.exc_class)) -class ItemsEqual: - """Matches the items in two sequences. - - This matcher will validate that the provided sequence has the same elements - as a reference sequence, regardless of the order. - """ - - def __init__(self, seq): - self._seq = seq - self._list = list(seq) - - def match(self, other): - other_list = list(other) - extra = misc.sequence_minus(other_list, self._list) - missing = misc.sequence_minus(self._list, other_list) - if extra or missing: - msg = ("Sequences %s and %s do not have same items." - % (self._seq, other)) - if missing: - msg += " Extra items in first sequence: %s." % missing - if extra: - msg += " Extra items in second sequence: %s." % extra - return matchers.Mismatch(msg) - return None - - class TestCase(base.BaseTestCase): """Test case base class for all taskflow unit tests.""" @@ -97,22 +58,6 @@ class TestCase(base.BaseTestCase): t_dir = self.useFixture(fixtures.TempDir()) return t_dir.path - def assertDictEqual(self, expected, check): - self.assertIsInstance(expected, dict, - 'First argument is not a dictionary') - self.assertIsInstance(check, dict, - 'Second argument is not a dictionary') - - # Testtools seems to want equals objects instead of just keys? - compare_dict = {} - for k in list(expected.keys()): - if not isinstance(expected[k], matchers.Equals): - compare_dict[k] = matchers.Equals(expected[k]) - else: - compare_dict[k] = expected[k] - self.assertThat(matchee=check, - matcher=matchers.MatchesDict(compare_dict)) - def assertRaisesAttrAccess(self, exc_class, obj, attr_name): def access_func(): @@ -120,18 +65,6 @@ class TestCase(base.BaseTestCase): self.assertRaises(exc_class, access_func) - def assertGreater(self, first, second): - matcher = matchers.GreaterThan(first) - self.assertThat(second, matcher) - - def assertGreaterEqual(self, first, second): - matcher = GreaterThanEqual(first) - self.assertThat(second, matcher) - - def assertRegexpMatches(self, text, pattern): - matcher = matchers.MatchesRegex(pattern) - self.assertThat(text, matcher) - def assertIsSuperAndSubsequence(self, super_seq, sub_seq, msg=None): super_seq = list(super_seq) sub_seq = list(sub_seq) @@ -158,10 +91,6 @@ class TestCase(base.BaseTestCase): except exceptions.WrappedFailure as e: self.assertThat(e, FailureRegexpMatcher(exc_class, pattern)) - def assertCountEqual(self, seq1, seq2, msg=None): - matcher = ItemsEqual(seq1) - self.assertThat(seq2, matcher) - class MockTestCase(TestCase): diff --git a/taskflow/tests/unit/action_engine/test_scoping.py b/taskflow/tests/unit/action_engine/test_scoping.py index 7964aeb2a..76a8b1748 100644 --- a/taskflow/tests/unit/action_engine/test_scoping.py +++ b/taskflow/tests/unit/action_engine/test_scoping.py @@ -242,7 +242,7 @@ class MixedPatternScopingTest(test.TestCase): if n.startswith('subroot.'): first_subroot = i break - self.assertGreater(first_subroot, first_root) + self.assertGreater(first_root, first_subroot) self.assertEqual(['root.2', 'root.1'], scope[0][-2:]) def test_shadow_graph(self): diff --git a/taskflow/tests/unit/jobs/test_zk_job.py b/taskflow/tests/unit/jobs/test_zk_job.py index adaf271cd..3cb0fddbd 100644 --- a/taskflow/tests/unit/jobs/test_zk_job.py +++ b/taskflow/tests/unit/jobs/test_zk_job.py @@ -250,7 +250,7 @@ class ZakeJobboardTest(test.TestCase, ZookeeperBoardTestMixin): self.assertEqual(1, len(paths)) path_key = list(paths.keys())[0] self.assertTrue(len(paths[path_key]['data']) > 0) - self.assertDictEqual({ + self.assertEqual({ 'uuid': posted_job.uuid, 'name': posted_job.name, 'book': { @@ -278,7 +278,7 @@ class ZakeJobboardTest(test.TestCase, ZookeeperBoardTestMixin): conductor_data = ( self.client.storage.paths[conductor_entity_path]['data']) self.assertTrue(len(conductor_data) > 0) - self.assertDictEqual({ + self.assertEqual({ 'name': conductor_name, 'kind': 'conductor', 'metadata': {}, diff --git a/taskflow/tests/unit/patterns/test_graph_flow.py b/taskflow/tests/unit/patterns/test_graph_flow.py index a4e79712b..ff3372954 100644 --- a/taskflow/tests/unit/patterns/test_graph_flow.py +++ b/taskflow/tests/unit/patterns/test_graph_flow.py @@ -238,7 +238,7 @@ class GraphFlowTest(test.TestCase): f = gf.Flow('test').add(task1, task2, f1) for (n, data) in f.iter_nodes(): self.assertIn(n, tasks) - self.assertDictEqual({}, data) + self.assertEqual({}, data) def test_iter_links(self): task1 = _task('task1') @@ -251,7 +251,7 @@ class GraphFlowTest(test.TestCase): for (u, v, data) in f.iter_links(): self.assertIn(u, tasks) self.assertIn(v, tasks) - self.assertDictEqual({}, data) + self.assertEqual({}, data) class TargetedGraphFlowTest(test.TestCase): diff --git a/taskflow/tests/unit/patterns/test_linear_flow.py b/taskflow/tests/unit/patterns/test_linear_flow.py index 32b288a64..e88da69a9 100644 --- a/taskflow/tests/unit/patterns/test_linear_flow.py +++ b/taskflow/tests/unit/patterns/test_linear_flow.py @@ -129,7 +129,7 @@ class LinearFlowTest(test.TestCase): tasks = {task1, task2, task3} for (node, data) in f.iter_nodes(): self.assertIn(node, tasks) - self.assertDictEqual({}, data) + self.assertEqual({}, data) def test_iter_links(self): task1 = _task(name='task1') @@ -140,4 +140,4 @@ class LinearFlowTest(test.TestCase): for (u, v, data) in f.iter_links(): self.assertIn(u, tasks) self.assertIn(v, tasks) - self.assertDictEqual({'invariant': True}, data) + self.assertEqual({'invariant': True}, data) diff --git a/taskflow/tests/unit/patterns/test_unordered_flow.py b/taskflow/tests/unit/patterns/test_unordered_flow.py index fef20d624..033159b98 100644 --- a/taskflow/tests/unit/patterns/test_unordered_flow.py +++ b/taskflow/tests/unit/patterns/test_unordered_flow.py @@ -125,7 +125,7 @@ class UnorderedFlowTest(test.TestCase): f.add(task2, task1) for (node, data) in f.iter_nodes(): self.assertIn(node, tasks) - self.assertDictEqual({}, data) + self.assertEqual({}, data) def test_iter_links(self): task1 = _task(name='task1', provides=['a', 'b']) diff --git a/taskflow/tests/unit/test_exceptions.py b/taskflow/tests/unit/test_exceptions.py index e66934e93..ba837ff19 100644 --- a/taskflow/tests/unit/test_exceptions.py +++ b/taskflow/tests/unit/test_exceptions.py @@ -37,7 +37,7 @@ class TestExceptions(test.TestCase): except Exception as e: capture = e self.assertIsNotNone(capture) - self.assertGreater(0, len(capture.pformat())) + self.assertGreater(len(capture.pformat()), 0) def test_raise_with(self): capture = None diff --git a/taskflow/tests/unit/test_functor_task.py b/taskflow/tests/unit/test_functor_task.py index 1188e48af..430aacb86 100644 --- a/taskflow/tests/unit/test_functor_task.py +++ b/taskflow/tests/unit/test_functor_task.py @@ -89,4 +89,4 @@ class FunctorTaskTest(test.TestCase): 'product': 6 }) - self.assertDictEqual(expected, result) + self.assertEqual(expected, result) diff --git a/taskflow/tests/unit/test_listeners.py b/taskflow/tests/unit/test_listeners.py index 157046ba2..10156f55c 100644 --- a/taskflow/tests/unit/test_listeners.py +++ b/taskflow/tests/unit/test_listeners.py @@ -177,7 +177,7 @@ class TestClaimListener(test.TestCase, EngineMakerMixin): self.assertNotEqual(-1, destroyed_at) after_states = ran_states[destroyed_at:] - self.assertGreater(0, len(after_states)) + self.assertGreater(len(after_states), 0) def test_claim_lost_new_owner(self): job = self._post_claim_job('test') @@ -224,7 +224,7 @@ class TestDurationListener(test.TestCase, EngineMakerMixin): self.assertIsNotNone(td) self.assertIsNotNone(td.meta) self.assertIn('duration', td.meta) - self.assertGreaterEqual(0.1, td.meta['duration']) + self.assertGreaterEqual(td.meta['duration'], 0.1) def test_flow_duration(self): with contextlib.closing(impl_memory.MemoryBackend()) as be: @@ -237,7 +237,7 @@ class TestDurationListener(test.TestCase, EngineMakerMixin): self.assertIsNotNone(fd) self.assertIsNotNone(fd.meta) self.assertIn('duration', fd.meta) - self.assertGreaterEqual(0.1, fd.meta['duration']) + self.assertGreaterEqual(fd.meta['duration'], 0.1) @mock.patch.object(timing.LOG, 'warning') def test_record_ending_exception(self, mocked_warning): @@ -271,12 +271,12 @@ class TestEventTimeListener(test.TestCase, EngineMakerMixin): self.assertIn(running_field, td.meta) self.assertIn(success_field, td.meta) td_duration = td.meta[success_field] - td.meta[running_field] - self.assertGreaterEqual(0.1, td_duration) + self.assertGreaterEqual(td_duration, 0.1) fd_meta = engine.storage._flowdetail.meta self.assertIn(running_field, fd_meta) self.assertIn(success_field, fd_meta) fd_duration = fd_meta[success_field] - fd_meta[running_field] - self.assertGreaterEqual(0.1, fd_duration) + self.assertGreaterEqual(fd_duration, 0.1) class TestCapturingListeners(test.TestCase, EngineMakerMixin): @@ -312,7 +312,7 @@ class TestLoggingListeners(test.TestCase, EngineMakerMixin): log, handler = self._make_logger() with logging_listeners.LoggingListener(e, log=log): e.run() - self.assertGreater(0, handler.counts[logging.DEBUG]) + self.assertGreater(handler.counts[logging.DEBUG], 0) for levelno in _LOG_LEVELS - {logging.DEBUG}: self.assertEqual(0, handler.counts[levelno]) self.assertEqual([], handler.exc_infos) @@ -326,7 +326,7 @@ class TestLoggingListeners(test.TestCase, EngineMakerMixin): e, log=log, level=logging.INFO) with listener: e.run() - self.assertGreater(0, handler.counts[logging.INFO]) + self.assertGreater(handler.counts[logging.INFO], 0) for levelno in _LOG_LEVELS - {logging.INFO}: self.assertEqual(0, handler.counts[levelno]) self.assertEqual([], handler.exc_infos) @@ -338,7 +338,7 @@ class TestLoggingListeners(test.TestCase, EngineMakerMixin): log, handler = self._make_logger() with logging_listeners.LoggingListener(e, log=log): self.assertRaises(RuntimeError, e.run) - self.assertGreater(0, handler.counts[logging.DEBUG]) + self.assertGreater(handler.counts[logging.DEBUG], 0) for levelno in _LOG_LEVELS - {logging.DEBUG}: self.assertEqual(0, handler.counts[levelno]) self.assertEqual(1, len(handler.exc_infos)) @@ -350,7 +350,7 @@ class TestLoggingListeners(test.TestCase, EngineMakerMixin): log, handler = self._make_logger() with logging_listeners.DynamicLoggingListener(e, log=log): e.run() - self.assertGreater(0, handler.counts[logging.DEBUG]) + self.assertGreater(handler.counts[logging.DEBUG], 0) for levelno in _LOG_LEVELS - {logging.DEBUG}: self.assertEqual(0, handler.counts[levelno]) self.assertEqual([], handler.exc_infos) @@ -362,8 +362,8 @@ class TestLoggingListeners(test.TestCase, EngineMakerMixin): log, handler = self._make_logger() with logging_listeners.DynamicLoggingListener(e, log=log): self.assertRaises(RuntimeError, e.run) - self.assertGreater(0, handler.counts[logging.WARNING]) - self.assertGreater(0, handler.counts[logging.DEBUG]) + self.assertGreater(handler.counts[logging.WARNING], 0) + self.assertGreater(handler.counts[logging.DEBUG], 0) self.assertEqual(1, len(handler.exc_infos)) for levelno in _LOG_LEVELS - {logging.DEBUG, logging.WARNING}: self.assertEqual(0, handler.counts[levelno]) @@ -377,8 +377,8 @@ class TestLoggingListeners(test.TestCase, EngineMakerMixin): e, log=log, failure_level=logging.ERROR) with listener: self.assertRaises(RuntimeError, e.run) - self.assertGreater(0, handler.counts[logging.ERROR]) - self.assertGreater(0, handler.counts[logging.DEBUG]) + self.assertGreater(handler.counts[logging.ERROR], 0) + self.assertGreater(handler.counts[logging.DEBUG], 0) self.assertEqual(1, len(handler.exc_infos)) for levelno in _LOG_LEVELS - {logging.DEBUG, logging.ERROR}: self.assertEqual(0, handler.counts[levelno]) diff --git a/taskflow/tests/unit/test_mapfunctor_task.py b/taskflow/tests/unit/test_mapfunctor_task.py index 4afc5c619..495b1502a 100644 --- a/taskflow/tests/unit/test_mapfunctor_task.py +++ b/taskflow/tests/unit/test_mapfunctor_task.py @@ -55,7 +55,7 @@ class MapFunctorTaskTest(test.TestCase): provides=provides)) result = engines.run(flow, store=self.flow_store) - self.assertDictEqual(expected, result) + self.assertEqual(expected, result) def test_square_array(self): expected = self.flow_store.copy() @@ -75,4 +75,4 @@ class MapFunctorTaskTest(test.TestCase): provides=provides)) result = engines.run(flow, store=self.flow_store) - self.assertDictEqual(expected, result) + self.assertEqual(expected, result) diff --git a/taskflow/tests/unit/test_reducefunctor_task.py b/taskflow/tests/unit/test_reducefunctor_task.py index 4612128df..47cbac5bd 100644 --- a/taskflow/tests/unit/test_reducefunctor_task.py +++ b/taskflow/tests/unit/test_reducefunctor_task.py @@ -51,7 +51,7 @@ class ReduceFunctorTaskTest(test.TestCase): provides=provides)) result = engines.run(flow, store=self.flow_store) - self.assertDictEqual(expected, result) + self.assertEqual(expected, result) def test_multiply_array(self): expected = self.flow_store.copy() @@ -67,4 +67,4 @@ class ReduceFunctorTaskTest(test.TestCase): provides=provides)) result = engines.run(flow, store=self.flow_store) - self.assertDictEqual(expected, result) + self.assertEqual(expected, result) diff --git a/taskflow/tests/unit/worker_based/test_worker.py b/taskflow/tests/unit/worker_based/test_worker.py index c245b67bb..218c54d5f 100644 --- a/taskflow/tests/unit/worker_based/test_worker.py +++ b/taskflow/tests/unit/worker_based/test_worker.py @@ -69,7 +69,7 @@ class TestWorker(test.MockTestCase): w.run(banner_writer=buf.write) w.wait() w.stop() - self.assertGreater(0, len(buf.getvalue())) + self.assertGreater(len(buf.getvalue()), 0) def test_creation_with_custom_threads_count(self): self.worker(threads_count=10)