2018-10-16 15:01:31 +00:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.utils.hashable import make_hashable
|
|
|
|
|
|
|
|
|
|
|
|
class TestHashable(SimpleTestCase):
|
|
|
|
def test_equal(self):
|
|
|
|
tests = (
|
|
|
|
([], ()),
|
|
|
|
(['a', 1], ('a', 1)),
|
|
|
|
({}, ()),
|
2018-10-16 15:02:36 +00:00
|
|
|
({'a'}, ('a',)),
|
2018-10-16 15:01:31 +00:00
|
|
|
(frozenset({'a'}), {'a'}),
|
2020-10-05 13:13:14 +00:00
|
|
|
({'a': 1, 'b': 2}, (('a', 1), ('b', 2))),
|
|
|
|
({'b': 2, 'a': 1}, (('a', 1), ('b', 2))),
|
2018-10-16 15:02:36 +00:00
|
|
|
(('a', ['b', 1]), ('a', ('b', 1))),
|
|
|
|
(('a', {'b': 1}), ('a', (('b', 1),))),
|
2018-10-16 15:01:31 +00:00
|
|
|
)
|
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest(value=value):
|
|
|
|
self.assertEqual(make_hashable(value), expected)
|
|
|
|
|
|
|
|
def test_count_equal(self):
|
|
|
|
tests = (
|
|
|
|
({'a': 1, 'b': ['a', 1]}, (('a', 1), ('b', ('a', 1)))),
|
2018-10-16 15:02:36 +00:00
|
|
|
({'a': 1, 'b': ('a', [1, 2])}, (('a', 1), ('b', ('a', (1, 2))))),
|
2018-10-16 15:01:31 +00:00
|
|
|
)
|
|
|
|
for value, expected in tests:
|
|
|
|
with self.subTest(value=value):
|
|
|
|
self.assertCountEqual(make_hashable(value), expected)
|
2018-10-16 15:02:36 +00:00
|
|
|
|
|
|
|
def test_unhashable(self):
|
|
|
|
class Unhashable:
|
|
|
|
__hash__ = None
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(TypeError, "unhashable type: 'Unhashable'"):
|
|
|
|
make_hashable(Unhashable())
|