Isolate tests from ambient .env; fix begin_nested mock warning #26

Merged
rbrooks merged 1 commit from chore/test-env-isolation into main 2026-06-24 00:23:52 +00:00
Owner

Summary

Resolves issue #5. Investigation found no pytest-asyncio deprecation warnings on the pinned stack (pytest 9 / pytest-asyncio 1.x / Python 3.12) — the security-plan note was prospective for Python 3.14. The actual problem surfaced when running the suite on a host with a real .env:

Settings loads .env from the working directory (app/config.py). On the dev server, that .env (non-https APP_BASE_URL, PUBLIC_ALERT_PAGES_ENABLED=true) leaked into Settings(), causing:

  • 7 test_config.py failures (production validators tripped on the dev APP_BASE_URL), and
  • 6 RuntimeWarning: coroutine ... never awaited — public alert pages became enabled, so _ensure_public_alert_page entered async with db.begin_nested(): against a mock that didn't model begin_nested.

On clean CI (no .env) the suite was green, which hid both.

Changes

  • tests/conftest.py — disable dotenv loading before app.main is imported, so Settings uses defaults regardless of host (matches CI). Makes the suite environment-independent.
  • tests/test_alert_processor.py — mock db.begin_nested() as a sync call returning an async context manager.

Validation

Run on the dev server with its real .env copied into a throwaway worktree (reproduces the contamination):

Before After
Result 7 failed, 300 passed 307 passed
Warnings 6 0

Clean CI was already green; this makes a .env-bearing host green too.

Closes #5

## Summary Resolves issue #5. Investigation found **no pytest-asyncio deprecation warnings** on the pinned stack (pytest 9 / pytest-asyncio 1.x / Python 3.12) — the security-plan note was prospective for Python 3.14. The actual problem surfaced when running the suite on a host with a real `.env`: `Settings` loads `.env` from the working directory (`app/config.py`). On the dev server, that `.env` (non-https `APP_BASE_URL`, `PUBLIC_ALERT_PAGES_ENABLED=true`) leaked into `Settings()`, causing: - **7 `test_config.py` failures** (production validators tripped on the dev `APP_BASE_URL`), and - **6 `RuntimeWarning: coroutine ... never awaited`** — public alert pages became enabled, so `_ensure_public_alert_page` entered `async with db.begin_nested():` against a mock that didn't model `begin_nested`. On clean CI (no `.env`) the suite was green, which hid both. ## Changes - **`tests/conftest.py`** — disable dotenv loading before `app.main` is imported, so `Settings` uses defaults regardless of host (matches CI). Makes the suite environment-independent. - **`tests/test_alert_processor.py`** — mock `db.begin_nested()` as a sync call returning an async context manager. ## Validation Run on the dev server **with its real `.env` copied into a throwaway worktree** (reproduces the contamination): | | Before | After | |---|---|---| | Result | 7 failed, 300 passed | **307 passed** | | Warnings | 6 | **0** | Clean CI was already green; this makes a `.env`-bearing host green too. Closes #5
Isolate tests from ambient .env and fix begin_nested mock
All checks were successful
CI / test (pull_request) Successful in 3m8s
1ee8169d8c
Settings reads a .env from the working directory, so running the suite on a
host that has a real .env (e.g. the dev server, which sets a non-https
APP_BASE_URL and PUBLIC_ALERT_PAGES_ENABLED=true) leaked that config into
Settings(). That caused test_config failures and, because public alert pages
became enabled, a "coroutine never awaited" RuntimeWarning where the mocked
db.begin_nested() was entered as an async context manager.

- conftest: disable dotenv loading before app import so Settings uses defaults,
  matching the clean CI environment regardless of host.
- test_alert_processor: mock db.begin_nested() as a sync call returning an
  async context manager.

Note: investigation for #5 found no pytest-asyncio deprecation warnings on the
pinned stack (pytest 9 / pytest-asyncio 1.x / Python 3.12); the only warning
noise was the begin_nested mock above, now resolved.

Closes #5

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
rbrooks deleted branch chore/test-env-isolation 2026-06-24 00:23:53 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
rbrooks/WeatherBot!26
No description provided.