diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/tasks/test_extension.py | 61 | ||||
| -rw-r--r-- | tests/tasks/test_notifications.py | 18 | ||||
| -rw-r--r-- | tests/tasks/test_scanner.py | 3 | ||||
| -rw-r--r-- | tests/test_init.py | 25 | ||||
| -rw-r--r-- | tests/test_packaging.py | 8 |
5 files changed, 112 insertions, 3 deletions
diff --git a/tests/tasks/test_extension.py b/tests/tasks/test_extension.py index 19f1191..a2495e8 100644 --- a/tests/tasks/test_extension.py +++ b/tests/tasks/test_extension.py @@ -8,7 +8,7 @@ from typing import Any import huey as huey_package import pytest -from flask import Flask, has_app_context +from flask import Flask, current_app, has_app_context from huey import crontab from webmentions_ssg.tasks.extension import Huey @@ -94,6 +94,65 @@ def test_task_raises_without_app(app: Flask, periodic: bool) -> None: task.call_local() +@pytest.mark.parametrize("periodic", [False, True], ids=["task", "periodic-task"]) +def test_task_declaration_before_init_and_rebind_after_init(periodic: bool) -> None: + """Imported task handles do not capture the first application backend.""" + queue = Huey() + observed_apps: list[str] = [] + + @task_decorator(queue, periodic) + def identify_app() -> str: + name = current_app.import_name + observed_apps.append(name) + return name + + assert identify_app.__name__ == "identify_app" + with pytest.raises(RuntimeError, match="Huey has not been initialized"): + identify_app.call_local() + + first = Flask("first") + first.config["TESTING"] = True + queue.init_app(first) + assert identify_app.call_local() == "first" + # Huey discards periodic-task results, even in immediate mode. Verify + # execution through a side effect as well as the returned result. + assert identify_app().get() == (None if periodic else "first") + assert observed_apps == ["first", "first"] + + previous_backend = queue.huey + second = Flask("second") + second.config["TESTING"] = True + queue.init_app(second) + + assert queue.huey is not previous_backend + assert identify_app.call_local() == "second" + assert identify_app().get() == (None if periodic else "second") + assert observed_apps == ["first", "first", "second", "second"] + # The new broker must recognize the registered task when deserializing work. + queued = identify_app.s() + restored = queue.huey.deserialize_task(queue.huey.serialize_task(queued)) + assert type(restored) is type(queued) + + +def test_multiple_deferred_tasks_registered_on_initialization() -> None: + queue = Huey() + + @queue.task(retries=2, retry_delay=50) + def first(value: int) -> int: + return value + 1 + + @queue.task() + def second(value: int) -> int: + return first.call_local(value) + 1 + + app = Flask("tasks") + app.config["TESTING"] = True + queue.init_app(app) + + assert second(2).get() == 4 + assert first.call_local(2) == 3 + + def test_getattr_delegates_to_huey(app: Flask) -> None: huey = Huey(app) assert huey.immediate is True diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py index 0254b19..f87fd47 100644 --- a/tests/tasks/test_notifications.py +++ b/tests/tasks/test_notifications.py @@ -9,8 +9,10 @@ from unittest.mock import patch import pytest from flask import Flask +from huey.exceptions import TaskLockedException from webmentions_ssg import db +from webmentions_ssg.extensions import huey from webmentions_ssg.models import ReceivedWebmention @@ -128,3 +130,19 @@ def test_digest_skips_when_no_new_mentions( notifications.send_webmention_digest() send.assert_not_called() + + +def test_digest_lock_uses_current_backend( + notifications: ModuleType, app: Flask +) -> None: + """A digest imported before reinitialization uses the new backend's lock.""" + with app.app_context(): + previous_backend = huey.huey + huey.init_app(app) + assert huey.huey is not previous_backend + + with ( + huey.lock_task("send-webmention-digest"), + pytest.raises(TaskLockedException), + ): + notifications.send_webmention_digest() diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index d11b3f2..c0b9223 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -42,8 +42,7 @@ def test_manual_scan_retries_when_periodic_scan_holds_lock( app: Flask, scanner_module: ModuleType ) -> None: with app.app_context(): - # Import-time decorators previously captured an old backend here, - # making this test pass or fail depending on module import order. + # A task imported earlier must still use the reinitialized backend. previous_backend = scanner_module.huey.huey scanner_module.huey.init_app(app) assert scanner_module.huey.huey is not previous_backend diff --git a/tests/test_init.py b/tests/test_init.py index 7112654..ccfdfd8 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -4,6 +4,9 @@ """Test application initialization and its public integrations.""" +import subprocess +import sys + from flask import Flask from webmentions_ssg import VERSION, huey @@ -22,3 +25,25 @@ def test_create_app_exposes_version_in_templates(app: Flask) -> None: app.update_template_context(context) assert context["VERSION"] == VERSION + + +def test_importing_views_and_tasks_before_creating_app() -> None: + """Module imports alone must never need an initialized Huey backend.""" + script = """ +from webmentions_ssg import huey +from webmentions_ssg.tasks.scanner import manual_scan_sources +from webmentions_ssg.tasks.receiver import verify_webmention +from webmentions_ssg.tasks.sender import send_webmention +from webmentions_ssg.views import root_page + +assert huey.app is None +assert huey._huey is None +assert manual_scan_sources.__name__ == 'manual_scan_sources' +assert verify_webmention.__name__ == 'verify_webmention' +assert send_webmention.__name__ == 'send_webmention' +assert root_page.name == 'root' +""" + result = subprocess.run( + [sys.executable, "-c", script], capture_output=True, text=True, check=False + ) + assert result.returncode == 0, result.stderr diff --git a/tests/test_packaging.py b/tests/test_packaging.py index 34c0d72..e0b4fb1 100644 --- a/tests/test_packaging.py +++ b/tests/test_packaging.py @@ -35,3 +35,11 @@ def test_no_legacy_ratelimit_only_groups() -> None: assert not any( group.startswith("ratelimit-") for group in metadata["dependency-groups"] ) + + +def test_hatch_version_does_not_import_the_flask_package() -> None: + project_root = Path(__file__).resolve().parents[1] + metadata = tomllib.loads((project_root / "pyproject.toml").read_text()) + assert metadata["tool"]["hatch"]["version"]["path"] == ( + "webmentions_ssg/version.py" + ) |
