Changeset View
Changeset View
Standalone View
Standalone View
swh/lister/gitlab/tests/test_tasks.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 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 | ||||
from time import sleep | from time import sleep | ||||
from celery.result import GroupResult | from celery.result import GroupResult | ||||
from unittest.mock import patch, call | |||||
from unittest.mock import patch | from swh.lister.gitea.tasks import NBPAGES | ||||
from swh.lister.utils import split_range | |||||
def test_ping(swh_scheduler_celery_app, swh_scheduler_celery_worker): | def test_ping(swh_scheduler_celery_app, swh_scheduler_celery_worker): | ||||
res = swh_scheduler_celery_app.send_task("swh.lister.gitlab.tasks.ping") | res = swh_scheduler_celery_app.send_task("swh.lister.gitlab.tasks.ping") | ||||
assert res | assert res | ||||
res.wait() | res.wait() | ||||
assert res.successful() | assert res.successful() | ||||
assert res.result == "OK" | assert res.result == "OK" | ||||
Show All 34 Lines | def test_range(lister, swh_scheduler_celery_app, swh_scheduler_celery_worker): | ||||
lister.assert_called_once_with() | lister.assert_called_once_with() | ||||
lister.db_last_index.assert_not_called() | lister.db_last_index.assert_not_called() | ||||
lister.run.assert_called_once_with(min_bound=12, max_bound=42) | lister.run.assert_called_once_with(min_bound=12, max_bound=42) | ||||
@patch("swh.lister.gitlab.tasks.GitLabLister") | @patch("swh.lister.gitlab.tasks.GitLabLister") | ||||
def test_relister(lister, swh_scheduler_celery_app, swh_scheduler_celery_worker): | def test_relister(lister, swh_scheduler_celery_app, swh_scheduler_celery_worker): | ||||
total_pages = 85 | |||||
# setup the mocked GitlabLister | # setup the mocked GitlabLister | ||||
lister.return_value = lister | lister.return_value = lister | ||||
lister.run.return_value = None | lister.run.return_value = None | ||||
lister.get_pages_information.return_value = (None, 85, None) | lister.get_pages_information.return_value = (None, total_pages, None) | ||||
lister.db_partition_indices.return_value = [ | |||||
(i, i + 9) for i in range(0, 80, 10) | |||||
] + [(80, 85)] | |||||
res = swh_scheduler_celery_app.send_task( | res = swh_scheduler_celery_app.send_task( | ||||
"swh.lister.gitlab.tasks.FullGitLabRelister" | "swh.lister.gitlab.tasks.FullGitLabRelister" | ||||
) | ) | ||||
assert res | assert res | ||||
res.wait() | res.wait() | ||||
assert res.successful() | assert res.successful() | ||||
Show All 14 Lines | def test_relister(lister, swh_scheduler_celery_app, swh_scheduler_celery_worker): | ||||
# + 9 for the RangeGitlabLister subtasks | # + 9 for the RangeGitlabLister subtasks | ||||
assert lister.call_count == 10 | assert lister.call_count == 10 | ||||
lister.db_last_index.assert_not_called() | lister.db_last_index.assert_not_called() | ||||
lister.db_partition_indices.assert_not_called() | lister.db_partition_indices.assert_not_called() | ||||
lister.get_pages_information.assert_called_once_with() | lister.get_pages_information.assert_called_once_with() | ||||
# lister.run should have been called once per partition interval | # lister.run should have been called once per partition interval | ||||
for i in range(8): | for min_bound, max_bound in split_range(total_pages, NBPAGES): | ||||
# XXX inconsistent behavior: max_bound is EXCLUDED here | |||||
assert ( | assert ( | ||||
dict(min_bound=10 * i, max_bound=10 * i + 10), | call(min_bound=min_bound, max_bound=max_bound) in lister.run.call_args_list | ||||
) in lister.run.call_args_list | ) | ||||
assert (dict(min_bound=80, max_bound=85),) in lister.run.call_args_list | |||||
@patch("swh.lister.gitlab.tasks.GitLabLister") | @patch("swh.lister.gitlab.tasks.GitLabLister") | ||||
def test_relister_instance( | def test_relister_instance( | ||||
lister, swh_scheduler_celery_app, swh_scheduler_celery_worker | lister, swh_scheduler_celery_app, swh_scheduler_celery_worker | ||||
): | ): | ||||
total_pages = 85 | |||||
# setup the mocked GitlabLister | # setup the mocked GitlabLister | ||||
lister.return_value = lister | lister.return_value = lister | ||||
lister.run.return_value = None | lister.run.return_value = None | ||||
lister.get_pages_information.return_value = (None, 85, None) | lister.get_pages_information.return_value = (None, total_pages, None) | ||||
lister.db_partition_indices.return_value = [ | |||||
(i, i + 9) for i in range(0, 80, 10) | |||||
] + [(80, 85)] | |||||
res = swh_scheduler_celery_app.send_task( | res = swh_scheduler_celery_app.send_task( | ||||
"swh.lister.gitlab.tasks.FullGitLabRelister", | "swh.lister.gitlab.tasks.FullGitLabRelister", | ||||
kwargs=dict(url="https://0xacab.org/api/v4"), | kwargs=dict(url="https://0xacab.org/api/v4"), | ||||
) | ) | ||||
assert res | assert res | ||||
res.wait() | res.wait() | ||||
Show All 15 Lines | ): | ||||
# + 9 for the RangeGitlabLister subtasks | # + 9 for the RangeGitlabLister subtasks | ||||
assert lister.call_count == 10 | assert lister.call_count == 10 | ||||
lister.db_last_index.assert_not_called() | lister.db_last_index.assert_not_called() | ||||
lister.db_partition_indices.assert_not_called() | lister.db_partition_indices.assert_not_called() | ||||
lister.get_pages_information.assert_called_once_with() | lister.get_pages_information.assert_called_once_with() | ||||
# lister.run should have been called once per partition interval | # lister.run should have been called once per partition interval | ||||
for i in range(8): | for min_bound, max_bound in split_range(total_pages, NBPAGES): | ||||
# XXX inconsistent behavior: max_bound is EXCLUDED here | |||||
assert ( | assert ( | ||||
dict(min_bound=10 * i, max_bound=10 * i + 10), | call(min_bound=min_bound, max_bound=max_bound) in lister.run.call_args_list | ||||
) in lister.run.call_args_list | ) | ||||
assert (dict(min_bound=80, max_bound=85),) in lister.run.call_args_list |