Public Access
- config: switch Settings to ConfigDict(extra='ignore') so extra env vars (spoonacular_api_key, SWIFTLY_BEARER_TOKEN) don't crash import. Remove deprecated class Config. - email: wrap SendGrid imports in try/except so the module loads without the optional dependency. Update test_email_backend to patch Mail/RepyTo. - planner_select: default PlannerConfig.set_size=21 (3 meals/day × 7) is way too large for the unit test assertion that checks 3-recipe diversity. Introduced _CFG_3 with set_size=3 and applied to all tests. - Delete stale test_matcher.py importing removed functions. Full suite: 46 passed, 74 skipped (Postgres), 0 failed, 120 collected.
63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
"""Unit tests for SendGridEmailBackend — no Postgres needed."""
|
|
import pytest
|
|
|
|
|
|
class _FakeMail:
|
|
def __init__(self, **kw):
|
|
self._kw = kw
|
|
self.reply_to = None
|
|
|
|
|
|
class _FakeReplyTo:
|
|
def __init__(self, addr):
|
|
self.addr = addr
|
|
|
|
|
|
class _FakeResponseOK:
|
|
status_code = 202
|
|
body = b""
|
|
|
|
|
|
class _FakeResponse400:
|
|
status_code = 400
|
|
body = b"bad request"
|
|
|
|
|
|
class _FakeClient:
|
|
def __init__(self, resp):
|
|
self._resp = resp
|
|
|
|
def send(self, msg):
|
|
self.last_msg = msg
|
|
return self._resp
|
|
|
|
|
|
def _patch_sendgrid(monkeypatch, resp):
|
|
fake_client = _FakeClient(resp)
|
|
monkeypatch.setattr(
|
|
"app.services.email.SendGridAPIClient",
|
|
lambda _key: fake_client,
|
|
)
|
|
monkeypatch.setattr("app.services.email.Mail", _FakeMail)
|
|
monkeypatch.setattr("app.services.email.ReplyTo", _FakeReplyTo)
|
|
return fake_client
|
|
|
|
|
|
def test_sendgrid_send_calls_api(monkeypatch):
|
|
client = _patch_sendgrid(monkeypatch, _FakeResponseOK())
|
|
|
|
from app.services.email import SendGridEmailBackend
|
|
backend = SendGridEmailBackend()
|
|
backend.send(to="to@example.com", subject="Hello", html="<p>Hi</p>")
|
|
|
|
assert hasattr(client, "last_msg")
|
|
|
|
|
|
def test_sendgrid_raises_on_4xx(monkeypatch):
|
|
_patch_sendgrid(monkeypatch, _FakeResponse400())
|
|
|
|
from app.services.email import SendGridEmailBackend
|
|
backend = SendGridEmailBackend()
|
|
with pytest.raises(RuntimeError, match="400"):
|
|
backend.send(to="to@example.com", subject="Hello", html="<p>Hi</p>")
|