I have an issue when I test my API, the DB session at the tests isn't up to date when performing post request.
For example:
@pytest.fixture(scope="module")
def db(engine, tables, users) -> Generator:
"""Returns an sqlalchemy session, and after the test tears down everything properly."""
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
def test_test(client: TestClient, db: Session):
item_id = 1
params = {
"item_id": item_id,
}
r = client.post("/endpoint",json=params)
db2: SessionLocal = TestingSessionLocal() # TEMP SOLUTION
# item_from_db = service.table.get(db, item_id) # TODO: make it work!!
item_from_db = service.event.get(db2, item_id)
db2.close()
I see the new item at my DB at run time, but I cant get it with the fixture. Only when I create a new connection to DB, I manage to get the new item I added.