mfreeborn / fastapi-sqlalchemy

Adds simple SQLAlchemy support to FastAPI
MIT License
594 stars 34 forks source link

During pytest, `with db()` does not use test db #30

Open buddha314 opened 3 years ago

buddha314 commented 3 years ago

With the standard FastAPI test_db fixture, using

# service.py 

def print_my_object():
  with db():
    return x = models.MyObject().first()
    print(x)
# test.py
def test_printing(test_db, # <- the fixture
                           test_object # <- loads the object into the db
):
  retval = service.print_my_object
  assert retval is not None

fails. The service is not a route. How do I get the test_db into the service during testing?