Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_service.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from collections import defaultdict | from collections import defaultdict | ||||
import hashlib | |||||
import itertools | import itertools | ||||
import random | import random | ||||
from hypothesis import given | from hypothesis import given | ||||
import pytest | import pytest | ||||
from swh.model.from_disk import DentryPerms | from swh.model.from_disk import DentryPerms | ||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_bytes, hash_to_hex | ||||
from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | ||||
from swh.model.model import Directory, DirectoryEntry, Origin, OriginVisit, Revision | from swh.model.model import Directory, DirectoryEntry, Origin, OriginVisit, Revision | ||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.exc import BadInputExc, NotFoundExc | from swh.web.common.exc import BadInputExc, NotFoundExc | ||||
from swh.web.common.typing import OriginInfo | |||||
from swh.web.tests.conftest import ctags_json_missing, fossology_missing | from swh.web.tests.conftest import ctags_json_missing, fossology_missing | ||||
from swh.web.tests.data import random_content, random_sha1 | from swh.web.tests.data import random_content, random_sha1 | ||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
ancestor_revisions, | ancestor_revisions, | ||||
content, | content, | ||||
contents, | contents, | ||||
contents_with_ctags, | contents_with_ctags, | ||||
directory, | directory, | ||||
▲ Show 20 Lines • Show All 961 Lines • ▼ Show 20 Lines | def test_lookup_snapshot_branch_name_from_tip_revision(archive_data, snapshot_id): | ||||
] | ] | ||||
assert ( | assert ( | ||||
service.lookup_snapshot_branch_name_from_tip_revision( | service.lookup_snapshot_branch_name_from_tip_revision( | ||||
snapshot_id, branch_info["revision"] | snapshot_id, branch_info["revision"] | ||||
) | ) | ||||
in possible_results | in possible_results | ||||
) | ) | ||||
@given(origin(), new_origin()) | |||||
def test_lookup_origins_get_by_sha1s(origin, unknown_origin): | |||||
hasher = hashlib.sha1() | |||||
hasher.update(origin["url"].encode("ascii")) | |||||
origin_info = OriginInfo(url=origin["url"]) | |||||
origin_sha1 = hasher.hexdigest() | |||||
hasher = hashlib.sha1() | |||||
hasher.update(unknown_origin.url.encode("ascii")) | |||||
unknown_origin_sha1 = hasher.hexdigest() | |||||
origins = list(service.lookup_origins_by_sha1s([origin_sha1])) | |||||
assert origins == [origin_info] | |||||
origins = list(service.lookup_origins_by_sha1s([origin_sha1, origin_sha1])) | |||||
assert origins == [origin_info, origin_info] | |||||
origins = list(service.lookup_origins_by_sha1s([origin_sha1, unknown_origin_sha1])) | |||||
assert origins == [origin_info, None] |