diff --git a/swh/loader/mercurial/tasks.py b/swh/loader/mercurial/tasks.py index 6e2703d..ed70ac8 100644 --- a/swh/loader/mercurial/tasks.py +++ b/swh/loader/mercurial/tasks.py @@ -1,43 +1,35 @@ # Copyright (C) 2017-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -from swh.scheduler.task import Task +from celery import current_app as app from .loader import HgBundle20Loader, HgArchiveBundle20Loader -class LoadMercurial(Task): +@app.task(name=__name__ + '.LoadMercurial') +def load_mercurial(origin_url, directory=None, visit_date=None): """Mercurial repository loading - """ - task_queue = 'swh_loader_mercurial' - - def run_task(self, *, origin_url, visit_date=None, directory=None): - """Import a mercurial tarball into swh. - - Args: see :func:`DepositLoader.load`. + Import a mercurial tarball into swh. - """ - loader = HgBundle20Loader() - loader.log = self.log - return loader.load(origin_url=origin_url, - directory=directory, - visit_date=visit_date) + Args: see :func:`DepositLoader.load`. + """ + loader = HgBundle20Loader() + return loader.load(origin_url=origin_url, + directory=directory, + visit_date=visit_date) -class LoadArchiveMercurial(Task): - task_queue = 'swh_loader_mercurial_archive' - - def run_task(self, *, origin_url, archive_path, visit_date): - """Import a mercurial tarball into swh. - Args: see :func:`DepositLoader.load`. +@app.task(name=__name__ + '.LoadArchiveMercurial') +def load_archive_mercurial(origin_url, archive_path, visit_date=None): + """Import a mercurial tarball into swh. - """ - loader = HgArchiveBundle20Loader() - loader.log = self.log - return loader.load(origin_url=origin_url, - archive_path=archive_path, - visit_date=visit_date) + Args: see :func:`DepositLoader.load`. + """ + loader = HgArchiveBundle20Loader() + return loader.load(origin_url=origin_url, + archive_path=archive_path, + visit_date=visit_date) diff --git a/swh/loader/mercurial/tests/conftest.py b/swh/loader/mercurial/tests/conftest.py new file mode 100644 index 0000000..d1dcb10 --- /dev/null +++ b/swh/loader/mercurial/tests/conftest.py @@ -0,0 +1,10 @@ +import pytest + +from swh.scheduler.tests.conftest import * # noqa + + +@pytest.fixture(scope='session') +def celery_includes(): + return [ + 'swh.loader.mercurial.tasks', + ] diff --git a/swh/loader/mercurial/tests/test_tasks.py b/swh/loader/mercurial/tests/test_tasks.py index c3f35aa..09c5517 100644 --- a/swh/loader/mercurial/tests/test_tasks.py +++ b/swh/loader/mercurial/tests/test_tasks.py @@ -1,53 +1,41 @@ # Copyright (C) 2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -import unittest from unittest.mock import patch -from swh.loader.mercurial.tasks import LoadMercurial, LoadArchiveMercurial +@patch('swh.loader.mercurial.loader.HgBundle20Loader.load') +def test_loader(mock_loader, swh_app, celery_session_worker): + mock_loader.return_value = {'status': 'eventful'} -class TestTasks(unittest.TestCase): - def test_check_task_name(self): - task = LoadMercurial() - self.assertEqual(task.task_queue, 'swh_loader_mercurial') + res = swh_app.send_task( + 'swh.loader.mercurial.tasks.LoadMercurial', + kwargs=dict(origin_url='origin_url', directory='/some/repo', + visit_date='now')) + assert res + res.wait() + assert res.successful() - @patch('swh.loader.mercurial.loader.HgBundle20Loader.load') - def test_task(self, mock_loader): - mock_loader.return_value = {'status': 'eventful'} - task = LoadMercurial() + assert res.result == {'status': 'eventful'} + mock_loader.assert_called_once_with( + origin_url='origin_url', visit_date='now', directory='/some/repo') - # given - actual_result = task.run_task( - origin_url='origin_url', visit_date='now', directory='/some/repo') - self.assertEqual(actual_result, {'status': 'eventful'}) +@patch('swh.loader.mercurial.loader.HgArchiveBundle20Loader.load') +def test_archive_loader(mock_loader, swh_app, celery_session_worker): + mock_loader.return_value = {'status': 'uneventful'} - mock_loader.assert_called_once_with( - origin_url='origin_url', visit_date='now', directory='/some/repo') + res = swh_app.send_task( + 'swh.loader.mercurial.tasks.LoadArchiveMercurial', + ('another_url', '/some/tar.tgz', 'now')) + assert res + res.wait() + assert res.successful() - -class TestTasks2(unittest.TestCase): - def test_check_task_name(self): - task = LoadArchiveMercurial() - self.assertEqual(task.task_queue, 'swh_loader_mercurial_archive') - - @patch('swh.loader.mercurial.loader.HgArchiveBundle20Loader.load') - def test_task(self, mock_loader): - mock_loader.return_value = {'status': 'uneventful'} - task = LoadArchiveMercurial() - - # given - actual_result = task.run_task( - origin_url='another_url', - archive_path='/some/tar.tgz', - visit_date='now') - - self.assertEqual(actual_result, {'status': 'uneventful'}) - - mock_loader.assert_called_once_with( - origin_url='another_url', - archive_path='/some/tar.tgz', - visit_date='now') + assert res.result == {'status': 'uneventful'} + mock_loader.assert_called_once_with( + origin_url='another_url', + archive_path='/some/tar.tgz', + visit_date='now')