3eb11543b5
The data routes (purchases, alerts, stores, etc.) are mounted at /api/v1 in production but most test files still called them without the prefix, producing 116 404s. The 39 tests that passed were the auth tests (/auth/* at root) plus test_models and test_encrypted_json. This commit brings the test suite in line with the actual route layout, fixes several additional pre-existing source/test bugs surfaced once the 404s cleared, and gets PR #42 to a clean green run (164 passed, 7 skipped, 0 failed). Source fixes - src/cartsnitch_api/auth/dependencies.py: parse ISO strings for expires_at before tzinfo check (SQLite returns raw text for TIMESTAMP) - src/cartsnitch_api/schemas.py: UserResponse.id is UUID, matching the actual model type and avoiding ResponseValidationError on /auth/me Test alignment - tests/test_routes/*, tests/test_e2e/*: add /api/v1 prefix to all data route calls (auth routes left alone — they live at root) - tests/test_openapi.py: refresh EXPECTED_ROUTES to match the actual OpenAPI spec (drop Better-Auth-only routes, add /api/v1 prefix, update route count to 31) Pre-existing test fixes - tests/test_middleware/test_rate_limit.py: InMemorySlidingWindow tests are async (is_allowed is a coroutine); Redis fallback mocks must raise RedisError, not bare Exception, to trigger the except branch - tests/test_middleware/test_error_handler.py: validation-error test uses /auth/me PATCH with a bad email so Pydantic 422s before any DB lookup; error-stats test uses settings.service_key instead of a hard-coded placeholder - tests/test_e2e/conftest.py: Coupon.valid_to is date.today()+offset so the seed coupons don't expire relative to the actual current date - tests/test_e2e/test_error_responses.py: skip TestRegistrationErrors and TestLoginErrors — they target Better-Auth endpoints that this gateway doesn't expose - tests/test_e2e/test_public_endpoints.py: trend data assertion loosened to >= 2 to match the seed window - tests/test_config.py: test_database_url_default uses monkeypatch to clear env vars so the hard-coded default assertion is deterministic - tests/test_routes/test_public.py: empty-list store comparison returns 422 (Pydantic validation), not 400 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
"""Tests for Settings config, specifically the database_url env var fallback."""
|
|
|
|
from cartsnitch_api.config import Settings
|
|
|
|
|
|
def test_database_url_prefers_cartsnitch_prefix():
|
|
"""CARTSNITCH_DATABASE_URL takes precedence over DATABASE_URL."""
|
|
env = {
|
|
"CARTSNITCH_DATABASE_URL": "postgresql+asyncpg://user1:pass1@host1:5432/db1",
|
|
"DATABASE_URL": "postgresql://user2:pass2@host2:5432/db2",
|
|
}
|
|
settings = Settings(**env)
|
|
assert settings.database_url == "postgresql+asyncpg://user1:pass1@host1:5432/db1"
|
|
|
|
|
|
def test_database_url_falls_back_to_database_url():
|
|
"""When CARTSNITCH_DATABASE_URL is absent, DATABASE_URL is accepted."""
|
|
env = {
|
|
"DATABASE_URL": "postgresql://user:pass@dbhost:5432/mydb",
|
|
}
|
|
settings = Settings(**env)
|
|
assert settings.database_url == "postgresql+asyncpg://user:pass@dbhost:5432/mydb"
|
|
|
|
|
|
def test_database_url_normalizes_plain_postgresql_prefix():
|
|
"""DATABASE_URL with plain postgresql:// is normalized to postgresql+asyncpg://."""
|
|
env = {
|
|
"DATABASE_URL": "postgresql://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
|
}
|
|
settings = Settings(**env)
|
|
assert (
|
|
settings.database_url
|
|
== "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
|
)
|
|
|
|
|
|
def test_database_url_preserves_asyncpg_prefix():
|
|
"""CARTSNITCH_DATABASE_URL with postgresql+asyncpg:// is left unchanged."""
|
|
env = {
|
|
"CARTSNITCH_DATABASE_URL": "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
|
}
|
|
settings = Settings(**env)
|
|
assert (
|
|
settings.database_url
|
|
== "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
|
)
|
|
|
|
|
|
def test_database_url_default(monkeypatch):
|
|
"""When neither env var is set, the hardcoded default is used."""
|
|
monkeypatch.delenv("CARTSNITCH_DATABASE_URL", raising=False)
|
|
monkeypatch.delenv("DATABASE_URL", raising=False)
|
|
settings = Settings()
|
|
assert (
|
|
settings.database_url
|
|
== "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
|
)
|