Changeset View
Changeset View
Standalone View
Standalone View
swh/scheduler/tests/test_journal_client.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 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 General Public License version 3, or any later version | # License: GNU 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 datetime | import datetime | ||||
import functools | import functools | ||||
from itertools import permutations | from itertools import permutations | ||||
from unittest.mock import Mock | |||||
import pytest | import pytest | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.scheduler.journal_client import max_date, process_journal_objects | from swh.scheduler.journal_client import max_date, process_journal_objects | ||||
from swh.scheduler.model import ListedOrigin, OriginVisitStats | from swh.scheduler.model import ListedOrigin, OriginVisitStats | ||||
from swh.scheduler.utils import utcnow | from swh.scheduler.utils import utcnow | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | def test_max_date_raise(): | ||||
with pytest.raises(ValueError, match="valid datetime"): | with pytest.raises(ValueError, match="valid datetime"): | ||||
max_date(None, None) | max_date(None, None) | ||||
def test_journal_client_origin_visit_status_from_journal_ignored_status(swh_scheduler): | def test_journal_client_origin_visit_status_from_journal_ignored_status(swh_scheduler): | ||||
"""Only final statuses (full, partial) are important, the rest remain ignored. | """Only final statuses (full, partial) are important, the rest remain ignored. | ||||
""" | """ | ||||
# Trace method calls on the swh_scheduler | |||||
swh_scheduler = Mock(wraps=swh_scheduler) | |||||
visit_statuses = [ | visit_statuses = [ | ||||
{ | { | ||||
"origin": "foo", | "origin": "foo", | ||||
"visit": 1, | "visit": 1, | ||||
"status": "created", | "status": "created", | ||||
"date": utcnow(), | "date": utcnow(), | ||||
"type": "git", | "type": "git", | ||||
"snapshot": None, | "snapshot": None, | ||||
}, | }, | ||||
{ | { | ||||
"origin": "bar", | "origin": "bar", | ||||
"visit": 1, | "visit": 1, | ||||
"status": "ongoing", | "status": "ongoing", | ||||
"date": utcnow(), | "date": utcnow(), | ||||
"type": "svn", | "type": "svn", | ||||
"snapshot": None, | "snapshot": None, | ||||
}, | }, | ||||
] | ] | ||||
process_journal_objects( | process_journal_objects( | ||||
{"origin_visit_status": visit_statuses}, scheduler=swh_scheduler | {"origin_visit_status": visit_statuses}, scheduler=swh_scheduler | ||||
) | ) | ||||
# Ensure those visit status are ignored | # All messages have been ignored: no stats have been upserted | ||||
actual_origin_visit_stats = swh_scheduler.origin_visit_stats_get( | swh_scheduler.origin_visit_stats_upsert.assert_not_called() | ||||
[(vs["origin"], vs["type"]) for vs in visit_statuses] | |||||
def test_journal_client_ignore_missing_type(swh_scheduler): | |||||
"""Ignore statuses with missing type key""" | |||||
# Trace method calls on the swh_scheduler | |||||
swh_scheduler = Mock(wraps=swh_scheduler) | |||||
date = utcnow() | |||||
snapshot = hash_to_bytes("dddcc0710eb6cf9efd5b920a8453e1e07157bddd") | |||||
visit_statuses = [ | |||||
{ | |||||
"origin": "foo", | |||||
"visit": 1, | |||||
"status": "full", | |||||
"date": date, | |||||
"snapshot": snapshot, | |||||
}, | |||||
] | |||||
process_journal_objects( | |||||
{"origin_visit_status": visit_statuses}, scheduler=swh_scheduler | |||||
) | ) | ||||
assert actual_origin_visit_stats == [] | |||||
# The message has been ignored: no stats have been upserted | |||||
swh_scheduler.origin_visit_stats_upsert.assert_not_called() | |||||
def test_journal_client_origin_visit_status_from_journal_last_notfound(swh_scheduler): | def test_journal_client_origin_visit_status_from_journal_last_notfound(swh_scheduler): | ||||
visit_status = { | visit_status = { | ||||
"origin": "foo", | "origin": "foo", | ||||
"visit": 1, | "visit": 1, | ||||
"status": "not_found", | "status": "not_found", | ||||
"date": DATE1, | "date": DATE1, | ||||
▲ Show 20 Lines • Show All 621 Lines • Show Last 20 Lines |