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

We have been able to use them since then, because
testtools required unittest2, which still included it. With testtools
removing Python 2.7 support [3][4], we will lose support for
assertItemsEqual, so we should switch to use assertCountEqual.

[1] - https://bugs.python.org/issue17866
[2] - https://hg.python.org/cpython/rev/d9921cb6e3cd
[3] - testing-cabal/testtools#286
[4] - testing-cabal/testtools#277

Change-Id: Iaa8251a1e9965a00fe99b7a740a104c011260340
This commit is contained in:
melissaml 2020-07-05 20:04:04 +08:00
parent 2a9761bece
commit cf327a2e2d
4 changed files with 50 additions and 50 deletions

View File

@ -182,7 +182,7 @@ class TestCase(base.BaseTestCase):
except exceptions.WrappedFailure as e:
self.assertThat(e, FailureRegexpMatcher(exc_class, pattern))
def assertItemsEqual(self, seq1, seq2, msg=None):
def assertCountEqual(self, seq1, seq2, msg=None):
matcher = ItemsEqual(seq1)
self.assertThat(seq2, matcher)

View File

@ -100,7 +100,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(6, len(g))
self.assertItemsEqual(g.edges(), [
self.assertCountEqual(g.edges(), [
('test', 'a'),
('test', 'b'),
('test', 'c'),
@ -148,7 +148,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(8, len(g))
self.assertItemsEqual(g.edges(), [
self.assertCountEqual(g.edges(), [
('test', 'a'),
('test', 'b'),
('test', 'test2'),
@ -168,7 +168,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(8, len(g))
self.assertItemsEqual(g.edges(), [
self.assertCountEqual(g.edges(), [
('lt', 'a'),
('a', 'ut'),
('ut', 'b'),
@ -199,7 +199,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(11, len(g))
self.assertItemsEqual(g.edges(), [
self.assertCountEqual(g.edges(), [
('test', 'a'),
('test', 'b'),
('test', 'c'),
@ -230,7 +230,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(11, len(g))
self.assertItemsEqual(g.edges(), [
self.assertCountEqual(g.edges(), [
('test', 'a'),
('test', 'b'),
('test', 'c'),
@ -263,15 +263,15 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(6, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'a', {'invariant': True}),
('a', 'b', {'manual': True}),
('b', 'c', {'manual': True}),
('c', 'd', {'manual': True}),
('d', 'test[$]', {'invariant': True}),
])
self.assertItemsEqual(['test'], g.no_predecessors_iter())
self.assertItemsEqual(['test[$]'], g.no_successors_iter())
self.assertCountEqual(['test'], g.no_predecessors_iter())
self.assertCountEqual(['test[$]'], g.no_successors_iter())
def test_graph_dependencies(self):
a = test_utils.ProvidesRequiresTask('a', provides=['x'], requires=[])
@ -281,13 +281,13 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(4, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'a', {'invariant': True}),
('a', 'b', {'reasons': set(['x'])}),
('b', 'test[$]', {'invariant': True}),
])
self.assertItemsEqual(['test'], g.no_predecessors_iter())
self.assertItemsEqual(['test[$]'], g.no_successors_iter())
self.assertCountEqual(['test'], g.no_predecessors_iter())
self.assertCountEqual(['test[$]'], g.no_successors_iter())
def test_graph_nested_requires(self):
a = test_utils.ProvidesRequiresTask('a', provides=['x'], requires=[])
@ -299,7 +299,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(7, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'a', {'invariant': True}),
('test2', 'b', {'invariant': True}),
('a', 'test2', {'reasons': set(['x'])}),
@ -307,8 +307,8 @@ class PatternCompileTest(test.TestCase):
('c', 'test2[$]', {'invariant': True}),
('test2[$]', 'test[$]', {'invariant': True}),
])
self.assertItemsEqual(['test'], list(g.no_predecessors_iter()))
self.assertItemsEqual(['test[$]'], list(g.no_successors_iter()))
self.assertCountEqual(['test'], list(g.no_predecessors_iter()))
self.assertCountEqual(['test[$]'], list(g.no_successors_iter()))
def test_graph_nested_provides(self):
a = test_utils.ProvidesRequiresTask('a', provides=[], requires=['x'])
@ -320,7 +320,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(7, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'test2', {'invariant': True}),
('a', 'test[$]', {'invariant': True}),
@ -331,8 +331,8 @@ class PatternCompileTest(test.TestCase):
('b', 'c', {'invariant': True}),
('c', 'test2[$]', {'invariant': True}),
])
self.assertItemsEqual(['test'], g.no_predecessors_iter())
self.assertItemsEqual(['test[$]'], g.no_successors_iter())
self.assertCountEqual(['test'], g.no_predecessors_iter())
self.assertCountEqual(['test[$]'], g.no_successors_iter())
def test_empty_flow_in_linear_flow(self):
flo = lf.Flow('lf')
@ -343,7 +343,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertItemsEqual(g.edges(), [
self.assertCountEqual(g.edges(), [
("lf", "a"),
("a", "empty"),
("empty", "empty[$]"),
@ -499,7 +499,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(6, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'c1', {'invariant': True}),
('c1', 'test2', {'invariant': True, 'retry': True}),
('test2', 'c2', {'invariant': True}),
@ -507,8 +507,8 @@ class PatternCompileTest(test.TestCase):
('test2[$]', 'test[$]', {'invariant': True}),
])
self.assertIs(c1, g.nodes['c2']['retry'])
self.assertItemsEqual(['test'], list(g.no_predecessors_iter()))
self.assertItemsEqual(['test[$]'], list(g.no_successors_iter()))
self.assertCountEqual(['test'], list(g.no_predecessors_iter()))
self.assertCountEqual(['test[$]'], list(g.no_successors_iter()))
def test_retry_in_linear_flow_with_tasks(self):
c = retry.AlwaysRevert("c")
@ -518,15 +518,15 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(5, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'c', {'invariant': True}),
('a', 'b', {'invariant': True}),
('c', 'a', {'invariant': True, 'retry': True}),
('b', 'test[$]', {'invariant': True}),
])
self.assertItemsEqual(['test'], g.no_predecessors_iter())
self.assertItemsEqual(['test[$]'], g.no_successors_iter())
self.assertCountEqual(['test'], g.no_predecessors_iter())
self.assertCountEqual(['test[$]'], g.no_successors_iter())
self.assertIs(c, g.nodes['a']['retry'])
self.assertIs(c, g.nodes['b']['retry'])
@ -538,7 +538,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(5, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'c', {'invariant': True}),
('c', 'a', {'invariant': True, 'retry': True}),
('c', 'b', {'invariant': True, 'retry': True}),
@ -546,8 +546,8 @@ class PatternCompileTest(test.TestCase):
('a', 'test[$]', {'invariant': True}),
])
self.assertItemsEqual(['test'], list(g.no_predecessors_iter()))
self.assertItemsEqual(['test[$]'], list(g.no_successors_iter()))
self.assertCountEqual(['test'], list(g.no_predecessors_iter()))
self.assertCountEqual(['test[$]'], list(g.no_successors_iter()))
self.assertIs(c, g.nodes['a']['retry'])
self.assertIs(c, g.nodes['b']['retry'])
@ -558,7 +558,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'r', {'invariant': True}),
('r', 'a', {'invariant': True, 'retry': True}),
('r', 'b', {'invariant': True, 'retry': True}),
@ -567,8 +567,8 @@ class PatternCompileTest(test.TestCase):
('c', 'test[$]', {'invariant': True}),
])
self.assertItemsEqual(['test'], g.no_predecessors_iter())
self.assertItemsEqual(['test[$]'], g.no_successors_iter())
self.assertCountEqual(['test'], g.no_predecessors_iter())
self.assertCountEqual(['test[$]'], g.no_successors_iter())
self.assertIs(r, g.nodes['a']['retry'])
self.assertIs(r, g.nodes['b']['retry'])
self.assertIs(r, g.nodes['c']['retry'])
@ -583,7 +583,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(10, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'c1', {'invariant': True}),
('c1', 'a', {'invariant': True, 'retry': True}),
('a', 'test2', {'invariant': True}),
@ -610,7 +610,7 @@ class PatternCompileTest(test.TestCase):
g = _replicate_graph_with_names(
compiler.PatternCompiler(flo).compile())
self.assertEqual(9, len(g))
self.assertItemsEqual(g.edges(data=True), [
self.assertCountEqual(g.edges(data=True), [
('test', 'c1', {'invariant': True}),
('c1', 'a', {'invariant': True, 'retry': True}),
('a', 'test2', {'invariant': True}),

View File

@ -85,7 +85,7 @@ class GraphFlowTest(test.TestCase):
f = gf.Flow('test').add(task1, task2)
self.assertEqual(2, len(f))
self.assertItemsEqual(f, [task1, task2])
self.assertCountEqual(f, [task1, task2])
self.assertEqual([], list(f.iter_links()))
def test_graph_flow_two_dependent_tasks(self):
@ -94,7 +94,7 @@ class GraphFlowTest(test.TestCase):
f = gf.Flow('test').add(task1, task2)
self.assertEqual(2, len(f))
self.assertItemsEqual(f, [task1, task2])
self.assertCountEqual(f, [task1, task2])
self.assertEqual([(task1, task2, {'reasons': set(['a'])})],
list(f.iter_links()))
@ -107,7 +107,7 @@ class GraphFlowTest(test.TestCase):
f = gf.Flow('test').add(task1).add(task2)
self.assertEqual(2, len(f))
self.assertItemsEqual(f, [task1, task2])
self.assertCountEqual(f, [task1, task2])
self.assertEqual([(task1, task2, {'reasons': set(['a'])})],
list(f.iter_links()))
@ -167,7 +167,7 @@ class GraphFlowTest(test.TestCase):
self.assertEqual(3, len(f))
self.assertItemsEqual(list(f.iter_links()), [
self.assertCountEqual(list(f.iter_links()), [
(task1, task2, {'reasons': set(['a', 'b'])}),
(task2, task3, {'reasons': set(['c'])})
])
@ -178,7 +178,7 @@ class GraphFlowTest(test.TestCase):
f = gf.Flow('test').add(task1, task2)
linked = f.link(task1, task2)
self.assertIs(linked, f)
self.assertItemsEqual(list(f.iter_links()), [
self.assertCountEqual(list(f.iter_links()), [
(task1, task2, {'manual': True})
])
@ -192,7 +192,7 @@ class GraphFlowTest(test.TestCase):
'manual': True,
'reasons': set(['a'])
}
self.assertItemsEqual(list(f.iter_links()), [
self.assertCountEqual(list(f.iter_links()), [
(task1, task2, expected_meta)
])
@ -267,7 +267,7 @@ class TargetedGraphFlowTest(test.TestCase):
f.add(task1, task2, task3, task4)
f.set_target(task3)
self.assertEqual(3, len(f))
self.assertItemsEqual(f, [task1, task2, task3])
self.assertCountEqual(f, [task1, task2, task3])
self.assertNotIn('c', f.provides)
def test_targeted_flow_reset(self):
@ -280,7 +280,7 @@ class TargetedGraphFlowTest(test.TestCase):
f.set_target(task3)
f.reset_target()
self.assertEqual(4, len(f))
self.assertItemsEqual(f, [task1, task2, task3, task4])
self.assertCountEqual(f, [task1, task2, task3, task4])
self.assertIn('c', f.provides)
def test_targeted_flow_bad_target(self):
@ -297,7 +297,7 @@ class TargetedGraphFlowTest(test.TestCase):
f.add(task1)
f.set_target(task1)
self.assertEqual(1, len(f))
self.assertItemsEqual(f, [task1])
self.assertCountEqual(f, [task1])
def test_recache_on_add(self):
f = gf.TargetedFlow("test")

View File

@ -355,7 +355,7 @@ class RetryTest(utils.EngineTestBase):
'task1.t SUCCESS(5)',
'task2.t SUCCESS(None)',
'flow-1.f SUCCESS']
self.assertItemsEqual(capturer.values, expected)
self.assertCountEqual(capturer.values, expected)
def test_nested_flow_reverts_parent_retries(self):
retry1 = retry.Times(3, 'r1', provides='x')
@ -715,7 +715,7 @@ class RetryTest(utils.EngineTestBase):
'r1.r REVERTING',
'r1.r REVERTED(None)',
'flow-1.f REVERTED']
self.assertItemsEqual(capturer.values, expected)
self.assertCountEqual(capturer.values, expected)
def test_nested_for_each_revert(self):
collection = [3, 2, 3, 5]
@ -897,7 +897,7 @@ class RetryTest(utils.EngineTestBase):
'r1.r REVERTING',
'r1.r REVERTED(None)',
'flow-1.f REVERTED']
self.assertItemsEqual(capturer.values, expected)
self.assertCountEqual(capturer.values, expected)
def test_nested_parameterized_for_each_revert(self):
values = [3, 2, 5]
@ -1144,7 +1144,7 @@ class RetryTest(utils.EngineTestBase):
'c.t REVERTED(None)',
'b.t REVERTING',
'b.t REVERTED(None)']
self.assertItemsEqual(capturer.values[:8], expected)
self.assertCountEqual(capturer.values[:8], expected)
# Task 'a' was or was not executed again, both cases are ok.
self.assertIsSuperAndSubsequence(capturer.values[8:], [
'b.t RUNNING',
@ -1173,7 +1173,7 @@ class RetryTest(utils.EngineTestBase):
'c.t REVERTED(None)',
'b.t REVERTING',
'b.t REVERTED(None)']
self.assertItemsEqual(capturer.values[:4], expected)
self.assertCountEqual(capturer.values[:4], expected)
expected = ['test2_retry.r RETRYING',
'b.t PENDING',
'c.t PENDING',
@ -1185,7 +1185,7 @@ class RetryTest(utils.EngineTestBase):
'c.t RUNNING',
'a.t SUCCESS(5)',
'c.t SUCCESS(5)']
self.assertItemsEqual(expected, capturer.values[4:])
self.assertCountEqual(expected, capturer.values[4:])
self.assertEqual(st.SUCCESS, engine.storage.get_flow_state())
@ -1225,7 +1225,7 @@ class RetryParallelExecutionTest(utils.EngineTestBase):
'task2.t RUNNING',
'task2.t SUCCESS(None)',
'task1.t SUCCESS(5)']
self.assertItemsEqual(capturer.values, expected)
self.assertCountEqual(capturer.values, expected)
def test_when_subflow_fails_revert_success_tasks(self):
waiting_task = utils.WaitForOneFromTask('task2', 'task1',
@ -1268,7 +1268,7 @@ class RetryParallelExecutionTest(utils.EngineTestBase):
'task2.t SUCCESS(5)',
'task3.t RUNNING',
'task3.t SUCCESS(None)']
self.assertItemsEqual(capturer.values, expected)
self.assertCountEqual(capturer.values, expected)
class SerialEngineTest(RetryTest, test.TestCase):