From 3cf97c3c5c1829b9b8da2674befead3ecd515757 Mon Sep 17 00:00:00 2001 From: Djeex Date: Sat, 22 Aug 2026 23:08:33 +0200 Subject: [PATCH] Cover notifier.py error paths (TEST_MODE, HTTP error, connection error) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .gitignore | 4 +- tests/test_notifier.py | 83 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7cd6f5d..2d61efc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .venv -__pycache__/ \ No newline at end of file +__pycache__/ +.coverage +.pytest_cache/ \ No newline at end of file diff --git a/tests/test_notifier.py b/tests/test_notifier.py index b19e46a..e8fdf58 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -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 = {}