Cover notifier.py error paths (TEST_MODE, HTTP error, connection error)
CI / build-and-scan (pull_request) Successful in 40s

Brings app/notifier.py from 73% to 100% test coverage by exercising the
three notification functions' TEST_MODE early-return, non-204 response,
and requests exception branches — the exact paths a requests upgrade
regression would break silently.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
Djeex
2026-08-22 23:08:33 +02:00
co-authored by Claude Sonnet 5
parent b3f2ea7be2
commit 3cf97c3c5c
2 changed files with 86 additions and 1 deletions
+83
View File
@@ -52,6 +52,50 @@ def test_in_stock_notification_posts_expected_payload(monkeypatch):
assert "RTX 5090 Founders Edition" in embed["title"]
def test_discord_notification_survives_http_error(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False")
monkeypatch.setattr(notifier.requests, "post", lambda *a, **k: FakeResponse(status_code=500, text="boom"))
notifier.send_discord_notification("RTX 5090 Founders Edition", "https://example.com", "1999")
def test_discord_notification_survives_connection_error(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False")
def raise_error(*a, **k):
raise notifier.requests.exceptions.ConnectionError("boom")
monkeypatch.setattr(notifier.requests, "post", raise_error)
# Should not raise even though the request itself blew up (network down, DNS, etc.)
notifier.send_discord_notification("RTX 5090 Founders Edition", "https://example.com", "1999")
def test_out_of_stock_test_mode_skips_network_call(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="True")
calls = []
monkeypatch.setattr(notifier.requests, "post", lambda *a, **k: calls.append((a, k)))
notifier.send_out_of_stock_notification("RTX 5090 Founders Edition", "https://example.com", "1999")
assert calls == []
def test_out_of_stock_notification_posts_on_success(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False")
captured = {}
def fake_post(url, json=None, **kwargs):
captured["json"] = json
return FakeResponse(status_code=204)
monkeypatch.setattr(notifier.requests, "post", fake_post)
notifier.send_out_of_stock_notification("RTX 5090 Founders Edition", "https://example.com/buy", "1999")
assert captured["json"]["embeds"][0]["url"] == "https://example.com/buy"
def test_out_of_stock_notification_survives_http_error(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False")
monkeypatch.setattr(notifier.requests, "post", lambda *a, **k: FakeResponse(status_code=500, text="boom"))
@@ -60,6 +104,45 @@ def test_out_of_stock_notification_survives_http_error(monkeypatch):
notifier.send_out_of_stock_notification("RTX 5090 Founders Edition", "https://example.com", "1999")
def test_out_of_stock_notification_survives_connection_error(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False")
def raise_error(*a, **k):
raise notifier.requests.exceptions.ConnectionError("boom")
monkeypatch.setattr(notifier.requests, "post", raise_error)
notifier.send_out_of_stock_notification("RTX 5090 Founders Edition", "https://example.com", "1999")
def test_sku_change_test_mode_skips_network_call(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="True")
calls = []
monkeypatch.setattr(notifier.requests, "post", lambda *a, **k: calls.append((a, k)))
notifier.send_sku_change_notification("RTX 5090 Founders Edition", "old-sku", "new-sku", "https://example.com")
assert calls == []
def test_sku_change_notification_survives_http_error(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False")
monkeypatch.setattr(notifier.requests, "post", lambda *a, **k: FakeResponse(status_code=500, text="boom"))
notifier.send_sku_change_notification("RTX 5090 Founders Edition", "old-sku", "new-sku", "https://example.com")
def test_sku_change_notification_survives_connection_error(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False")
def raise_error(*a, **k):
raise notifier.requests.exceptions.ConnectionError("boom")
monkeypatch.setattr(notifier.requests, "post", raise_error)
notifier.send_sku_change_notification("RTX 5090 Founders Edition", "old-sku", "new-sku", "https://example.com")
def test_sku_change_notification_mentions_role_and_skus(monkeypatch):
notifier = _import_notifier(monkeypatch, test_mode="False", discord_roles="<@&123456789012345678>")
captured = {}