|
| 1 | +# src/tests/backend/test_event_utils.py |
| 2 | + |
| 3 | +import os |
| 4 | +import logging |
| 5 | +import types |
| 6 | +import sys |
| 7 | +import pytest |
| 8 | + |
| 9 | +# --- Stub out azure.monitor.events.extension.track_event so import won't fail --- |
| 10 | +azure_pkg = types.ModuleType("azure") |
| 11 | +monitor_pkg = types.ModuleType("azure.monitor") |
| 12 | +events_pkg = types.ModuleType("azure.monitor.events") |
| 13 | +extension_pkg = types.ModuleType("azure.monitor.events.extension") |
| 14 | + |
| 15 | +# we don't need a real implementation here |
| 16 | +extension_pkg.track_event = lambda name, data: None |
| 17 | + |
| 18 | +azure_pkg.monitor = monitor_pkg |
| 19 | +monitor_pkg.events = events_pkg |
| 20 | +events_pkg.extension = extension_pkg |
| 21 | + |
| 22 | +sys.modules["azure"] = azure_pkg |
| 23 | +sys.modules["azure.monitor"] = monitor_pkg |
| 24 | +sys.modules["azure.monitor.events"] = events_pkg |
| 25 | +sys.modules["azure.monitor.events.extension"] = extension_pkg |
| 26 | + |
| 27 | +# now import the function under test |
| 28 | +import backend.event_utils as eu |
| 29 | + |
| 30 | +@pytest.fixture(autouse=True) |
| 31 | +def clear_env(monkeypatch): |
| 32 | + # ensure the Application Insights key is unset by default |
| 33 | + monkeypatch.delenv("APPLICATIONINSIGHTS_CONNECTION_STRING", raising=False) |
| 34 | + yield |
| 35 | + |
| 36 | +def test_skip_when_not_configured(caplog): |
| 37 | + caplog.set_level(logging.WARNING) |
| 38 | + called = False |
| 39 | + # patch eu.track_event itself so even if config were set, nothing runs |
| 40 | + def fake(name, data): |
| 41 | + nonlocal called |
| 42 | + called = True |
| 43 | + |
| 44 | + setattr(eu, "track_event", fake) |
| 45 | + eu.track_event_if_configured("TestEvent", {"foo": "bar"}) |
| 46 | + assert not called |
| 47 | + assert "Skipping track_event for TestEvent as Application Insights is not configured" in caplog.text |
| 48 | + |
| 49 | +def test_track_when_configured(monkeypatch): |
| 50 | + monkeypatch.setenv("APPLICATIONINSIGHTS_CONNECTION_STRING", "ikey") |
| 51 | + calls = [] |
| 52 | + def fake(name, data): |
| 53 | + calls.append((name, data)) |
| 54 | + |
| 55 | + setattr(eu, "track_event", fake) |
| 56 | + eu.track_event_if_configured("MyEvent", {"a": 1}) |
| 57 | + assert calls == [("MyEvent", {"a": 1})] |
| 58 | + |
| 59 | +def test_attribute_error_is_caught(monkeypatch, caplog): |
| 60 | + monkeypatch.setenv("APPLICATIONINSIGHTS_CONNECTION_STRING", "ikey") |
| 61 | + caplog.set_level(logging.WARNING) |
| 62 | + |
| 63 | + def bad(name, data): |
| 64 | + raise AttributeError("missing resource") |
| 65 | + setattr(eu, "track_event", bad) |
| 66 | + |
| 67 | + eu.track_event_if_configured("Evt", {"x": 2}) |
| 68 | + assert "ProxyLogger error in track_event: missing resource" in caplog.text |
| 69 | + |
| 70 | +def test_other_exception_is_caught(monkeypatch, caplog): |
| 71 | + monkeypatch.setenv("APPLICATIONINSIGHTS_CONNECTION_STRING", "ikey") |
| 72 | + caplog.set_level(logging.WARNING) |
| 73 | + |
| 74 | + def bad(name, data): |
| 75 | + raise RuntimeError("boom") |
| 76 | + setattr(eu, "track_event", bad) |
| 77 | + |
| 78 | + eu.track_event_if_configured("Evt2", {"y": 3}) |
| 79 | + assert "Error in track_event: boom" in caplog.text |
0 commit comments