first commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import asyncio
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
import uuid
|
||||
from collections.abc import Iterator
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
TEST_ID = uuid.uuid4().hex
|
||||
TEST_DB = Path(tempfile.gettempdir()) / f"opencloud-{TEST_ID}.sqlite3"
|
||||
TEST_UPLOADS = Path(tempfile.gettempdir()) / f"opencloud-{TEST_ID}-uploads"
|
||||
|
||||
os.environ["ENVIRONMENT"] = "test"
|
||||
os.environ["DATABASE_URL"] = f"sqlite+aiosqlite:///{TEST_DB}"
|
||||
os.environ["SECRET_KEY"] = "test-secret-key-that-is-long-enough-for-opencloud"
|
||||
os.environ["PUBLIC_BASE_URL"] = "http://testserver"
|
||||
os.environ["FRONTEND_URL"] = "http://testserver"
|
||||
os.environ["CORS_ORIGINS"] = "http://testserver"
|
||||
os.environ["UPLOAD_DIR"] = str(TEST_UPLOADS)
|
||||
os.environ["EMAIL_DELIVERY_MODE"] = "console"
|
||||
os.environ["ADMIN_EMAIL"] = "admin@example.com"
|
||||
os.environ["ADMIN_PASSWORD"] = "admin-password-123"
|
||||
os.environ["ADMIN_USERNAME"] = "测试管理员"
|
||||
|
||||
from app.database import Base, engine # noqa: E402
|
||||
from app.main import app # noqa: E402
|
||||
from app.seed import seed # noqa: E402
|
||||
|
||||
|
||||
async def _prepare_database() -> None:
|
||||
async with engine.begin() as connection:
|
||||
await connection.run_sync(Base.metadata.create_all)
|
||||
await seed()
|
||||
|
||||
|
||||
async def _close_database() -> None:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def client() -> Iterator[TestClient]:
|
||||
asyncio.run(_prepare_database())
|
||||
with TestClient(app) as test_client:
|
||||
yield test_client
|
||||
asyncio.run(_close_database())
|
||||
TEST_DB.unlink(missing_ok=True)
|
||||
shutil.rmtree(TEST_UPLOADS, ignore_errors=True)
|
||||
Reference in New Issue
Block a user