OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to access app dependency in FastAPI when not injecting to endpoint

  • Thread starter Thread starter dam
  • Start date Start date
D

dam

Guest
In my main.py where the FastApi app is defined I need to do some initialization where I load from a database into a cache. When my app runs normally, it calls a get_db() function to return a SqlServer session and this get_db function is used in endpoints using session=Depends(get_db) in the parameters. I need some kind of call to get the correct get_db function based on app dependencies, so that it will use the override version that I defined in testconf.py when I'm running pytests. How do I do this?

Code:
#database.py
    engine = create_engine("sqlserver connection string here")
    SessionLocal - sessionmaker(autocommit=False,autoflush=False,bind=engine)
    def get_db ():
       # standard way to get db in FastAPI
       db = SessionLocal()
       yield db

#main.py
from database import get_db

# When running pytests, its not finding the override version in 
# in the app dependencies
if fn := app.dependency_overrides.get(get_db):
   sess = next(fn())  
else:
   sess = next(get_db())
CacheService.load_cache(sess) 

testconf.py

#defines fixtures used by pytests
@pytest.fixture 
def engine():
   url = "sqlite://"
   return create_engine(url, ...)

@pytest.fixture
def app_session(engine):
   TestingSessionLocal = sessionmaker(...bind=engine)
   def override_get_db():
      db = TestingSessionLocal()
      yield db
   app.dependency_overrides[get_db] = override_get_db
   return app

@pytest.fixture
def client(app_session):
   return TestClient(app=appsession)

#some test
def test_something(app_session, client):
    # It loads the caches using a sqlserver session 
    client.get("url")
<p>In my main.py where the FastApi app is defined I need to do some initialization where I load from a database into a cache. When my app runs normally, it calls a get_db() function to return a SqlServer session and this get_db function is used in endpoints using <code>session=Depends(get_db)</code> in the parameters. I need some kind of call to get the correct get_db function based on app dependencies, so that it will use the override version that I defined in testconf.py when I'm running pytests. How do I do this?</p>
<pre><code>#database.py
engine = create_engine("sqlserver connection string here")
SessionLocal - sessionmaker(autocommit=False,autoflush=False,bind=engine)
def get_db ():
# standard way to get db in FastAPI
db = SessionLocal()
yield db

#main.py
from database import get_db

# When running pytests, its not finding the override version in
# in the app dependencies
if fn := app.dependency_overrides.get(get_db):
sess = next(fn())
else:
sess = next(get_db())
CacheService.load_cache(sess)

testconf.py

#defines fixtures used by pytests
@pytest.fixture
def engine():
url = "sqlite://"
return create_engine(url, ...)

@pytest.fixture
def app_session(engine):
TestingSessionLocal = sessionmaker(...bind=engine)
def override_get_db():
db = TestingSessionLocal()
yield db
app.dependency_overrides[get_db] = override_get_db
return app

@pytest.fixture
def client(app_session):
return TestClient(app=appsession)

#some test
def test_something(app_session, client):
# It loads the caches using a sqlserver session
client.get("url")

</code></pre>
 

Latest posts

Top