Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_archive.py
# Copyright (C) 2015-2021 The Software Heritage developers | # Copyright (C) 2015-2021 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 ObjectType | from swh.model.identifiers import ObjectType | ||||
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, PagedResult | 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 981 Lines • ▼ Show 20 Lines | |||||
@given(origin()) | @given(origin()) | ||||
def test_search_origin(origin): | def test_search_origin(origin): | ||||
results = archive.search_origin(url_pattern=origin["url"])[0] | results = archive.search_origin(url_pattern=origin["url"])[0] | ||||
assert results == [{"url": origin["url"]}] | assert results == [{"url": origin["url"]}] | ||||
@given(origin()) | @given(origin()) | ||||
def test_search_origin_use_ql(mocker, 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"]}] | ORIGIN = [{"url": origin["url"]}] | ||||
mock_archive_search = mocker.patch("swh.web.common.archive.search") | mock_archive_search = mocker.patch("swh.web.common.archive.search") | ||||
mock_archive_search.origin_search.return_value = PagedResult( | mock_archive_search.origin_search.return_value = PagedResult( | ||||
results=ORIGIN, next_page_token=None, | results=ORIGIN, next_page_token=None, | ||||
) | ) | ||||
results = archive.search_origin( | query = f"origin = '{origin['url']}'" | ||||
url_pattern=f"origin = '{origin['url']}'", use_ql=True | |||||
)[0] | results = archive.search_origin(url_pattern=query, use_ql=True)[0] | ||||
assert results == ORIGIN | assert results == ORIGIN | ||||
mock_archive_search.origin_search.assert_called_with( | |||||
query=query, page_token=None, with_visit=False, visit_types=None, limit=50 | |||||
) | |||||
@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, | ||||
▲ Show 20 Lines • Show All 186 Lines • Show Last 20 Lines |