diff --git a/swh/model/model.py b/swh/model/model.py index 512824d60a1ab05254dbb7649d6f0ba2ffc54bd6..26accb65b0f584bc65fd4dc2ac7e2ce9be5cc3fd 100644 --- a/swh/model/model.py +++ b/swh/model/model.py @@ -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): diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py index 5560127ed87e1f7e961c25e00e9d8c39db492d07..e905725d5624ce1ad311292c9e2111e3b45905a2 100644 --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -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']