This is a FastAPI + SQLAlchemy + Dependency Injector example application.
Thanks to @ShvetsovYura for providing initial example: FastAPI_DI_SqlAlchemy.
Build the Docker image:
docker compose build
Run the docker-compose environment:
docker compose up
The output should be something like:
Starting fastapi-sqlalchemy_webapp_1 ... done Attaching to fastapi-sqlalchemy_webapp_1 webapp_1 | 2022-02-04 22:07:19,804 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1 webapp_1 | 2022-02-04 22:07:19,804 INFO sqlalchemy.engine.base.Engine () webapp_1 | 2022-02-04 22:07:19,804 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1 webapp_1 | 2022-02-04 22:07:19,804 INFO sqlalchemy.engine.base.Engine () webapp_1 | 2022-02-04 22:07:19,805 INFO sqlalchemy.engine.base.Engine PRAGMA main.table_info("users") webapp_1 | 2022-02-04 22:07:19,805 INFO sqlalchemy.engine.base.Engine () webapp_1 | 2022-02-04 22:07:19,808 INFO sqlalchemy.engine.base.Engine PRAGMA temp.table_info("users") webapp_1 | 2022-02-04 22:07:19,808 INFO sqlalchemy.engine.base.Engine () webapp_1 | 2022-02-04 22:07:19,809 INFO sqlalchemy.engine.base.Engine webapp_1 | CREATE TABLE users ( webapp_1 | id INTEGER NOT NULL, webapp_1 | email VARCHAR, webapp_1 | hashed_password VARCHAR, webapp_1 | is_active BOOLEAN, webapp_1 | PRIMARY KEY (id), webapp_1 | UNIQUE (email), webapp_1 | CHECK (is_active IN (0, 1)) webapp_1 | ) webapp_1 | webapp_1 | webapp_1 | 2022-02-04 22:07:19,810 INFO sqlalchemy.engine.base.Engine () webapp_1 | 2022-02-04 22:07:19,821 INFO sqlalchemy.engine.base.Engine COMMIT webapp_1 | INFO: Started server process [8] webapp_1 | INFO: Waiting for application startup. webapp_1 | INFO: Application startup complete. webapp_1 | INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
After that visit http://127.0.0.1:8000/docs in your browser.
This application comes with the unit tests.
To run the tests do:
docker compose run --rm webapp py.test webapp/tests.py --cov=webapp
The output should be something like:
platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0 rootdir: /code plugins: cov-6.0.0, anyio-4.7.0 collected 7 items webapp/tests.py ....... [100%] ---------- coverage: platform linux, python 3.10.0-final-0 ---------- Name Stmts Miss Cover -------------------------------------------- webapp/__init__.py 0 0 100% webapp/application.py 12 0 100% webapp/containers.py 10 0 100% webapp/database.py 24 8 67% webapp/endpoints.py 32 0 100% webapp/models.py 10 1 90% webapp/repositories.py 36 20 44% webapp/services.py 16 0 100% webapp/tests.py 59 0 100% -------------------------------------------- TOTAL 199 29 85%