Skip to content

Commit 2644b07

Browse files
authored
Sem-Ver: feature Allow SettingsDict instances to be hashed.
Signed-off-by: David Black <[email protected]>
1 parent 1037481 commit 2644b07

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

atlassian_jwt_auth/frameworks/common/tests/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
3+
from atlassian_jwt_auth.frameworks.common import utils
4+
5+
6+
class SettingsDictTest(unittest.TestCase):
7+
""" Tests for the SettingsDict class. """
8+
9+
def test_hash(self):
10+
""" Test that SettingsDict instances can be hashed. """
11+
dictionary_one = {'a': 'b', '3': set([1]), 'f': None}
12+
dictionary_two = {'a': 'b', '3': set([1]), 'f': None}
13+
dictionary_three = {'a': 'b', '3': set([1]), 'diff': '333'}
14+
settings_one = utils.SettingsDict(dictionary_one)
15+
settings_two = utils.SettingsDict(dictionary_two)
16+
settings_three = utils.SettingsDict(dictionary_three)
17+
self.assertEqual(settings_one, settings_two)
18+
self.assertEqual(hash(settings_one), hash(settings_two))
19+
self.assertNotEqual(settings_one, settings_three)
20+
self.assertNotEqual(hash(settings_one), hash(settings_three))

atlassian_jwt_auth/frameworks/common/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ def __getattr__(self, name):
77

88
def __setitem__(self, key, value):
99
raise AttributeError('SettingsDict properties are immutable')
10+
11+
def _hash_key(self):
12+
keys_and_values = []
13+
for key, value in self.items():
14+
if isinstance(value, set):
15+
value = frozenset(value)
16+
keys_and_values.append("%s %s" % (key, hash(value)))
17+
return frozenset(keys_and_values)
18+
19+
def __hash__(self):
20+
return hash(self._hash_key())
21+
22+
def __eq__(self, other):
23+
return hash(self) == hash(other)

0 commit comments

Comments
 (0)