def test_app(): # set up app = create_application() app.dependency_overrides[get_settings] = get_settings_override with TestClient(app) as test_client: # testing yield test_client
def test_app_with_db(): def override_get_db(): try: db = testing_session_local() yield db finally: db.close() # if os.environ.get("DATABASE_TEST_URL"): # SQLALCHEMY_DATABASE_URL = os.environ.get("DATABASE_TEST_URL") # else: # SQLALCHEMY_DATABASE_URL = "postgres://*****:*****@localhost:5432/web_test" SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db" engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) testing_session_local = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base.metadata.create_all(bind=engine) app = create_application() # new app.dependency_overrides[get_settings] = get_settings_override app.dependency_overrides[get_db] = override_get_db with TestClient(app) as test_client: # updated # testing yield test_client
def test_app(): """CReates a test app fixture to replace the real app""" # set up app = create_application() app.dependency_overrides[get_settings] = get_settings_override with TestClient(app) as test_client: # testing yield test_client
def test_app(): # set up app = create_application() with TestClient(app) as test_client: # testing yield test_client
def test_app(): app = create_application() app.dependency_overrides[get_db] = override_get_db app.dependency_overrides[get_settings] = override_test_settings app.dependency_overrides[get_current_user] = override_get_current_user with TestClient(app) as test_client: yield test_client
def test_app(): app = create_application() # app.dependency_overrides is a dict of key/value pairs where the key is the # dependency name and the value is what we'd like to override it with: app.dependency_overrides[get_settings] = get_settings_override with TestClient(app) as test_client: # testing yield test_client
def test_app(): app = create_application() # new app.dependency_overrides[get_settings] = get_settings_override generate_schema() with TestClient(app) as test_client: # updated # testing yield test_client
def test_app(): # set up app = create_application() # new app.dependency_overrides[get_settings] = get_settings_override initializer(["app.models.tortoise"]) with TestClient(app) as test_client: # testing yield test_client finalizer()
def test_app_with_db() -> Generator: app = create_application() app.dependency_overrides[get_settings] = get_settings_override initializer(db_url=os.environ.get("DATABASE_TEST_URL"), modules=["app.models.tortoise.users"]) run_async(add_users()) with TestClient(app) as test_client: yield test_client finalizer()
def client() -> Generator: # set up app = create_application() # app.dependency_overrides[get_settings] = get_settings_override settings = get_settings() initializer(["app.models"], db_url=settings.database_test_url) with TestClient(app) as test_client: # testing yield test_client finalizer()
def client() -> Generator: # set up app = create_application() initializer(settings.MODELS, db_url=settings.TEST_DATABASE_URL) with TestClient(app) as test_client: # testing yield test_client # tear down finalizer()
def test_app_with_db(): app = create_application() app.dependency_overrides[get_settings] = get_settings_override register_tortoise( app, db_url=os.environ.get("DATABASE_TEST_URL"), modules={"models": ["app.models.tortoise"]}, generate_schemas=True, add_exception_handlers=True, ) with TestClient(app) as test_client: yield test_client
def test_app_with_db(): # set up app = create_application() app.dependency_overrides[get_settings] = get_settings_override # Link with DB for testing initializer( ["app.infra.postgres.models"], db_url=os.environ.get("DATABASE_TEST_URL"), ) with TestClient(app) as test_client: # testing yield test_client
def test_app(): # set up print("Starting test ...") app = create_application() app.dependency_overrides[get_settings] = get_settings_override with TestClient(app) as test_client: # testing yield test_client # tear down print("Test finished !")
def test_app_with_db(db_url): # set up app = create_application() app.dependency_overrides[get_settings] = get_settings_override register_tortoise( app, db_url=db_url, modules={"models": ["app.models.tortoise"]}, generate_schemas=True, add_exception_handlers=True, ) with TestClient(app) as test_client: # testing yield test_client
def test_app_with_db(): """Client for summarizer app tests with a DB.""" app = create_application() app.dependency_overrides[get_settings] = get_settings_override register_tortoise( app, db_url=os.getenv('DATABASE_TEST_URL'), modules={'models': ['app.models']}, generate_schemas=True, add_exception_handlers=True, ) with TestClient(app) as test_client: yield test_client
def test_app_tacacs() -> Generator: app = create_application() app.dependency_overrides[get_settings] = get_settings_override_tacacs with TestClient(app) as test_client: yield test_client
import logging import uvicorn from app.db import init_db from app.main import create_application logger = logging.getLogger(__name__) application = create_application() @application.on_event("startup") async def startup_event(): logger.info("Starting up...") init_db(application) @application.on_event("shutdown") async def shutdown_event(): logger.info("Shutting down...") if __name__ == "__main__": uvicorn.run(application, host="0.0.0.0", port=8000, log_level="debug")
def app() -> FastAPI: return create_application()
def test_app(): """Basic client for summarizer app tests without DB.""" app = create_application() app.dependency_overrides[get_settings] = get_settings_override with TestClient(app) as test_client: yield test_client