fix: test isolation + auth import compatibility + add optuna to requirements (HRT-136)
Test isolation fixes: - auth_db.get_db(): read TURF_SAAS_DB dynamically (not frozen at import) - api_v1/utils.get_db(): read TURF_SAAS_DB dynamically (not frozen at import) - api_tokens_db.get_db(): read TURF_SAAS_DB dynamically (not frozen at import) - tests/test_history.py: enforce _tmp_db.name + call init_auth_tables() in fixtures - tests/test_user_tokens.py: enforce _tmp_db.name + call migrate_api_tokens_tables() in app fixture Auth compatibility fixes: - api_v1/routes/history.py: use auth.jwt_required_middleware (flask_jwt_extended) with saas_auth fallback for portal_server context - api_v1/routes/ml_feedback.py: same auth import strategy - api_v1/routes/user.py: same auth import strategy Dependencies: - requirements.txt: add optuna>=4.0.0 (used in ML ensemble tests and training) Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -52,6 +52,9 @@ def auth_header(token: str) -> dict:
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def app():
|
||||
# Enforce this module s temp DB
|
||||
os.environ["TURF_SAAS_DB"] = _tmp_db.name
|
||||
os.environ["JWT_SECRET_KEY"] = "test-history-secret-key"
|
||||
application = create_app()
|
||||
application.config["TESTING"] = True
|
||||
application.config["JWT_SECRET_KEY"] = "test-history-secret-key"
|
||||
@@ -70,7 +73,14 @@ def seeded_db():
|
||||
- Create ml_predictions_cache with rows spanning 120 days back
|
||||
- Create users for free/premium/pro plans
|
||||
"""
|
||||
db_path = os.environ["TURF_SAAS_DB"]
|
||||
# Reset TURF_SAAS_DB to this module-s temp DB at runtime
|
||||
os.environ["TURF_SAAS_DB"] = _tmp_db.name
|
||||
db_path = _tmp_db.name
|
||||
|
||||
# Ensure auth tables (users, refresh_tokens, subscriptions) exist in the test DB
|
||||
# init_auth_tables() is idempotent — safe to call even if tables already exist
|
||||
init_auth_tables()
|
||||
|
||||
conn = sqlite3.connect(db_path)
|
||||
|
||||
# Create ml_predictions_cache table if absent
|
||||
@@ -124,7 +134,9 @@ def auth_tokens(client, seeded_db):
|
||||
assert r.status_code in (201, 409), f"register failed for {plan}: {r.data}"
|
||||
|
||||
# Set plan via direct DB
|
||||
db_path = os.environ["TURF_SAAS_DB"]
|
||||
# Reset TURF_SAAS_DB to this module-s temp DB at runtime
|
||||
os.environ["TURF_SAAS_DB"] = _tmp_db.name
|
||||
db_path = _tmp_db.name
|
||||
conn = sqlite3.connect(db_path)
|
||||
for plan, email in plans.items():
|
||||
conn.execute("UPDATE users SET plan = ? WHERE email = ?", (plan, email))
|
||||
|
||||
Reference in New Issue
Block a user