phabricator/tasks: Fix task function return type
Previously, the following error was raised when the task has finished its execution: "Object of type ListerStats is not JSON serializable". So ensure ListerStats object gets converted to dict before returning it. Also add missing test for task function.
This commit is contained in:
parent
b48f71ff93
commit
c782275296
2 changed files with 42 additions and 6 deletions
|
@ -2,7 +2,7 @@
|
|||
# License: GNU General Public License version 3, or any later version
|
||||
# See top-level LICENSE file for more information
|
||||
|
||||
from typing import Optional
|
||||
from typing import Dict, Optional
|
||||
|
||||
from celery import shared_task
|
||||
|
||||
|
@ -10,11 +10,17 @@ from swh.lister.phabricator.lister import PhabricatorLister
|
|||
|
||||
|
||||
@shared_task(name=__name__ + ".FullPhabricatorLister")
|
||||
def list_phabricator_full(url: str, instance: str, api_token: Optional[str] = None):
|
||||
def list_phabricator_full(
|
||||
url: str, instance: str, api_token: Optional[str] = None
|
||||
) -> Dict[str, int]:
|
||||
"""Full update of a Phabricator instance"""
|
||||
return PhabricatorLister.from_configfile(
|
||||
url=url, instance=instance, api_token=api_token
|
||||
).run()
|
||||
return (
|
||||
PhabricatorLister.from_configfile(
|
||||
url=url, instance=instance, api_token=api_token
|
||||
)
|
||||
.run()
|
||||
.dict()
|
||||
)
|
||||
|
||||
|
||||
@shared_task(name=__name__ + ".ping")
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
# Copyright (C) 2019-2020 the Software Heritage developers
|
||||
# Copyright (C) 2019-2021 the Software Heritage developers
|
||||
# License: GNU General Public License version 3, or any later version
|
||||
# See top-level LICENSE file for more information
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from swh.lister.pattern import ListerStats
|
||||
|
||||
|
||||
def test_ping(swh_scheduler_celery_app, swh_scheduler_celery_worker):
|
||||
res = swh_scheduler_celery_app.send_task("swh.lister.phabricator.tasks.ping")
|
||||
|
@ -9,3 +13,29 @@ def test_ping(swh_scheduler_celery_app, swh_scheduler_celery_worker):
|
|||
res.wait()
|
||||
assert res.successful()
|
||||
assert res.result == "OK"
|
||||
|
||||
|
||||
@patch("swh.lister.phabricator.tasks.PhabricatorLister")
|
||||
def test_phabricator_lister_task(
|
||||
lister, swh_scheduler_celery_app, swh_scheduler_celery_worker
|
||||
):
|
||||
# setup the mocked PhabricatorLister
|
||||
lister.from_configfile.return_value = lister
|
||||
lister_stats = ListerStats(pages=2, origins=200)
|
||||
lister.run.return_value = lister_stats
|
||||
|
||||
task_params = {
|
||||
"url": "https://forge.softwareheritage.org/api/diffusion.repository.search",
|
||||
"instance": "swh",
|
||||
"api_token": None,
|
||||
}
|
||||
|
||||
res = swh_scheduler_celery_app.send_task(
|
||||
"swh.lister.phabricator.tasks.FullPhabricatorLister", kwargs=task_params
|
||||
)
|
||||
assert res
|
||||
res.wait()
|
||||
assert res.successful()
|
||||
assert res.result == lister_stats.dict()
|
||||
|
||||
lister.from_configfile.assert_called_once_with(**task_params)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue