Skip to content

Commit

Permalink
Merge pull request #9352 Fix and rename assertUnhashableCountEqual.
Browse files Browse the repository at this point in the history
Rename assertArrayCountEqual to assertUnhashableCountEqual and fix typo
  • Loading branch information
robertwb authored Aug 23, 2019
2 parents aa8b29b + aeada18 commit f085cb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions sdks/python/apache_beam/testing/extra_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def assertCountEqual(self, first, second, msg=None):
"""
return self.assertItemsEqual(first, second, msg=msg)

def assertArrayCountEqual(self, data1, data2):
def assertUnhashableCountEqual(self, data1, data2):
"""Assert that two containers have the same items, with special treatment
for numpy arrays.
"""
try:
self.assertCountEqual(data1, data2)
except (TypeError, ValueError):
data1 = [self._to_hashable(d) for d in data1]
data2 = [self._to_hashable(d) for d in data1]
data2 = [self._to_hashable(d) for d in data2]
self.assertCountEqual(data1, data2)

def _to_hashable(self, element):
Expand All @@ -54,7 +54,7 @@ def _to_hashable(self, element):

if isinstance(element, dict):
hashable_elements = []
for key, value in element.items():
for key, value in sorted(element.items(), key=lambda t: hash(t[0])):
hashable_elements.append((key, self._to_hashable(value)))
return tuple(hashable_elements)

Expand Down
23 changes: 12 additions & 11 deletions sdks/python/apache_beam/testing/extra_assertions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ class ExtraAssertionsMixinTest(ExtraAssertionsMixin, unittest.TestCase):
def test_assert_array_count_equal_strings(self):
data1 = [u"±♠Ωℑ", u"hello", "world"]
data2 = ["hello", u"±♠Ωℑ", u"world"]
self.assertArrayCountEqual(data1, data2)
self.assertUnhashableCountEqual(data1, data2)

def test_assert_array_count_equal_mixed(self):
# TODO(ostrokach): Add a timeout, since if assertArrayCountEqual is not
# implemented efficiently, this test has the potential to run for a very
# long time.
data1 = [
#
{'a': 1, 123: 1.234},
['d', 1],
u"±♠Ωℑ",
Expand All @@ -47,11 +43,10 @@ def test_assert_array_count_equal_mixed(self):
100,
'abc',
('a', 'b', 'c'),
None
None,
]
data2 = [
#
{'a': 1, 123: 1.234},
{123: 1.234, 'a': 1},
('a', 'b', 'c'),
['d', 1],
None,
Expand All @@ -60,10 +55,16 @@ def test_assert_array_count_equal_mixed(self):
u"±♠Ωℑ",
100,
(1, 2, 3, 'b'),
np.zeros((3, 6))
np.zeros((3, 6)),
]
self.assertArrayCountEqual(data1, data2)
self.assertArrayCountEqual(data1 * 2, data2 * 2)
self.assertUnhashableCountEqual(data1, data2)
self.assertUnhashableCountEqual(data1 * 2, data2 * 2)

def test_assert_not_equal(self):
data1 = [{'a': 123, 'b': 321}, [1, 2, 3]]
data2 = [{'a': 123, 'c': 321}, [1, 2, 3]]
with self.assertRaises(AssertionError):
self.assertUnhashableCountEqual(data1, data2)


if __name__ == '__main__':
Expand Down

0 comments on commit f085cb5

Please sign in to comment.