Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
FastAPI endpoint test - database session not sync

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.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda