testlib: fix for python 3.3

unittest.TestCase no longer has a assertSameElements method. Use assertCountEqual instead of assertSameElements/assertItemsEqual. Closes #144526

authorJulien Cristau <julien.cristau@logilab.fr>
changeset49aeb5521e29
branchstable
phasepublic
hiddenno
parent revision#aefd37dbb754 Fixed: Install fails on Windows with Python 3.3. Calling subprocess with
child revision#9e4eb6f9c594 graph: use codecs.open. Closes #155138
files modified by this revision
test/unittest_cache.py
testlib.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1374475895 -7200
# Mon Jul 22 08:51:35 2013 +0200
# Branch stable
# Node ID 49aeb5521e2962fce18f5d02323ac8668dfd88b3
# Parent aefd37dbb754a2d96f006f8eddd629bab61e93e0
testlib: fix for python 3.3

unittest.TestCase no longer has a assertSameElements method.
Use assertCountEqual instead of assertSameElements/assertItemsEqual.
Closes #144526

diff --git a/test/unittest_cache.py b/test/unittest_cache.py
@@ -31,11 +31,11 @@
1          self.cache[1] = 'foo'
2          self.assertEqual(self.cache[1], 'foo', "1:foo is not in cache")
3          self.assertEqual(len(self.cache._usage), 1)
4          self.assertEqual(self.cache._usage[-1], 1,
5                           '1 is not the most recently used key')
6 -        self.assertItemsEqual(self.cache._usage,
7 +        self.assertCountEqual(self.cache._usage,
8                                self.cache.keys(),
9                                "usage list and data keys are different")
10 
11      def test_setitem2(self):
12          """Checks that the setitem method works for multiple items"""
@@ -45,21 +45,21 @@
13                           "2 : 'bar' is not in cache.data")
14          self.assertEqual(len(self.cache._usage), 2,
15                           "lenght of usage list is not 2")
16          self.assertEqual(self.cache._usage[-1], 2,
17                       '1 is not the most recently used key')
18 -        self.assertItemsEqual(self.cache._usage,
19 +        self.assertCountEqual(self.cache._usage,
20                                self.cache.keys())# usage list and data keys are different
21 
22      def test_setitem3(self):
23          """Checks that the setitem method works when replacing an element in the cache"""
24          self.cache[1] = 'foo'
25          self.cache[1] = 'bar'
26          self.assertEqual(self.cache[1], 'bar', "1 : 'bar' is not in cache.data")
27          self.assertEqual(len(self.cache._usage), 1, "lenght of usage list is not 1")
28          self.assertEqual(self.cache._usage[-1], 1, '1 is not the most recently used key')
29 -        self.assertItemsEqual(self.cache._usage,
30 +        self.assertCountEqual(self.cache._usage,
31                                self.cache.keys())# usage list and data keys are different
32 
33      def test_recycling1(self):
34          """Checks the removal of old elements"""
35          self.cache[1] = 'foo'
@@ -72,11 +72,11 @@
36                       'key 1 has not been suppressed from the cache dictionnary')
37          self.assertTrue(1 not in self.cache._usage,
38                       'key 1 has not been suppressed from the cache LRU list')
39          self.assertEqual(len(self.cache._usage), 5, "lenght of usage list is not 5")
40          self.assertEqual(self.cache._usage[-1], 6, '6 is not the most recently used key')
41 -        self.assertItemsEqual(self.cache._usage,
42 +        self.assertCountEqual(self.cache._usage,
43                                self.cache.keys())# usage list and data keys are different
44 
45      def test_recycling2(self):
46          """Checks that accessed elements get in the front of the list"""
47          self.cache[1] = 'foo'
@@ -84,22 +84,22 @@
48          self.cache[3] = 'baz'
49          self.cache[4] = 'foz'
50          a = self.cache[1]
51          self.assertEqual(a, 'foo')
52          self.assertEqual(self.cache._usage[-1], 1, '1 is not the most recently used key')
53 -        self.assertItemsEqual(self.cache._usage,
54 +        self.assertCountEqual(self.cache._usage,
55                                self.cache.keys())# usage list and data keys are different
56 
57      def test_delitem(self):
58          """Checks that elements are removed from both element dict and element
59          list.
60          """
61          self.cache['foo'] = 'bar'
62          del self.cache['foo']
63          self.assertTrue('foo' not in self.cache.keys(), "Element 'foo' was not removed cache dictionnary")
64          self.assertTrue('foo' not in self.cache._usage, "Element 'foo' was not removed usage list")
65 -        self.assertItemsEqual(self.cache._usage,
66 +        self.assertCountEqual(self.cache._usage,
67                                self.cache.keys())# usage list and data keys are different
68 
69 
70      def test_nullsize(self):
71          """Checks that a 'NULL' size cache doesn't store anything
diff --git a/testlib.py b/testlib.py
@@ -719,11 +719,11 @@
72                  base = '%s\n' % context
73              else:
74                  base = ''
75              self.fail(base + '\n'.join(msgs))
76 
77 -    @deprecated('Please use assertItemsEqual instead.')
78 +    @deprecated('Please use assertCountEqual instead.')
79      def assertUnorderedIterableEquals(self, got, expected, msg=None):
80          """compares two iterable and shows difference between both
81 
82          :param got: the unordered Iterable that we found
83          :param expected: the expected unordered Iterable
@@ -1181,14 +1181,17 @@
84                  excName = str(excClass)
85              raise self.failureException("%s not raised" % excName)
86 
87      assertRaises = failUnlessRaises
88 
89 -    if not hasattr(unittest.TestCase, 'assertItemsEqual'):
90 -        # python 3.2 has deprecated assertSameElements and is missing
91 -        # assertItemsEqual
92 -        assertItemsEqual = unittest.TestCase.assertSameElements
93 +    if sys.version_info >= (3,2):
94 +        assertItemsEqual = unittest.TestCase.assertCountEqual
95 +    else:
96 +        assertCountEqual = unittest.TestCase.assertItemsEqual
97 +
98 +TestCase.assertItemsEqual = deprecated('assertItemsEqual is deprecated, use assertCountEqual')(
99 +    TestCase.assertItemsEqual)
100 
101  import doctest
102 
103  class SkippedSuite(unittest.TestSuite):
104      def test(self):