|
| 1 | +""" |
| 2 | +Shared testing utilities for openedx-core tests. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from contextlib import contextmanager |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import Generator |
| 10 | + |
| 11 | +from openedx_events.tooling import OpenEdxPublicSignal # type: ignore[import-untyped] |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class CapturedEvent: |
| 16 | + """A single captured event emission.""" |
| 17 | + |
| 18 | + signal: OpenEdxPublicSignal |
| 19 | + kwargs: dict |
| 20 | + |
| 21 | + |
| 22 | +@contextmanager |
| 23 | +def capture_events( |
| 24 | + signals: list[OpenEdxPublicSignal] | None = None, |
| 25 | + expected_count: int | None = None, |
| 26 | +) -> Generator[list[CapturedEvent], None, None]: |
| 27 | + """ |
| 28 | + Context manager that captures Open edX events emitted during the block. |
| 29 | +
|
| 30 | + Args: |
| 31 | + signals: Optional list of ``OpenEdxPublicSignal`` instances to monitor. |
| 32 | + Defaults to all registered signals (OpenEdxPublicSignal.all_events()). |
| 33 | + expected_count: How many events are expected (optional). If specified, |
| 34 | + will assert that the resulting list has this length. |
| 35 | +
|
| 36 | + Yields: |
| 37 | + list[CapturedEvent]: A list that is populated as each event fires. |
| 38 | + Each entry has a ``signal`` attribute and a ``kwargs`` |
| 39 | + dict containing the event data (learning_package, |
| 40 | + changed_by, etc.) plus ``metadata`` and |
| 41 | + ``from_event_bus``. |
| 42 | +
|
| 43 | + Example usage:: |
| 44 | +
|
| 45 | + with capture_events(expected_count=1) as captured: |
| 46 | + api.do_something(entity.id, ...) |
| 47 | +
|
| 48 | + assert captured[0].signal is LEARNING_PACKAGE_ENTITIES_CHANGED |
| 49 | + assert captured[0].kwargs['learning_package'].id == learning_package.id |
| 50 | + """ |
| 51 | + if signals is None: |
| 52 | + signals = list(OpenEdxPublicSignal.all_events()) |
| 53 | + |
| 54 | + captured: list[CapturedEvent] = [] |
| 55 | + receivers: dict[OpenEdxPublicSignal, object] = {} |
| 56 | + |
| 57 | + for signal in signals: |
| 58 | + |
| 59 | + def make_receiver(sig: OpenEdxPublicSignal): |
| 60 | + def receiver(sender, **kwargs): |
| 61 | + kwargs.pop("signal", None) |
| 62 | + captured.append(CapturedEvent(signal=sig, kwargs=kwargs)) |
| 63 | + |
| 64 | + return receiver |
| 65 | + |
| 66 | + receiver = make_receiver(signal) |
| 67 | + signal.connect(receiver) |
| 68 | + receivers[signal] = receiver |
| 69 | + |
| 70 | + try: |
| 71 | + yield captured |
| 72 | + finally: |
| 73 | + for signal, receiver in receivers.items(): |
| 74 | + signal.disconnect(receiver) |
| 75 | + |
| 76 | + if expected_count is not None: |
| 77 | + assert len(captured) == expected_count, ( |
| 78 | + f"Expected {expected_count} event(s), got {len(captured)}: {[e.signal for e in captured]}" |
| 79 | + ) |
0 commit comments