Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor celery tasks + management commands #1347

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
7 changes: 6 additions & 1 deletion src/open_inwoner/configurations/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import logging

from django.core.management import call_command
Expand All @@ -11,6 +12,10 @@
def send_failed_mail_digest():
logger.info("starting send_failed_mail_digest() task")

call_command("send_failed_mail_digest")
out = io.StringIO()

call_command("send_failed_mail_digest", stdout=out)

logger.info("finished send_failed_mail_digest() task")

return out.getvalue()
33 changes: 22 additions & 11 deletions src/open_inwoner/configurations/tests/test_emails.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest import mock
from unittest.mock import patch

from django.core import mail
from django.core.management import call_command
Expand All @@ -10,6 +10,7 @@

from open_inwoner.configurations.tasks import send_failed_mail_digest

from ..emails import inform_admins_about_failing_emails
from ..models import SiteConfiguration


Expand All @@ -31,7 +32,7 @@ def test_no_recipients_configured(self):
self.assertEqual(Log.objects.count(), 1)

with freeze_time("2024-01-02T00:00:00"):
call_command("send_failed_mail_digest")
inform_admins_about_failing_emails()

self.assertEqual(len(mail.outbox), 0)

Expand All @@ -51,7 +52,7 @@ def test_no_failing_emails_in_past_24_hours(self):
self.assertEqual(Log.objects.count(), 1)

with freeze_time("2024-01-02T00:00:00"):
call_command("send_failed_mail_digest")
inform_admins_about_failing_emails()

self.assertEqual(len(mail.outbox), 0)

Expand All @@ -77,7 +78,7 @@ def test_send_daily_failing_email_digest(self):
)

with freeze_time("2024-01-02T00:00:00"):
call_command("send_failed_mail_digest")
inform_admins_about_failing_emails()

self.assertEqual(len(mail.outbox), 1)

Expand Down Expand Up @@ -106,13 +107,23 @@ def test_task_triggers_sending_of_digest(self):
)

with freeze_time("2024-01-02T00:00:00"):
send_failed_mail_digest.delay()
inform_admins_about_failing_emails()

self.assertEqual(len(mail.outbox), 1)

# Due to Celery's eager task discovery, we have to mock the imported
# call_command.
@mock.patch("open_inwoner.configurations.tasks.call_command")
def test_task_invokes_management_command(self, m):
send_failed_mail_digest.delay()
m.assert_called_once_with("send_failed_mail_digest")
@patch("open_inwoner.configurations.tasks.call_command")
def test_command_called_when_task_triggered(self, mock_command):
send_failed_mail_digest()

mock_command.assert_called_once()
self.assertEqual(mock_command.call_args.args, ("send_failed_mail_digest",))

@patch(
"open_inwoner.configurations.management.commands."
"send_failed_mail_digest.inform_admins_about_failing_emails"
)
def test_entrypoint_called_when_command_called(self, mock):
call_command("send_failed_mail_digest")

mock.assert_called_once()
self.assertEqual(mock.call_args.args, ())
7 changes: 6 additions & 1 deletion src/open_inwoner/openzaak/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import logging

from django.core.management import call_command
Expand All @@ -11,6 +12,10 @@
def import_zgw_data():
logger.info("starting import_zgw_data() task")

call_command("zgw_import_data")
out = io.StringIO()

call_command("zgw_import_data", stdout=out)

logger.info("finished import_zgw_data() task")

return out.getvalue()
3 changes: 2 additions & 1 deletion src/open_inwoner/openzaak/tests/test_zgw_imports.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.test import TestCase
from django.test import TestCase, override_settings

import requests_mock

Expand Down Expand Up @@ -129,6 +129,7 @@ def install_mocks(self, m) -> "ZaakTypeMockData":
return self


@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
@requests_mock.Mocker()
class ZGWImportTest(ClearCachesMixin, TestCase):
config: OpenZaakConfig
Expand Down
6 changes: 4 additions & 2 deletions src/open_inwoner/openzaak/tests/test_zgw_imports_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def tearDownClass(cls):
celery_app.conf.task_always_eager = cls._old_eager

@patch("open_inwoner.openzaak.tasks.call_command")
def test_task_calls_command(self, mock_call: Mock):
def test_zgw_import_task_calls_command(self, mock_call: Mock):
import_zgw_data()
mock_call.assert_called_once_with("zgw_import_data")

mock_call.assert_called_once()
self.assertEqual(mock_call.call_args.args, ("zgw_import_data",))
7 changes: 6 additions & 1 deletion src/open_inwoner/search/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import logging

from django.core.management import call_command
Expand All @@ -11,6 +12,10 @@
def rebuild_search_index():
logger.info("starting rebuild_search_index() task")

call_command("search_index", "--rebuild", "-f")
out = io.StringIO()

call_command("search_index", "--rebuild", "-f", stdout=out)

logger.info("finished rebuild_search_index() task")

return out.getvalue()
23 changes: 21 additions & 2 deletions src/open_inwoner/search/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@

from django.test import TestCase

from open_inwoner.celery import app as celery_app

from ..tasks import rebuild_search_index


class SearchTaskTest(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# manually patch the conf: it is a dynamic object that is hard to patch
cls._old_eager = celery_app.conf.task_always_eager
celery_app.conf.task_always_eager = False

@classmethod
def tearDownClass(cls):
super().tearDownClass()
celery_app.conf.task_always_eager = cls._old_eager

@patch("open_inwoner.search.tasks.call_command")
def test_task_calls_command(self, mock: Mock):
def test_search_index_task_calls_command(
self,
mock: Mock,
):
rebuild_search_index()
mock.assert_called_once_with("search_index", "--rebuild", "-f")

mock.assert_called_once()
self.assertEqual(mock.call_args.args, ("search_index", "--rebuild", "-f"))
Loading