Skip to content
Snippets Groups Projects
Commit 2c1e02b8 authored by vlorentz's avatar vlorentz
Browse files

Add method BaseModel.hashes().

Can be useful to deduplicate code in swh-storage.
parent fcfbd4de
No related branches found
Tags v0.0.45
No related merge requests found
......@@ -384,6 +384,10 @@ class BaseContent(BaseModel):
raise ValueError('{} is not a valid hash name.'.format(hash_name))
return getattr(self, hash_name)
def hashes(self) -> Dict[str, bytes]:
"""Returns a dictionary {hash_name: hash_value}"""
return {algo: getattr(self, algo) for algo in DEFAULT_ALGORITHMS}
@attr.s(frozen=True)
class Content(BaseContent):
......
......@@ -62,6 +62,13 @@ def test_content_get_hash():
assert c.get_hash(hash_name) == hash_
def test_content_hashes():
hashes = dict(
sha1=b'foo', sha1_git=b'bar', sha256=b'baz', blake2s256=b'qux')
c = Content(length=42, status='visible', **hashes)
assert c.hashes() == hashes
def test_directory_model_id_computation():
dir_dict = dict(directory_example)
del dir_dict['id']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment