Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Moving util.dict_to_tuple_key() into only module that uses it. #429

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions oauth2client/contrib/multistore_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ class NewerCredentialStoreError(Error):
"""The credential store is a newer version than supported."""


def _dict_to_tuple_key(dictionary):
"""Converts a dictionary to a tuple that can be used as an immutable key.

The resulting key is always sorted so that logically equivalent
dictionaries always produce an identical tuple for a key.

Args:
dictionary: the dictionary to use as the key.

Returns:
A tuple representing the dictionary in it's naturally sorted ordering.
"""
return tuple(sorted(dictionary.items()))


@util.positional(4)
def get_credential_storage(filename, client_id, user_agent, scope,
warn_on_readonly=True):
Expand Down Expand Up @@ -139,7 +154,7 @@ def get_credential_storage_custom_key(filename, key_dict,
credential.
"""
multistore = _get_multistore(filename, warn_on_readonly=warn_on_readonly)
key = util.dict_to_tuple_key(key_dict)
key = _dict_to_tuple_key(key_dict)
return multistore._get_storage(key)


Expand Down Expand Up @@ -404,7 +419,7 @@ def _decode_credential_from_json(self, cred_entry):
OAuth2Credential object.
"""
raw_key = cred_entry['key']
key = util.dict_to_tuple_key(raw_key)
key = _dict_to_tuple_key(raw_key)
credential = None
credential = Credentials.new_from_json(
json.dumps(cred_entry['credential']))
Expand Down
15 changes: 0 additions & 15 deletions oauth2client/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,6 @@ def string_to_scopes(scopes):
return scopes


def dict_to_tuple_key(dictionary):
"""Converts a dictionary to a tuple that can be used as an immutable key.

The resulting key is always sorted so that logically equivalent
dictionaries always produce an identical tuple for a key.

Args:
dictionary: the dictionary to use as the key.

Returns:
A tuple representing the dictionary in it's naturally sorted ordering.
"""
return tuple(sorted(dictionary.items()))


def _add_query_parameter(url, name, value):
"""Adds a query parameter to a url.

Expand Down
28 changes: 28 additions & 0 deletions tests/contrib/test_multistore_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ def filename(self):
return self.filename_str


class Test__dict_to_tuple_key(unittest2.TestCase):

def test_key_conversions(self):
key1, val1 = 'somekey', 'some value'
key2, val2 = 'another', 'something else'
key3, val3 = 'onemore', 'foo'
test_dict = {
key1: val1,
key2: val2,
key3: val3,
}
tuple_key = multistore_file._dict_to_tuple_key(test_dict)

# the resulting key should be naturally sorted
expected_output = (
(key2, val2),
(key3, val3),
(key1, val1),
)
self.assertTupleEqual(expected_output, tuple_key)
# check we get the original dictionary back
self.assertDictEqual(test_dict, dict(tuple_key))


class MultistoreFileTests(unittest2.TestCase):

def tearDown(self):
Expand Down Expand Up @@ -257,3 +281,7 @@ def test_multistore_file_get_all_keys(self):
store2.delete()
keys = multistore_file.get_all_credential_keys(FILENAME)
self.assertEquals([], keys)


if __name__ == '__main__': # pragma: NO COVER
unittest2.main()
18 changes: 0 additions & 18 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,5 @@ def test_conversion(self):
self.assertEqual(expected, util.string_to_scopes(case))


class KeyConversionTests(unittest.TestCase):

def test_key_conversions(self):
d = {'somekey': 'some value', 'another': 'something else',
'onemore': 'foo'}
tuple_key = util.dict_to_tuple_key(d)

# the resulting key should be naturally sorted
self.assertEqual(
(('another', 'something else'),
('onemore', 'foo'),
('somekey', 'some value')),
tuple_key)

# check we get the original dictionary back
self.assertEqual(d, dict(tuple_key))


if __name__ == '__main__': # pragma: NO COVER
unittest.main()