refactor(tests): compress admin_client + mock_send_email into conftest
Lift admin_client fixture from 7 duplicate definitions into conftest.py. Add mock_send_email fixture, replacing 60 inline patch() blocks across test_emails.py, test_waitlist.py, and test_businessplan.py. Net -197 lines. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -9,7 +9,6 @@ Covers:
|
||||
"""
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from padelnomics.businessplan import generate_business_plan, get_plan_sections
|
||||
@@ -184,19 +183,18 @@ async def _insert_export(db, user_id: int, scenario_id: int, status: str = "pend
|
||||
|
||||
@requires_weasyprint
|
||||
class TestWorkerHandler:
|
||||
async def test_happy_path_generates_pdf_and_updates_status(self, db, scenario):
|
||||
async def test_happy_path_generates_pdf_and_updates_status(self, db, scenario, mock_send_email):
|
||||
from padelnomics.worker import handle_generate_business_plan
|
||||
|
||||
export = await _insert_export(db, scenario["user_id"], scenario["id"])
|
||||
output_file = None
|
||||
|
||||
with patch("padelnomics.worker.send_email", new_callable=AsyncMock) as mock_email:
|
||||
await handle_generate_business_plan({
|
||||
"export_id": export["id"],
|
||||
"user_id": scenario["user_id"],
|
||||
"scenario_id": scenario["id"],
|
||||
"language": "en",
|
||||
})
|
||||
await handle_generate_business_plan({
|
||||
"export_id": export["id"],
|
||||
"user_id": scenario["user_id"],
|
||||
"scenario_id": scenario["id"],
|
||||
"language": "en",
|
||||
})
|
||||
|
||||
# Status should be 'ready'
|
||||
from padelnomics.core import fetch_one
|
||||
@@ -214,14 +212,14 @@ class TestWorkerHandler:
|
||||
assert output_file.read_bytes()[:4] == b"%PDF"
|
||||
|
||||
# Email should have been sent
|
||||
mock_email.assert_called_once()
|
||||
assert "to" in mock_email.call_args.kwargs
|
||||
assert "subject" in mock_email.call_args.kwargs
|
||||
mock_send_email.assert_called_once()
|
||||
assert "to" in mock_send_email.call_args.kwargs
|
||||
assert "subject" in mock_send_email.call_args.kwargs
|
||||
finally:
|
||||
if output_file and output_file.exists():
|
||||
output_file.unlink()
|
||||
|
||||
async def test_marks_failed_on_bad_scenario(self, db, scenario):
|
||||
async def test_marks_failed_on_bad_scenario(self, db, scenario, mock_send_email):
|
||||
"""Handler marks export failed when user_id doesn't match scenario owner."""
|
||||
from padelnomics.worker import handle_generate_business_plan
|
||||
|
||||
@@ -229,14 +227,13 @@ class TestWorkerHandler:
|
||||
wrong_user_id = scenario["user_id"] + 9999
|
||||
export = await _insert_export(db, scenario["user_id"], scenario["id"])
|
||||
|
||||
with patch("padelnomics.worker.send_email", new_callable=AsyncMock):
|
||||
with pytest.raises(ValueError):
|
||||
await handle_generate_business_plan({
|
||||
"export_id": export["id"],
|
||||
"user_id": wrong_user_id,
|
||||
"scenario_id": scenario["id"],
|
||||
"language": "en",
|
||||
})
|
||||
with pytest.raises(ValueError):
|
||||
await handle_generate_business_plan({
|
||||
"export_id": export["id"],
|
||||
"user_id": wrong_user_id,
|
||||
"scenario_id": scenario["id"],
|
||||
"language": "en",
|
||||
})
|
||||
|
||||
from padelnomics.core import fetch_one
|
||||
row = await fetch_one(
|
||||
|
||||
Reference in New Issue
Block a user