| 1 |
from django.test import SimpleTestCase |
| 2 |
from django.utils.hashable import make_hashable |
| 3 |
|
| 4 |
|
| 5 |
class TestHashable(SimpleTestCase): |
| 6 |
def test_equal(self): |
| 7 |
tests = ( |
| 8 |
([], ()), |
| 9 |
(["a", 1], ("a", 1)), |
| 10 |
({}, ()), |
| 11 |
({"a"}, ("a",)), |
| 12 |
(frozenset({"a"}), {"a"}), |
| 13 |
({"a": 1, "b": 2}, (("a", 1), ("b", 2))), |
| 14 |
({"b": 2, "a": 1}, (("a", 1), ("b", 2))), |
| 15 |
(("a", ["b", 1]), ("a", ("b", 1))), |
| 16 |
(("a", {"b": 1}), ("a", (("b", 1),))), |
| 17 |
) |
| 18 |
for value, expected in tests: |
| 19 |
with self.subTest(value=value): |
| 20 |
self.assertEqual(make_hashable(value), expected) |
| 21 |
|
| 22 |
def test_count_equal(self): |
| 23 |
tests = ( |
| 24 |
({"a": 1, "b": ["a", 1]}, (("a", 1), ("b", ("a", 1)))), |
| 25 |
({"a": 1, "b": ("a", [1, 2])}, (("a", 1), ("b", ("a", (1, 2))))), |
| 26 |
) |
| 27 |
for value, expected in tests: |
| 28 |
with self.subTest(value=value): |
| 29 |
self.assertCountEqual(make_hashable(value), expected) |
| 30 |
|
| 31 |
def test_unhashable(self): |
| 32 |
class Unhashable: |
| 33 |
__hash__ = None |
| 34 |
|
| 35 |
with self.assertRaisesMessage(TypeError, "unhashable type: 'Unhashable'"): |
| 36 |
make_hashable(Unhashable()) |