Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/browse/test_snapshot_context.py
# Copyright (C) 2020-2021 The Software Heritage developers | # Copyright (C) 2020-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 | ||||
import random | import random | ||||
from swh.model.hashutil import hash_to_bytes | |||||
from swh.model.model import ObjectType as ModelObjectType | |||||
from swh.model.model import Release, Snapshot, SnapshotBranch, TargetType | |||||
from swh.model.swhids import ObjectType | from swh.model.swhids import ObjectType | ||||
from swh.web.browse.snapshot_context import ( | from swh.web.browse.snapshot_context import ( | ||||
vlorentz: for consistency with other packages.
(we really need to get rid of this.....) | |||||
Done Inline Actionsack, yes it is so confusing, not the first time I encounter that issue. anlambert: ack, yes it is so confusing, not the first time I encounter that issue. | |||||
_get_release, | _get_release, | ||||
get_origin_visit_snapshot, | get_origin_visit_snapshot, | ||||
get_snapshot_content, | get_snapshot_content, | ||||
get_snapshot_context, | get_snapshot_context, | ||||
) | ) | ||||
from swh.web.browse.utils import gen_revision_url | from swh.web.browse.utils import gen_revision_url | ||||
from swh.web.common.identifiers import gen_swhid | from swh.web.common.identifiers import gen_swhid | ||||
from swh.web.common.origin_visits import get_origin_visit, get_origin_visits | from swh.web.common.origin_visits import get_origin_visit, get_origin_visits | ||||
▲ Show 20 Lines • Show All 381 Lines • ▼ Show 20 Lines | def _get_revision_info(archive_data, revision_id): | ||||
if revision_id: | if revision_id: | ||||
revision_info = archive_data.revision_get(revision_id) | revision_info = archive_data.revision_get(revision_id) | ||||
revision_info["message_header"] = revision_info["message"].split("\n")[0] | revision_info["message_header"] = revision_info["message"].split("\n")[0] | ||||
revision_info["date"] = format_utc_iso_date(revision_info["date"]) | revision_info["date"] = format_utc_iso_date(revision_info["date"]) | ||||
revision_info["committer_date"] = format_utc_iso_date( | revision_info["committer_date"] = format_utc_iso_date( | ||||
revision_info["committer_date"] | revision_info["committer_date"] | ||||
) | ) | ||||
return revision_info | return revision_info | ||||
def test_get_snapshot_context_revision_release(archive_data, revision): | |||||
release_name = "v1.0.0" | |||||
release = Release( | |||||
name=release_name.encode(), | |||||
message=f"release {release_name}".encode(), | |||||
target=hash_to_bytes(revision), | |||||
target_type=ModelObjectType.REVISION, | |||||
synthetic=True, | |||||
) | |||||
archive_data.release_add([release]) | |||||
snapshot = Snapshot( | |||||
branches={ | |||||
b"HEAD": SnapshotBranch( | |||||
target=release_name.encode(), target_type=TargetType.ALIAS | |||||
), | |||||
release_name.encode(): SnapshotBranch( | |||||
target=release.id, target_type=TargetType.RELEASE | |||||
), | |||||
}, | |||||
) | |||||
archive_data.snapshot_add([snapshot]) | |||||
snapshot_no_head = Snapshot( | |||||
branches={ | |||||
release_name.encode(): SnapshotBranch( | |||||
target=release.id, target_type=TargetType.RELEASE | |||||
), | |||||
}, | |||||
) | |||||
archive_data.snapshot_add([snapshot_no_head]) | |||||
revision_data = archive_data.revision_get(revision) | |||||
for params in ( | |||||
{"snapshot_id": snapshot.id.hex()}, | |||||
{"snapshot_id": snapshot.id.hex(), "release_name": release_name}, | |||||
{"snapshot_id": snapshot_no_head.id.hex()}, | |||||
): | |||||
snapshot_context = get_snapshot_context(**params) | |||||
assert snapshot_context["branches"] == [] | |||||
assert snapshot_context["releases"] != [] | |||||
assert snapshot_context["release"] == release_name | |||||
assert snapshot_context["release_id"] == release.id.hex() | |||||
assert snapshot_context["revision_id"] == revision | |||||
assert snapshot_context["root_directory"] == revision_data["directory"] | |||||
def test_get_snapshot_context_directory_release(archive_data, directory): | |||||
release_name = "v1.0.0" | |||||
release = Release( | |||||
name=release_name.encode(), | |||||
message=f"release {release_name}".encode(), | |||||
target=hash_to_bytes(directory), | |||||
target_type=ModelObjectType.DIRECTORY, | |||||
synthetic=True, | |||||
) | |||||
archive_data.release_add([release]) | |||||
snapshot = Snapshot( | |||||
branches={ | |||||
b"HEAD": SnapshotBranch( | |||||
target=release_name.encode(), target_type=TargetType.ALIAS | |||||
), | |||||
release_name.encode(): SnapshotBranch( | |||||
target=release.id, target_type=TargetType.RELEASE | |||||
), | |||||
}, | |||||
) | |||||
archive_data.snapshot_add([snapshot]) | |||||
snapshot_no_head = Snapshot( | |||||
branches={ | |||||
release_name.encode(): SnapshotBranch( | |||||
target=release.id, target_type=TargetType.RELEASE | |||||
), | |||||
}, | |||||
) | |||||
archive_data.snapshot_add([snapshot_no_head]) | |||||
for params in ( | |||||
{"snapshot_id": snapshot.id.hex()}, | |||||
{"snapshot_id": snapshot.id.hex(), "release_name": release_name}, | |||||
{"snapshot_id": snapshot_no_head.id.hex()}, | |||||
): | |||||
snapshot_context = get_snapshot_context(**params) | |||||
assert snapshot_context["branches"] == [] | |||||
assert snapshot_context["releases"] != [] | |||||
assert snapshot_context["release"] == release_name | |||||
assert snapshot_context["release_id"] == release.id.hex() | |||||
assert snapshot_context["revision_id"] is None | |||||
assert snapshot_context["root_directory"] == directory |
for consistency with other packages.
(we really need to get rid of this.....)