Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure put returns key #35

Merged
merged 1 commit into from
May 6, 2021
Merged
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
4 changes: 2 additions & 2 deletions tests/basic_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def is_emulated_gcstore_test(store):

class BasicStore(object):
def test_store(self, store, key, value):
key = store.put(key, value)
assert isinstance(key, str)
new_key = store.put(key, value)
assert key == new_key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have seen some cases (in the redis store?) where you can have new_key != key is key is None. Should we handle that here, too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we return the key:

def _put(
self, key: str, value: bytes, ttl_secs: Optional[Union[str, int, float]] = None
) -> str:
assert ttl_secs is not None
if ttl_secs in (NOT_SET, FOREVER):
# if we do not care about ttl, just use set
# in redis, using SET will also clear the timeout
# note that this assumes that there is no way in redis
# to set a default timeout on keys
self.redis.set(key, value)
else:
ittl = None
try:
ittl = int(ttl_secs)
except ValueError:
pass # let it blow up further down
if ittl == ttl_secs:
self.redis.setex(key, ittl, value)
else:
self.redis.psetex(key, int(ttl_secs * 1000), value)
return key

I guess there it the KeyTransformingDecorator (undocumented):

def put(self, key: str, *args, **kwargs): # noqa D
return self._unmap_key(self._dstore.put(self._map_key(key), *args, **kwargs))

and the HashDecorator:

def put(self, key: Optional[str], data: bytes, *args, **kwargs):
"""Store bytestring data at key.
Parameters
----------
key : str or None
The key under which the data is to be stored. If None, the hash of data is
used.
data : bytes
Data to be stored at key, must be of type ``bytes``.
Returns
-------
str
The key under which data was stored.
Raises
------
ValueError
If the key is not valid.
IOError
If storing failed or the file could not be read.
"""
if key is None:
key = self._template.format(self.hashfunc(data).hexdigest())
return self._dstore.put(key, data, *args, **kwargs) # type: ignore

The former is surely untested (else tests would be failing).


def test_unicode_store(self, store, key, unicode_value):
with pytest.raises(IOError):
Expand Down