diff --git a/swh/storage/algos/diff.py b/swh/storage/algos/diff.py --- a/swh/storage/algos/diff.py +++ b/swh/storage/algos/diff.py @@ -1,4 +1,4 @@ -# Copyright (C) 2018 The Software Heritage developers +# Copyright (C) 2018-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -14,20 +14,27 @@ import collections +from typing import Any, Dict + from swh.model.hashutil import hash_to_bytes from swh.model.identifiers import directory_identifier +from swh.storage.interface import StorageInterface + from .dir_iterators import DirectoryIterator, DoubleDirectoryIterator, Remaining + # get the hash identifier for an empty directory _empty_dir_hash = hash_to_bytes(directory_identifier({"entries": []})) -def _get_rev(storage, rev_id): +def _get_rev(storage: StorageInterface, rev_id: bytes) -> Dict[str, Any]: """ Return revision data from swh storage. """ - return list(storage.revision_get([rev_id]))[0] + revision = storage.revision_get([rev_id])[0] + assert revision is not None + return revision.to_dict() class _RevisionChangesList(object): diff --git a/swh/storage/tests/algos/test_diff.py b/swh/storage/tests/algos/test_diff.py --- a/swh/storage/tests/algos/test_diff.py +++ b/swh/storage/tests/algos/test_diff.py @@ -1,11 +1,13 @@ -# Copyright (C) 2018 The Software Heritage developers +# Copyright (C) 2018-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information # flake8: noqa +import pytest import unittest + from unittest.mock import patch from swh.model.hashutil import hash_to_bytes @@ -15,6 +17,19 @@ from .test_dir_iterator import DirectoryModel +def test__get_rev(swh_storage, sample_data): + revision = sample_data.revision + + # does not exist then raises + with pytest.raises(AssertionError): + diff._get_rev(swh_storage, revision.id) + + # otherwise, we retrieve its dict representation + swh_storage.revision_add([revision]) + actual_revision = diff._get_rev(swh_storage, revision.id) + assert actual_revision == revision.to_dict() + + @patch("swh.storage.algos.diff._get_rev") @patch("swh.storage.algos.dir_iterators._get_dir") class TestDiffRevisions(unittest.TestCase):