Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_archive.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 | ||||
from copy import deepcopy | |||||
import hashlib | 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 ( | from swh.model.model import ( | ||||
Directory, | Directory, | ||||
DirectoryEntry, | DirectoryEntry, | ||||
Origin, | Origin, | ||||
OriginVisit, | OriginVisit, | ||||
Revision, | Revision, | ||||
Snapshot, | Snapshot, | ||||
SnapshotBranch, | SnapshotBranch, | ||||
TargetType, | TargetType, | ||||
) | ) | ||||
from swh.web.common import archive | from swh.web.common import archive | ||||
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.common.typing import OriginInfo, PagedResult | ||||
from swh.web.config import get_config | |||||
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 970 Lines • ▼ Show 20 Lines | def test_lookup_origins_get_by_sha1s(origin, unknown_origin): | ||||
origins = list(archive.lookup_origins_by_sha1s([origin_sha1, origin_sha1])) | origins = list(archive.lookup_origins_by_sha1s([origin_sha1, origin_sha1])) | ||||
assert origins == [origin_info, origin_info] | assert origins == [origin_info, origin_info] | ||||
origins = list(archive.lookup_origins_by_sha1s([origin_sha1, unknown_origin_sha1])) | origins = list(archive.lookup_origins_by_sha1s([origin_sha1, unknown_origin_sha1])) | ||||
assert origins == [origin_info, None] | assert origins == [origin_info, None] | ||||
@given(origin()) | |||||
def test_search_origin(origin): | |||||
results = archive.search_origin(url_pattern=origin["url"])[0] | |||||
assert results == [{"url": origin["url"]}] | |||||
@given(origin()) | |||||
def test_search_origin_use_ql(mocker, origin): | |||||
config = deepcopy(get_config()) | |||||
config["search_config"]["enable_ql"] = True | |||||
mock_get_config = mocker.patch("swh.web.config.get_config") | |||||
mock_get_config.return_value = config | |||||
ORIGIN = [{"url": origin["url"]}] | |||||
mock_archive_search = mocker.patch("swh.web.common.archive.search") | |||||
mock_archive_search.origin_search.return_value = PagedResult( | |||||
results=ORIGIN, next_page_token=None, | |||||
) | |||||
results = archive.search_origin( | |||||
url_pattern=f"origin = '{origin['url']}'", use_ql=True | |||||
)[0] | |||||
assert results == ORIGIN | |||||
@given(snapshot()) | @given(snapshot()) | ||||
def test_lookup_snapshot_sizes(archive_data, snapshot): | def test_lookup_snapshot_sizes(archive_data, snapshot): | ||||
branches = archive_data.snapshot_get(snapshot)["branches"] | branches = archive_data.snapshot_get(snapshot)["branches"] | ||||
expected_sizes = { | expected_sizes = { | ||||
"alias": 0, | "alias": 0, | ||||
"release": 0, | "release": 0, | ||||
"revision": 0, | "revision": 0, | ||||
▲ Show 20 Lines • Show All 185 Lines • Show Last 20 Lines |