diff --git a/swh/web/tests/api/test_apiresponse.py b/swh/web/tests/api/test_apiresponse.py --- a/swh/web/tests/api/test_apiresponse.py +++ b/swh/web/tests/api/test_apiresponse.py @@ -10,7 +10,6 @@ ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ) -from hypothesis import given from swh.model.swhids import ObjectType from swh.web.api.apiresponse import ( @@ -22,7 +21,6 @@ from swh.web.common.identifiers import gen_swhid from swh.web.common.utils import reverse from swh.web.tests.django_asserts import assert_contains -from swh.web.tests.strategies import revision from swh.web.tests.utils import check_http_get_response, check_http_post_response @@ -153,7 +151,6 @@ assert "Traceback" in resp.data["traceback"] -@given(revision()) def test_api_endpoints_have_cors_headers(client, content, directory, revision): url = reverse("api-1-stat-counters") diff --git a/swh/web/tests/api/test_utils.py b/swh/web/tests/api/test_utils.py --- a/swh/web/tests/api/test_utils.py +++ b/swh/web/tests/api/test_utils.py @@ -11,7 +11,7 @@ from swh.web.api import utils from swh.web.common.origin_visits import get_origin_visits from swh.web.common.utils import resolve_branch_alias, reverse -from swh.web.tests.strategies import revision, snapshot +from swh.web.tests.strategies import snapshot url_map = [ { @@ -287,7 +287,6 @@ assert enriched_content == content_data -@given(revision()) def test_enrich_revision_without_children_or_parent( api_request_factory, archive_data, revision ): @@ -317,11 +316,10 @@ assert actual_revision == revision_data -@given(revision(), revision(), revision()) def test_enrich_revision_with_children_and_parent_no_dir( - api_request_factory, archive_data, revision, parent_revision, child_revision + api_request_factory, archive_data, revisions_list ): - + revision, parent_revision, child_revision = revisions_list(size=3) revision_data = archive_data.revision_get(revision) del revision_data["directory"] revision_data["parents"] = revision_data["parents"] + (parent_revision,) @@ -359,11 +357,8 @@ assert actual_revision == revision_data -@given(revision(), revision(), revision()) -def test_enrich_revision_no_context( - api_request_factory, revision, parent_revision, child_revision -): - +def test_enrich_revisionno_context(api_request_factory, revisions_list): + revision, parent_revision, child_revision = revisions_list(size=3) revision_data = { "id": revision, "parents": [parent_revision], @@ -403,11 +398,10 @@ assert actual_revision == revision_data -@given(revision(), revision(), revision()) def test_enrich_revision_with_no_message( - api_request_factory, archive_data, revision, parent_revision, child_revision + api_request_factory, archive_data, revisions_list ): - + revision, parent_revision, child_revision = revisions_list(size=3) revision_data = archive_data.revision_get(revision) revision_data["message"] = None revision_data["parents"] = revision_data["parents"] + (parent_revision,) @@ -451,11 +445,10 @@ assert actual_revision == revision_data -@given(revision(), revision(), revision()) def test_enrich_revision_with_invalid_message( - api_request_factory, archive_data, revision, parent_revision, child_revision + api_request_factory, archive_data, revisions_list ): - + revision, parent_revision, child_revision = revisions_list(size=3) revision_data = archive_data.revision_get(revision) revision_data["decoding_failures"] = ["message"] revision_data["parents"] = revision_data["parents"] + (parent_revision,) diff --git a/swh/web/tests/api/views/test_identifiers.py b/swh/web/tests/api/views/test_identifiers.py --- a/swh/web/tests/api/views/test_identifiers.py +++ b/swh/web/tests/api/views/test_identifiers.py @@ -10,7 +10,6 @@ from swh.web.common.utils import reverse from swh.web.tests.data import random_sha1 from swh.web.tests.strategies import ( - revision, snapshot, unknown_content, unknown_directory, @@ -21,9 +20,9 @@ from swh.web.tests.utils import check_api_get_responses, check_api_post_responses -@given(revision(), snapshot()) +@given(snapshot()) def test_swhid_resolve_success( - api_client, client, content, directory, origin, release, revision, snapshot + api_client, content, directory, origin, release, revision, snapshot ): for obj_type, obj_id in ( @@ -112,7 +111,7 @@ check_api_get_responses(api_client, url, status_code=400) -@given(revision(), snapshot()) +@given(snapshot()) def test_api_known_swhid_all_present( api_client, content, directory, release, revision, snapshot ): diff --git a/swh/web/tests/api/views/test_revision.py b/swh/web/tests/api/views/test_revision.py --- a/swh/web/tests/api/views/test_revision.py +++ b/swh/web/tests/api/views/test_revision.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2019 The Software Heritage developers +# Copyright (C) 2015-2021 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -17,11 +17,10 @@ from swh.web.api.utils import enrich_content, enrich_directory_entry, enrich_revision from swh.web.common.utils import reverse from swh.web.tests.data import random_sha1 -from swh.web.tests.strategies import new_person, new_swh_date, revision +from swh.web.tests.strategies import new_person, new_swh_date from swh.web.tests.utils import check_api_get_responses, check_http_get_response -@given(revision()) def test_api_revision(api_client, archive_data, revision): url = reverse("api-1-revision", url_args={"sha1_git": revision}) rv = check_api_get_responses(api_client, url, status_code=200) @@ -44,7 +43,6 @@ } -@given(revision()) def test_api_revision_raw_ok(api_client, archive_data, revision): url = reverse("api-1-revision-raw-message", url_args={"sha1_git": revision}) @@ -68,7 +66,6 @@ } -@given(revision()) def test_api_revision_log(api_client, archive_data, revision): limit = 10 @@ -111,7 +108,6 @@ } -@given(revision()) def test_api_revision_directory_ok_returns_dir_entries( api_client, archive_data, revision ): @@ -176,7 +172,7 @@ } -@given(revision(), new_person(), new_swh_date()) +@given(new_person(), new_swh_date()) def test_api_revision_directory_ok_returns_revision( api_client, archive_data, revision, person, date ): @@ -220,7 +216,6 @@ } -@given(revision()) def test_api_revision_uppercase(api_client, revision): url = reverse( "api-1-revision-uppercase-checksum", url_args={"sha1_git": revision.upper()} diff --git a/swh/web/tests/api/views/test_vault.py b/swh/web/tests/api/views/test_vault.py --- a/swh/web/tests/api/views/test_vault.py +++ b/swh/web/tests/api/views/test_vault.py @@ -11,7 +11,7 @@ from swh.model.swhids import CoreSWHID from swh.vault.exc import NotFoundExc from swh.web.common.utils import reverse -from swh.web.tests.strategies import revision, unknown_directory, unknown_revision +from swh.web.tests.strategies import unknown_directory, unknown_revision from swh.web.tests.utils import ( check_api_get_responses, check_api_post_responses, @@ -23,7 +23,6 @@ # Current API: -@given(revision()) def test_api_vault_cook(api_client, mocker, directory, revision): mock_archive = mocker.patch("swh.web.api.views.vault.archive") @@ -73,7 +72,7 @@ mock_archive.vault_fetch.assert_called_with(bundle_type, swhid) -@given(revision(), unknown_directory(), unknown_revision()) +@given(unknown_directory(), unknown_revision()) def test_api_vault_cook_notfound( api_client, mocker, directory, revision, unknown_directory, unknown_revision ): @@ -186,7 +185,6 @@ # Legacy API: -@given(revision()) def test_api_vault_cook_legacy(api_client, mocker, directory, revision): mock_archive = mocker.patch("swh.web.api.views.vault.archive") @@ -238,7 +236,6 @@ mock_archive.vault_fetch.assert_called_with(bundle_type, swhid) -@given(revision()) def test_api_vault_cook_uppercase_hash_legacy(api_client, directory, revision): for obj_type, obj_id in ( @@ -274,7 +271,7 @@ assert rv["location"] == redirect_url -@given(revision(), unknown_directory(), unknown_revision()) +@given(unknown_directory(), unknown_revision()) def test_api_vault_cook_notfound_legacy( api_client, mocker, directory, revision, unknown_directory, unknown_revision ): diff --git a/swh/web/tests/browse/views/test_identifiers.py b/swh/web/tests/browse/views/test_identifiers.py --- a/swh/web/tests/browse/views/test_identifiers.py +++ b/swh/web/tests/browse/views/test_identifiers.py @@ -13,7 +13,7 @@ from swh.web.common.identifiers import gen_swhid from swh.web.common.utils import reverse from swh.web.tests.django_asserts import assert_contains -from swh.web.tests.strategies import revision, snapshot +from swh.web.tests.strategies import snapshot from swh.web.tests.utils import check_html_get_response @@ -47,7 +47,6 @@ assert resp["location"] == directory_browse_url -@given(revision()) def test_revision_id_browse(client, revision): swhid = gen_swhid(ObjectType.REVISION, revision) diff --git a/swh/web/tests/browse/views/test_origin.py b/swh/web/tests/browse/views/test_origin.py --- a/swh/web/tests/browse/views/test_origin.py +++ b/swh/web/tests/browse/views/test_origin.py @@ -36,7 +36,6 @@ from swh.web.tests.strategies import ( new_origin, new_snapshot, - revisions, unknown_revision, visit_dates, ) @@ -433,14 +432,12 @@ @given( - new_origin(), - new_snapshot(min_size=4, max_size=4), - visit_dates(), - revisions(min_size=3, max_size=3), + new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), ) def test_origin_snapshot_null_branch( - client, archive_data, new_origin, new_snapshot, visit_dates, revisions + client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, ): + revisions = revisions_list(size=4) snp_dict = new_snapshot.to_dict() archive_data.origin_add([new_origin]) for i, branch in enumerate(snp_dict["branches"].keys()): @@ -475,14 +472,12 @@ @given( - new_origin(), - new_snapshot(min_size=4, max_size=4), - visit_dates(), - revisions(min_size=4, max_size=4), + new_origin(), new_snapshot(min_size=4, max_size=4), visit_dates(), ) def test_origin_snapshot_invalid_branch( - client, archive_data, new_origin, new_snapshot, visit_dates, revisions + client, archive_data, revisions_list, new_origin, new_snapshot, visit_dates, ): + revisions = revisions_list(size=4) snp_dict = new_snapshot.to_dict() archive_data.origin_add([new_origin]) for i, branch in enumerate(snp_dict["branches"].keys()): @@ -1233,15 +1228,16 @@ @given( - new_origin(), visit_dates(), revisions(min_size=10, max_size=10), + new_origin(), visit_dates(), ) def test_origin_branches_pagination_with_alias( - client, archive_data, mocker, release, new_origin, visit_dates, revisions, + client, archive_data, mocker, release, revisions_list, new_origin, visit_dates, ): """ When a snapshot contains a branch or a release alias, pagination links in the branches / releases view should be displayed. """ + revisions = revisions_list(size=10) mocker.patch("swh.web.browse.snapshot_context.PER_PAGE", len(revisions) / 2) snp_dict = {"branches": {}, "id": hash_to_bytes(random_sha1())} for i in range(len(revisions)): diff --git a/swh/web/tests/browse/views/test_revision.py b/swh/web/tests/browse/views/test_revision.py --- a/swh/web/tests/browse/views/test_revision.py +++ b/swh/web/tests/browse/views/test_revision.py @@ -20,13 +20,11 @@ new_origin, new_person, new_swh_date, - revision, unknown_revision, ) from swh.web.tests.utils import check_html_get_response -@given(revision()) def test_revision_browse(client, archive_data, revision): _revision_browse_checks(client, archive_data, revision) @@ -45,7 +43,6 @@ _revision_browse_checks(client, archive_data, revision, origin_url=origin["url"]) -@given(revision()) def test_revision_log_browse(client, archive_data, revision): per_page = 10 @@ -158,7 +155,7 @@ ) -@given(revision(), unknown_revision(), new_origin()) +@given(unknown_revision(), new_origin()) def test_revision_request_errors(client, revision, unknown_revision, new_origin): url = reverse("browse-revision", url_args={"sha1_git": unknown_revision}) @@ -183,7 +180,6 @@ ) -@given(revision()) def test_revision_uppercase(client, revision): url = reverse( "browse-revision-uppercase-checksum", url_args={"sha1_git": revision.upper()} @@ -301,7 +297,6 @@ assert_contains(resp, swh_dir_id_url) -@given(revision()) def test_revision_invalid_path(client, archive_data, revision): path = "foo/bar" url = reverse( diff --git a/swh/web/tests/common/test_archive.py b/swh/web/tests/common/test_archive.py --- a/swh/web/tests/common/test_archive.py +++ b/swh/web/tests/common/test_archive.py @@ -30,14 +30,9 @@ 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.strategies import ( - ancestor_revisions, invalid_sha1, new_origin, new_revision, - non_ancestor_revisions, - revision, - revision_with_submodules, - revisions, sha256, snapshot, unknown_content, @@ -291,7 +286,7 @@ assert actual_release == archive_data.release_get(release) -@given(revision(), invalid_sha1(), sha256()) +@given(invalid_sha1(), sha256()) def test_lookup_revision_with_context_ko_not_a_sha1(revision, invalid_sha1, sha256): sha1_git_root = revision sha1_git = invalid_sha1 @@ -307,7 +302,7 @@ assert e.match("Only sha1_git is supported") -@given(revision(), unknown_revision()) +@given(unknown_revision()) def test_lookup_revision_with_context_ko_sha1_git_does_not_exist( revision, unknown_revision ): @@ -319,7 +314,7 @@ assert e.match("Revision %s not found" % sha1_git) -@given(revision(), unknown_revision()) +@given(unknown_revision()) def test_lookup_revision_with_context_ko_root_sha1_git_does_not_exist( revision, unknown_revision ): @@ -330,7 +325,6 @@ assert e.match("Revision root %s not found" % sha1_git_root) -@given(ancestor_revisions()) def test_lookup_revision_with_context(archive_data, ancestor_revisions): sha1_git = ancestor_revisions["sha1_git"] root_sha1_git = ancestor_revisions["sha1_git_root"] @@ -349,7 +343,6 @@ assert actual_revision == expected_revision -@given(non_ancestor_revisions()) def test_lookup_revision_with_context_ko(non_ancestor_revisions): sha1_git = non_ancestor_revisions["sha1_git"] root_sha1_git = non_ancestor_revisions["sha1_git_root"] @@ -401,7 +394,6 @@ assert e.match("Content not found for revision %s" % new_revision_id) -@given(revision()) def test_lookup_directory_with_revision_ko_path_to_nowhere(revision): invalid_path = "path/to/something/unknown" with pytest.raises(NotFoundExc) as e: @@ -412,7 +404,6 @@ assert e.match("not found") -@given(revision_with_submodules()) def test_lookup_directory_with_revision_submodules( archive_data, revision_with_submodules ): @@ -435,7 +426,6 @@ assert actual_data == expected_data -@given(revision()) def test_lookup_directory_with_revision_without_path(archive_data, revision): actual_directory_entries = archive.lookup_directory_with_revision(revision) @@ -446,7 +436,6 @@ assert actual_directory_entries["content"] == expected_directory_entries -@given(revision()) def test_lookup_directory_with_revision_with_path(archive_data, revision): rev_data = archive_data.revision_get(revision) dir_entries = [ @@ -472,7 +461,6 @@ assert actual_dir_entry["content"] == sub_dir_entries -@given(revision()) def test_lookup_directory_with_revision_with_path_to_file_and_data( archive_data, revision ): @@ -500,7 +488,6 @@ assert actual_dir_entry["content"]["data"] == expected_data["data"] -@given(revision()) def test_lookup_revision(archive_data, revision): actual_revision = archive.lookup_revision(revision) assert actual_revision == archive_data.revision_get(revision) @@ -535,7 +522,6 @@ assert e.match("Revision with sha1_git %s not found." % unknown_revision_) -@given(revisions()) def test_lookup_revision_multiple(archive_data, revisions): actual_revisions = list(archive.lookup_revision_multiple(revisions)) @@ -554,7 +540,6 @@ assert actual_revisions == [None] * len(unknown_revisions_) -@given(revision()) def test_lookup_revision_log(archive_data, revision): actual_revision_log = list(archive.lookup_revision_log(revision, limit=25)) expected_revision_log = archive_data.revision_log(revision, limit=25) @@ -690,7 +675,6 @@ assert actual_revision == expected_revision -@given(revision()) def test_lookup_revision_with_context_by_ko(origin, revision): with pytest.raises(NotFoundExc): archive.lookup_revision_with_context_by( @@ -757,7 +741,6 @@ ) == archive.lookup_revision_by(origin["url"], branch_name, None) -@given(ancestor_revisions()) def test_lookup_revision_through_with_context(ancestor_revisions): sha1_git = ancestor_revisions["sha1_git"] sha1_git_root = ancestor_revisions["sha1_git_root"] @@ -767,14 +750,12 @@ ) == archive.lookup_revision_with_context(sha1_git_root, sha1_git) -@given(revision()) def test_lookup_revision_through_with_revision(revision): assert archive.lookup_revision_through( {"sha1_git": revision} ) == archive.lookup_revision(revision) -@given(revision()) def test_lookup_directory_through_revision_ko_not_found(revision): with pytest.raises(NotFoundExc): archive.lookup_directory_through_revision( @@ -782,7 +763,6 @@ ) -@given(revision()) def test_lookup_directory_through_revision_ok(archive_data, revision): rev_data = archive_data.revision_get(revision) dir_entries = [ @@ -797,7 +777,6 @@ ) == (revision, archive.lookup_directory_with_revision(revision, dir_entry["name"])) -@given(revision()) def test_lookup_directory_through_revision_ok_with_data(archive_data, revision): rev_data = archive_data.revision_get(revision) dir_entries = [ @@ -817,7 +796,7 @@ ) -@given(revision(), snapshot()) +@given(snapshot()) def test_lookup_known_objects( archive_data, content, directory, release, revision, snapshot ): @@ -1022,7 +1001,6 @@ assert archive.lookup_snapshot_sizes(snapshot) == expected_sizes -@given(revision()) def test_lookup_snapshot_sizes_with_filtering(archive_data, revision): rev_id = hash_to_bytes(revision) snapshot = Snapshot( @@ -1061,13 +1039,11 @@ assert resolved_alias["target"] is not None -@given(revision()) def test_lookup_snapshot_missing(revision): with pytest.raises(NotFoundExc): archive.lookup_snapshot(revision) -@given(revision()) def test_lookup_snapshot_empty_branch_list(archive_data, revision): rev_id = hash_to_bytes(revision) snapshot = Snapshot( @@ -1089,7 +1065,6 @@ assert not branches -@given(revision()) def test_lookup_snapshot_branch_names_filtering(archive_data, revision): rev_id = hash_to_bytes(revision) snapshot = Snapshot( @@ -1134,7 +1109,6 @@ assert not branch_name.startswith(exclude_prefix) -@given(revision()) def test_lookup_snapshot_branch_names_filtering_paginated( archive_data, directory, revision ): diff --git a/swh/web/tests/common/test_identifiers.py b/swh/web/tests/common/test_identifiers.py --- a/swh/web/tests/common/test_identifiers.py +++ b/swh/web/tests/common/test_identifiers.py @@ -25,7 +25,7 @@ from swh.web.common.typing import SWHObjectInfo from swh.web.common.utils import reverse from swh.web.tests.data import random_sha1 -from swh.web.tests.strategies import revision, snapshot +from swh.web.tests.strategies import snapshot def test_gen_swhid(content): @@ -63,7 +63,7 @@ assert e.match("Invalid swh object type") -@given(revision(), snapshot()) +@given(snapshot()) def test_resolve_swhid_legacy(content, directory, release, revision, snapshot): for obj_type, obj_id in ( (ObjectType.CONTENT, content["sha1_git"]), @@ -100,7 +100,7 @@ resolve_swhid(f"swh:1:ori:{random_sha1()}") -@given(revision(), snapshot()) +@given(snapshot()) def test_get_swhid(content, directory, release, revision, snapshot): for obj_type, obj_id in ( (ObjectType.CONTENT, content["sha1_git"]), @@ -119,7 +119,7 @@ get_swhid("foo") -@given(revision(), snapshot()) +@given(snapshot()) def test_group_swhids(content, directory, release, revision, snapshot): swhids = [] expected = {} @@ -216,7 +216,6 @@ } -@given(revision()) def test_get_swhids_info_revision_context(archive_data, revision): revision_data = archive_data.revision_get(revision) directory = revision_data["directory"] @@ -678,7 +677,6 @@ assert origin_url in resolved_swhid["browse_url"] -@given(revision()) def test_resolve_dir_entry_swhid_with_anchor_revision(archive_data, revision): revision_data = archive_data.revision_get(revision) directory = revision_data["directory"] diff --git a/swh/web/tests/conftest.py b/swh/web/tests/conftest.py --- a/swh/web/tests/conftest.py +++ b/swh/web/tests/conftest.py @@ -3,6 +3,7 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information +from collections import defaultdict from datetime import timedelta import json import os @@ -31,6 +32,7 @@ from swh.model.swhids import ObjectType from swh.scheduler.tests.common import TASK_TYPES from swh.storage.algos.origin import origin_get_latest_visit_status +from swh.storage.algos.revisions_walker import get_revisions_walker from swh.storage.algos.snapshot import snapshot_get_all_branches, snapshot_get_latest from swh.web.auth.utils import OIDC_SWH_WEB_CLIENT_ID from swh.web.common import converters @@ -386,6 +388,122 @@ return Directory(entries=()).id.hex() +@pytest.fixture(scope="function") +def revision(tests_data): + """Fixturereturning a random revision ingested into the test archive. + """ + return random.choice(_known_swh_objects(tests_data, "revisions")) + + +@pytest.fixture(scope="function") +def revisions(tests_data): + """Fixture returning random revisions ingested into the test archive. + """ + return random.choices( + _known_swh_objects(tests_data, "revisions"), k=random.randint(2, 8), + ) + + +@pytest.fixture(scope="function") +def revisions_list(tests_data): + """Fixture returning random revisions ingested into the test archive. + """ + + def gen_revisions_list(size): + return random.choices(_known_swh_objects(tests_data, "revisions"), k=size,) + + return gen_revisions_list + + +def _get_origin_dfs_revisions_walker(tests_data): + storage = tests_data["storage"] + origin = random.choice(tests_data["origins"][:-1]) + snapshot = snapshot_get_latest(storage, origin["url"]) + if snapshot.branches[b"HEAD"].target_type.value == "alias": + target = snapshot.branches[b"HEAD"].target + head = snapshot.branches[target].target + else: + head = snapshot.branches[b"HEAD"].target + return get_revisions_walker("dfs", storage, head) + + +@pytest.fixture(scope="function") +def ancestor_revisions(tests_data): + """Fixture returning a pair of revisions ingested into the test archive + with an ancestor relation. + """ + # get a dfs revisions walker for one of the origins + # loaded into the test archive + revisions_walker = _get_origin_dfs_revisions_walker(tests_data) + master_revisions = [] + children = defaultdict(list) + init_rev_found = False + # get revisions only authored in the master branch + for rev in revisions_walker: + for rev_p in rev["parents"]: + children[rev_p].append(rev["id"]) + if not init_rev_found: + master_revisions.append(rev) + if not rev["parents"]: + init_rev_found = True + + # head revision + root_rev = master_revisions[0] + # pick a random revision, different from head, only authored + # in the master branch + ancestor_rev_idx = random.choice(list(range(1, len(master_revisions) - 1))) + ancestor_rev = master_revisions[ancestor_rev_idx] + ancestor_child_revs = children[ancestor_rev["id"]] + + return { + "sha1_git_root": hash_to_hex(root_rev["id"]), + "sha1_git": hash_to_hex(ancestor_rev["id"]), + "children": [hash_to_hex(r) for r in ancestor_child_revs], + } + + +@pytest.fixture(scope="function") +def non_ancestor_revisions(tests_data): + """Fixture returning a pair of revisions ingested into the test archive + with no ancestor relation. + """ + # get a dfs revisions walker for one of the origins + # loaded into the test archive + revisions_walker = _get_origin_dfs_revisions_walker(tests_data) + merge_revs = [] + children = defaultdict(list) + # get all merge revisions + for rev in revisions_walker: + if len(rev["parents"]) > 1: + merge_revs.append(rev) + for rev_p in rev["parents"]: + children[rev_p].append(rev["id"]) + # find a merge revisions whose parents have a unique child revision + random.shuffle(merge_revs) + selected_revs = None + for merge_rev in merge_revs: + if all(len(children[rev_p]) == 1 for rev_p in merge_rev["parents"]): + selected_revs = merge_rev["parents"] + + return { + "sha1_git_root": hash_to_hex(selected_revs[0]), + "sha1_git": hash_to_hex(selected_revs[1]), + } + + +@pytest.fixture(scope="function") +def revision_with_submodules(): + """Fixture returning a revision that is known to + point to a directory with revision entries (aka git submodules) + """ + + return { + "rev_sha1_git": "ffcb69001f3f6745dfd5b48f72ab6addb560e234", + "rev_dir_sha1_git": "d92a21446387fa28410e5a74379c934298f39ae2", + "rev_dir_rev_path": "libtess2", + } + + @pytest.fixture(scope="function") def release(tests_data): """Fixture returning a random release ingested into the test archive. @@ -484,6 +602,14 @@ return _object_type_swhid(tests_data, ObjectType.RELEASE) +@pytest.fixture(scope="function") +def revision_swhid(tests_data): + """Fixture returning a qualified SWHID for a random revision object + ingested into the test archive. + """ + return _object_type_swhid(tests_data, ObjectType.REVISION) + + # Fixture to manipulate data from a sample archive used in the tests @pytest.fixture(scope="function") def archive_data(tests_data): diff --git a/swh/web/tests/misc/test_badges.py b/swh/web/tests/misc/test_badges.py --- a/swh/web/tests/misc/test_badges.py +++ b/swh/web/tests/misc/test_badges.py @@ -16,7 +16,6 @@ from swh.web.tests.strategies import ( invalid_sha1, new_origin, - revision, snapshot, unknown_content, unknown_directory, @@ -43,7 +42,6 @@ _test_badge_endpoints(client, "release", release) -@given(revision()) def test_revision_badge(client, revision): _test_badge_endpoints(client, "revision", revision) diff --git a/swh/web/tests/misc/test_iframe.py b/swh/web/tests/misc/test_iframe.py --- a/swh/web/tests/misc/test_iframe.py +++ b/swh/web/tests/misc/test_iframe.py @@ -8,7 +8,7 @@ from swh.model.hashutil import hash_to_bytes from swh.model.swhids import CoreSWHID, ObjectType from swh.web.common.utils import reverse -from swh.web.tests.strategies import revision_swhid, unknown_directory +from swh.web.tests.strategies import unknown_directory from swh.web.tests.utils import check_html_get_response @@ -46,7 +46,6 @@ ) -@given(revision_swhid()) def test_iframe_unsupported_object(client, revision_swhid): url = reverse("swhid-iframe", url_args={"swhid": str(revision_swhid)}) check_html_get_response( diff --git a/swh/web/tests/strategies.py b/swh/web/tests/strategies.py --- a/swh/web/tests/strategies.py +++ b/swh/web/tests/strategies.py @@ -3,7 +3,6 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -from collections import defaultdict from datetime import datetime import random @@ -14,7 +13,6 @@ characters, composite, datetimes, - just, lists, sampled_from, text, @@ -25,8 +23,6 @@ from swh.model.hypothesis_strategies import snapshots as new_snapshot from swh.model.model import Person, Revision, RevisionType, TimestampWithTimezone from swh.model.swhids import ObjectType -from swh.storage.algos.revisions_walker import get_revisions_walker -from swh.storage.algos.snapshot import snapshot_get_latest from swh.web.tests.data import get_tests_data # Module dedicated to the generation of input data for tests through @@ -170,14 +166,6 @@ ) -def revision(): - """ - Hypothesis strategy returning a random revision ingested - into the test archive. - """ - return _known_swh_object("revisions") - - def unknown_revision(): """ Hypothesis strategy returning a random revision not ingested @@ -244,14 +232,6 @@ ) -def revisions(min_size=2, max_size=8): - """ - Hypothesis strategy returning random revisions ingested - into the test archive. - """ - return lists(revision(), min_size=min_size, max_size=max_size) - - def unknown_revisions(min_size=2, max_size=8): """ Hypothesis strategy returning random revisions not ingested @@ -289,105 +269,6 @@ ) -def _get_origin_dfs_revisions_walker(): - tests_data = get_tests_data() - storage = tests_data["storage"] - origin = random.choice(tests_data["origins"][:-1]) - snapshot = snapshot_get_latest(storage, origin["url"]) - if snapshot.branches[b"HEAD"].target_type.value == "alias": - target = snapshot.branches[b"HEAD"].target - head = snapshot.branches[target].target - else: - head = snapshot.branches[b"HEAD"].target - return get_revisions_walker("dfs", storage, head) - - -def ancestor_revisions(): - """ - Hypothesis strategy returning a pair of revisions ingested into the - test archive with an ancestor relation. - """ - # get a dfs revisions walker for one of the origins - # loaded into the test archive - revisions_walker = _get_origin_dfs_revisions_walker() - master_revisions = [] - children = defaultdict(list) - init_rev_found = False - # get revisions only authored in the master branch - for rev in revisions_walker: - for rev_p in rev["parents"]: - children[rev_p].append(rev["id"]) - if not init_rev_found: - master_revisions.append(rev) - if not rev["parents"]: - init_rev_found = True - - # head revision - root_rev = master_revisions[0] - # pick a random revision, different from head, only authored - # in the master branch - ancestor_rev_idx = random.choice(list(range(1, len(master_revisions) - 1))) - ancestor_rev = master_revisions[ancestor_rev_idx] - ancestor_child_revs = children[ancestor_rev["id"]] - - return just( - { - "sha1_git_root": hash_to_hex(root_rev["id"]), - "sha1_git": hash_to_hex(ancestor_rev["id"]), - "children": [hash_to_hex(r) for r in ancestor_child_revs], - } - ) - - -def non_ancestor_revisions(): - """ - Hypothesis strategy returning a pair of revisions ingested into the - test archive with no ancestor relation. - """ - # get a dfs revisions walker for one of the origins - # loaded into the test archive - revisions_walker = _get_origin_dfs_revisions_walker() - merge_revs = [] - children = defaultdict(list) - # get all merge revisions - for rev in revisions_walker: - if len(rev["parents"]) > 1: - merge_revs.append(rev) - for rev_p in rev["parents"]: - children[rev_p].append(rev["id"]) - # find a merge revisions whose parents have a unique child revision - random.shuffle(merge_revs) - selected_revs = None - for merge_rev in merge_revs: - if all(len(children[rev_p]) == 1 for rev_p in merge_rev["parents"]): - selected_revs = merge_rev["parents"] - - return just( - { - "sha1_git_root": hash_to_hex(selected_revs[0]), - "sha1_git": hash_to_hex(selected_revs[1]), - } - ) - - -# The following strategies returns data specific to some tests -# that can not be generated and thus are hardcoded. - - -def revision_with_submodules(): - """ - Hypothesis strategy returning a revision that is known to - point to a directory with revision entries (aka git submodule) - """ - return just( - { - "rev_sha1_git": "ffcb69001f3f6745dfd5b48f72ab6addb560e234", - "rev_dir_sha1_git": "d92a21446387fa28410e5a74379c934298f39ae2", - "rev_dir_rev_path": "libtess2", - } - ) - - def swhid(): """ Hypothesis strategy returning a qualified SWHID for any object @@ -396,14 +277,6 @@ return _known_swh_object("swhids") -def revision_swhid(): - """ - Hypothesis strategy returning a qualified SWHID for a revision object - ingested into the test archive. - """ - return swhid().filter(lambda swhid: swhid.object_type == ObjectType.REVISION) - - def snapshot_swhid(): """ Hypothesis strategy returning a qualified SWHID for a snapshot object