50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from alembic import command
|
|
from alembic.config import Config
|
|
from fastapi.testclient import TestClient
|
|
|
|
# Keep a single sqlite target for app + alembic during tests.
|
|
TEST_DATABASE_URL = "sqlite+aiosqlite:///./app.db"
|
|
os.environ["DATABASE_URL"] = TEST_DATABASE_URL
|
|
os.environ["OPENBAO__SETTINGS__DATABASE_URL"] = TEST_DATABASE_URL
|
|
os.environ["OPENBAO__SETTINGS_TEST__DATABASE_URL"] = TEST_DATABASE_URL
|
|
os.environ["OPENBAO__SETTINGS__DEBUG"] = "true"
|
|
os.environ["OPENBAO__SETTINGS_TEST__DEBUG"] = "true"
|
|
|
|
from src.main import app
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def migrate_db():
|
|
db_files = [Path("app.db"), Path("dfip_budget_planing.db"), Path("dfip_new_schema.db")]
|
|
for db_file in db_files:
|
|
if db_file.exists():
|
|
os.remove(db_file)
|
|
|
|
alembic_cfg = Config("alembic.ini")
|
|
command.upgrade(alembic_cfg, "head")
|
|
yield
|
|
|
|
for db_file in db_files:
|
|
if db_file.exists():
|
|
os.remove(db_file)
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_tokens(client):
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username": "admin", "password": "admin"},
|
|
)
|
|
assert response.status_code == 200
|
|
return response.json()
|