52 lines
1.1 KiB
Python
52 lines
1.1 KiB
Python
import os
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import StaticPool, create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from allmende_payment_system.api.dependencies import get_session
|
|
from allmende_payment_system.app import app
|
|
from allmende_payment_system.models import Base
|
|
|
|
|
|
def make_db():
|
|
engine = create_engine(
|
|
"sqlite:///:memory:",
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
Base.metadata.create_all(bind=engine) # Create tables
|
|
return sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def make_in_memory_session():
|
|
db = make_db()
|
|
session = db()
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
app.dependency_overrides[get_session] = make_in_memory_session
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def client():
|
|
os.environ["APS_username"] = "test"
|
|
return TestClient(app)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def unauthorized_client():
|
|
os.environ.pop("APS_username", None)
|
|
return TestClient(app)
|
|
|
|
|
|
@pytest.fixture
|
|
def test_db():
|
|
db = make_db()
|
|
return db()
|