Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/misc/test_coverage.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021-2022 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 datetime import datetime, timezone | from datetime import datetime, timezone | ||||
from itertools import chain | from itertools import chain | ||||
import os | import os | ||||
from random import choice, randint | from random import randint | ||||
import uuid | import uuid | ||||
import pytest | import pytest | ||||
from django.conf import settings | from django.conf import settings | ||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from swh.scheduler.model import LastVisitStatus, ListedOrigin, OriginVisitStats | from swh.scheduler.model import LastVisitStatus, ListedOrigin, OriginVisitStats | ||||
Show All 10 Lines | |||||
@pytest.fixture(autouse=True) | @pytest.fixture(autouse=True) | ||||
def clear_lru_caches(): | def clear_lru_caches(): | ||||
_get_listers_metrics.cache_clear() | _get_listers_metrics.cache_clear() | ||||
_get_deposits_netloc_counts.cache_clear() | _get_deposits_netloc_counts.cache_clear() | ||||
def test_coverage_view_no_metrics(client): | def test_coverage_view_no_metrics(client, swh_scheduler): | ||||
""" | """ | ||||
Check coverage view can be rendered when scheduler metrics and deposits | Check coverage view can be rendered when scheduler metrics and deposits | ||||
data are not available. | data are not available. | ||||
""" | """ | ||||
url = reverse("swh-coverage") | url = reverse("swh-coverage") | ||||
check_html_get_response( | check_html_get_response( | ||||
client, url, status_code=200, template_used="misc/coverage.html" | client, url, status_code=200, template_used="misc/coverage.html" | ||||
) | ) | ||||
def test_coverage_view_with_metrics(client, swh_scheduler, mocker): | def test_coverage_view_with_metrics(client, swh_scheduler, mocker): | ||||
""" | """ | ||||
Generate some sample scheduler metrics and some sample deposits | Generate some sample scheduler metrics and some sample deposits | ||||
that will be consumed by the archive coverage view, then check | that will be consumed by the archive coverage view, then check | ||||
the HTML page gets rendered without errors. | the HTML page gets rendered without errors. | ||||
""" | """ | ||||
mocker.patch( | mocker.patch( | ||||
"swh.web.misc.coverage._get_nixguix_origins_count" | "swh.web.misc.coverage._get_nixguix_origins_count" | ||||
).return_value = 30095 | ).return_value = 30095 | ||||
listers = [] | listers = [] | ||||
visit_types = ["git", "hg", "svn", "bzr", "svn"] | |||||
for origins in listed_origins["origins"]: | for origins in listed_origins["origins"]: | ||||
# create some instances for each lister | # create some instances for each lister | ||||
for instance in range(randint(1, 5)): | for instance in range(randint(1, 5)): | ||||
lister = swh_scheduler.get_or_create_lister( | lister = swh_scheduler.get_or_create_lister( | ||||
origins["type"], f"instance-{instance}" | origins["type"], f"instance-{instance}" | ||||
) | ) | ||||
listers.append(lister) | listers.append(lister) | ||||
# record some sample listed origins | # record some sample listed origins | ||||
_origins = [] | _origins = [] | ||||
origin_visit_stats = [] | origin_visit_stats = [] | ||||
for i in range(randint(3, 10)): | for i, visit_type in enumerate(visit_types): | ||||
url = str(uuid.uuid4()) | url = str(uuid.uuid4()) | ||||
visit_type = choice(["git", "hg", "svn"]) | |||||
_origins.append( | _origins.append( | ||||
ListedOrigin( | ListedOrigin( | ||||
lister_id=lister.id, | lister_id=lister.id, | ||||
url=url, | url=url, | ||||
visit_type=visit_type, | visit_type=visit_type, | ||||
extra_loader_arguments={}, | extra_loader_arguments={}, | ||||
) | ) | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | for origins in chain( | ||||
listed_origins["origins"], | listed_origins["origins"], | ||||
legacy_origins["origins"], | legacy_origins["origins"], | ||||
deposited_origins["origins"], | deposited_origins["origins"], | ||||
): | ): | ||||
logo_url = f'{settings.STATIC_URL}img/logos/{origins["type"].lower()}.png' | logo_url = f'{settings.STATIC_URL}img/logos/{origins["type"].lower()}.png' | ||||
assert_contains(resp, f'src="{logo_url}"') | assert_contains(resp, f'src="{logo_url}"') | ||||
if "instances" in origins: | if "instances" in origins: | ||||
for visit_types in origins["instances"].values(): | for visit_types_ in origins["instances"].values(): | ||||
for data in visit_types.values(): | for data in visit_types_.values(): | ||||
if data["count"]: | if data["count"]: | ||||
assert_contains(resp, f'<a href="{escape(data["search_url"])}"') | assert_contains(resp, f'<a href="{escape(data["search_url"])}"') | ||||
else: | else: | ||||
for search_url in origins["search_urls"].values(): | for search_url in origins["search_urls"].values(): | ||||
assert_contains(resp, f'<a href="{escape(search_url)}"') | assert_contains(resp, f'<a href="{escape(search_url)}"') | ||||
for visit_type in visit_types: | |||||
ardumont: to make sure we do have some visit types to go through? | |||||
Done Inline Actionsthis is not needed as we iterate on a hard-coded list if visit types. anlambert: this is not needed as we iterate on a hard-coded list if visit types. | |||||
Not Done Inline Actionsok ardumont: ok | |||||
assert_contains(resp, f"<td>{visit_type}</td>") |
to make sure we do have some visit types to go through?