|
| 1 | +import importlib.util |
| 2 | +from enum import Enum |
| 3 | + |
| 4 | +import pytest |
| 5 | +from celery import Celery |
| 6 | + |
| 7 | +from eolic.base import Eolic |
| 8 | +from eolic.helpers.modules import get_module |
| 9 | +from eolic.integrations.celery import CeleryIntegration |
| 10 | +from eolic.model import EventDTO, EventRemoteCeleryTarget |
| 11 | +from eolic.remote import ( |
| 12 | + EventRemoteTargetHandler, |
| 13 | + EventRemoteCeleryDispatcher, |
| 14 | + EventRemoteDispatcherFactory, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def app(): |
| 20 | + """ |
| 21 | + Fixture for creating a Celery app instance. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + Celery: An instance of Celery. |
| 25 | + """ |
| 26 | + return Celery() |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def eolic(): |
| 31 | + """ |
| 32 | + Fixture for creating an Eolic instance. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + Eolic: An instance of Eolic. |
| 36 | + """ |
| 37 | + return Eolic() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def target_handler() -> EventRemoteTargetHandler: |
| 42 | + """ |
| 43 | + Fixture to provide an instance of EventRemoteTargetHandler. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + EventRemoteTargetHandler: An instance of EventRemoteTargetHandler. |
| 47 | + """ |
| 48 | + handler = EventRemoteTargetHandler() |
| 49 | + return handler |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def event_remote_celery_target() -> EventRemoteCeleryTarget: |
| 54 | + """ |
| 55 | + Fixture to provide an instance of EventRemoteCeleryTarget. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + EventRemoteCeleryTarget: An instance of EventRemoteCeleryTarget. |
| 59 | + """ |
| 60 | + return EventRemoteCeleryTarget(type="celery", address="amqp://localhost:5672") |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def target_celery_handler( |
| 65 | + event_remote_celery_target: EventRemoteCeleryTarget, |
| 66 | +) -> EventRemoteCeleryDispatcher: |
| 67 | + """ |
| 68 | + Fixture to provide an instance of EventRemoteTargetHandler. |
| 69 | +
|
| 70 | + Args: |
| 71 | + event_remote_celery_target (EventRemoteCeleryTarget): The EventRemoteCeleryTarget instance. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + EventRemoteTargetHandler: An instance of EventRemoteTargetHandler. |
| 75 | + """ |
| 76 | + event_remote_celery_dispatcher = EventRemoteCeleryDispatcher( |
| 77 | + event_remote_celery_target |
| 78 | + ) |
| 79 | + |
| 80 | + return event_remote_celery_dispatcher |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def event_dto(): |
| 85 | + """ |
| 86 | + Fixture for creating an EventDTO instance. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + EventDTO: An instance of EventDTO with predefined attributes. |
| 90 | + """ |
| 91 | + return EventDTO(event="test_event", args=("arg1",), kwargs={"key": "value"}) |
| 92 | + |
| 93 | + |
| 94 | +def test_celery_integration_init(eolic: Eolic, app: Celery): |
| 95 | + """ |
| 96 | + Test initialization of CeleryIntegration. |
| 97 | +
|
| 98 | + Args: |
| 99 | + eolic (Eolic): The Eolic instance. |
| 100 | + app (Celery): The Celery app instance. |
| 101 | + """ |
| 102 | + integration = CeleryIntegration(app=app) |
| 103 | + eolic.setup_integration(integration) |
| 104 | + |
| 105 | + assert integration.eolic is not None |
| 106 | + assert integration.app == app |
| 107 | + assert integration.event_function == "events" |
| 108 | + assert integration.queue_name == "eolic" |
| 109 | + |
| 110 | + |
| 111 | +def test_celery_integration_no_app(): |
| 112 | + """ |
| 113 | + Test that an exception is raised when CeleryIntegration is initialized with no app. |
| 114 | + """ |
| 115 | + with pytest.raises( |
| 116 | + Exception, match="Please declare you app to setup the integration." |
| 117 | + ): |
| 118 | + CeleryIntegration(app=None) |
| 119 | + |
| 120 | + |
| 121 | +def test_celery_not_installed( |
| 122 | + monkeypatch: pytest.MonkeyPatch, |
| 123 | + app: Celery, |
| 124 | + target_celery_handler: EventRemoteCeleryDispatcher, |
| 125 | + event_remote_celery_target: EventRemoteCeleryTarget, |
| 126 | +): |
| 127 | + """ |
| 128 | + Test that an exception is raised when Celery is not installed. |
| 129 | +
|
| 130 | + Args: |
| 131 | + monkeypatch: The pytest monkeypatch fixture. |
| 132 | + app (Celery): The Celery app instance. |
| 133 | + target_celery_handler (EventRemoteCeleryDispatcher): The EventRemoteCeleryDispatcher instance. |
| 134 | + event_remote_celery_target (EventRemoteCeleryTarget): The EventRemoteCeleryTarget instance. |
| 135 | + """ |
| 136 | + monkeypatch.setattr(importlib.util, "find_spec", lambda _: None) |
| 137 | + |
| 138 | + with pytest.raises(Exception, match="Celery Integration is not installed.*"): |
| 139 | + CeleryIntegration(app=app) |
| 140 | + |
| 141 | + with pytest.raises(Exception, match="Celery Integration is not installed.*"): |
| 142 | + EventRemoteCeleryDispatcher(event_remote_celery_target) |
| 143 | + |
| 144 | + |
| 145 | +def test_celery_empty_event_function(eolic: Eolic, app: Celery): |
| 146 | + """ |
| 147 | + Test that an exception is raised when event_route is None. |
| 148 | +
|
| 149 | + Args: |
| 150 | + eolic (Eolic): The Eolic instance. |
| 151 | + app (Celery): The Celery app instance. |
| 152 | + """ |
| 153 | + integration = CeleryIntegration(app, event_function=None) |
| 154 | + with pytest.raises( |
| 155 | + Exception, match="Event function is required for Celery integration." |
| 156 | + ): |
| 157 | + eolic.setup_integration(integration) |
| 158 | + |
| 159 | + |
| 160 | +def test_celery_listener_event( |
| 161 | + monkeypatch: pytest.MonkeyPatch, eolic: Eolic, app: Celery |
| 162 | +): |
| 163 | + """ |
| 164 | + Test that an exception is raised when event_route is None. |
| 165 | +
|
| 166 | + Args: |
| 167 | + monkeypatch: The pytest monkeypatch fixture. |
| 168 | + eolic (Eolic): The Eolic instance. |
| 169 | + app (Celery): The Celery app instance. |
| 170 | + """ |
| 171 | + integration = CeleryIntegration(app, event_function="event") |
| 172 | + integration.setup(eolic) |
| 173 | + |
| 174 | + def mock_lambda_handler(event, *args, **kwargs): |
| 175 | + return event, args, kwargs |
| 176 | + |
| 177 | + monkeypatch.setattr(integration, "forward_event", mock_lambda_handler) |
| 178 | + |
| 179 | + integration.app.tasks["event"]("test_event", "arg1", key="value") |
| 180 | + |
| 181 | + |
| 182 | +def test_celery_module(eolic: Eolic, app: Celery): |
| 183 | + """ |
| 184 | + Test that an exception is raised when event_route is None. |
| 185 | +
|
| 186 | + Args: |
| 187 | + eolic (Eolic): The Eolic instance. |
| 188 | + app (Celery): The Celery app instance. |
| 189 | + """ |
| 190 | + assert isinstance(get_module("celery").Celery(), Celery) |
| 191 | + |
| 192 | + |
| 193 | +def test_parse_celery_target(target_handler: EventRemoteTargetHandler) -> None: |
| 194 | + """ |
| 195 | + Test parsing a dictionary target. |
| 196 | +
|
| 197 | + Args: |
| 198 | + target_handler (EventRemoteTargetHandler): An instance of EventRemoteTargetHandler. |
| 199 | + """ |
| 200 | + target = { |
| 201 | + "type": "celery", |
| 202 | + "address": "amqp://localhost:5672", |
| 203 | + "queue_name": "q", |
| 204 | + "function_name": "f", |
| 205 | + } |
| 206 | + |
| 207 | + parsed_target = target_handler._parse_target(target) |
| 208 | + |
| 209 | + assert isinstance(parsed_target, EventRemoteCeleryTarget) |
| 210 | + assert parsed_target.address == "amqp://localhost:5672" |
| 211 | + assert parsed_target.function_name == "f" |
| 212 | + assert parsed_target.queue_name == "q" |
| 213 | + |
| 214 | + |
| 215 | +@pytest.mark.asyncio |
| 216 | +async def test_target_celery_handler( |
| 217 | + monkeypatch: pytest.MonkeyPatch, |
| 218 | + target_celery_handler: EventRemoteCeleryDispatcher, |
| 219 | +) -> None: |
| 220 | + """ |
| 221 | + Test parsing a dictionary target. |
| 222 | +
|
| 223 | + Args: |
| 224 | + monkeypatch: The pytest monkeypatch fixture. |
| 225 | + target_celery_handler (EventRemoteCeleryDispatcher): An instance of EventRemoteCeleryDispatcher. |
| 226 | + """ |
| 227 | + |
| 228 | + class Events(str, Enum): |
| 229 | + |
| 230 | + event = "event" |
| 231 | + |
| 232 | + def send_task_mock(function_name: str, *args, **kwargs): |
| 233 | + print("{} - {} - {}".format(function_name, args, kwargs)) |
| 234 | + |
| 235 | + monkeypatch.setattr(target_celery_handler.celery, "send_task", send_task_mock) |
| 236 | + |
| 237 | + await target_celery_handler.dispatch(Events.event, "1", "2", "3", k={"4": "4"}) |
| 238 | + |
| 239 | + |
| 240 | +async def test_event_remote_dispatcher_factory( |
| 241 | + target_celery_handler: EventRemoteCeleryDispatcher, |
| 242 | + event_remote_celery_target: EventRemoteCeleryTarget, |
| 243 | +) -> None: |
| 244 | + """ |
| 245 | + Test parsing a dictionary target. |
| 246 | +
|
| 247 | + Args: |
| 248 | + target_celery_handler (EventRemoteCeleryDispatcher): An instance of EventRemoteCeleryDispatcher. |
| 249 | + event_remote_celery_target (EventRemoteCeleryTarget): The EventRemoteCeleryTarget instance. |
| 250 | + """ |
| 251 | + |
| 252 | + EventRemoteDispatcherFactory().create(event_remote_celery_target) |
0 commit comments