DerevenetsArtyom / tdd-fastapi

Text summarization service with Python, FastAPI, and Docker. The service itself is exposed via a RESTful API and deployed to Heroku with Docker.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test-Driven Development with FastAPI and Docker

Continuous Integration and Delivery

Table of contents

About

fastapi-tdd is a code-along (with some differences) to the course Test-Driven Development with FastAPI and Docker by Michael Herman. Link to original repo.

Objectives achieved in the project

  • Develop an asynchronous RESTful API with Python and FastAPI
  • Practice Test-Driven Development
  • Test a FastAPI app with pytest
  • Interact with a Postgres database asynchronously
  • Containerize FastAPI and Postgres inside a Docker container
  • Run unit and integration tests with code coverage inside a Docker container
  • Check the code for any code quality issues via a linter
  • Configure GitHub Actions for continuous integration and deployment
  • Use GitHub Packages to store Docker Images
  • Speed up a Docker-based CI build with Docker Cache
  • Deploy FastAPI, Uvicorn, and Postgres to Heroku with Docker
  • Parameterize test functions and mock functionality in tests with pytest
  • Run tests in parallel with pytest-xdist
  • Document a RESTful API with Swagger/OpenAPI (out of the box)
  • Run a background process outside the request/response flow

Tools and technologies

This project uses a variety of technologies and services:

Endpoints

Endpoint HTTP Method CRUD Method Result
/summaries GET READ get all summaries
/summaries/:id GET READ get a single summary
/summaries POST CREATE add a summary
/summaries/:id PUT UPDATE update a summary
/summaries/:id DELETE DELETE delete a summary
/ping GET ------ get test json
/docs GET ------ get the docs

Useful commands

Build and Launch Containers

docker-compose up -d --build

Accessing Database

docker-compose exec web-db psql -U postgres

postgres=# \c web_dev
postgres=# \q

Update Database Schema

docker-compose exec web python app/db.py

Run Tests

# normal run
$ docker-compose exec web python -m pytest

# disable warnings
$ docker-compose exec web python -m pytest -p no:warnings

# run only the last failed tests
$ docker-compose exec web python -m pytest --lf

# run only the tests with names that match the string expression
$ docker-compose exec web python -m pytest -k "summary and not test_read_summary"

# stop the test session after the first failure
$ docker-compose exec web python -m pytest -x

# enter PDB after first failure then end the test session
$ docker-compose exec web python -m pytest -x --pdb

# stop the test run after two failures
$ docker-compose exec web python -m pytest --maxfail=2

# show local variables in tracebacks
$ docker-compose exec web python -m pytest -l

# list the 2 slowest tests
$ docker-compose exec web python -m pytest --durations=2

#Run unit tests in parallel
$ docker-compose exec web pytest -k "unit" -n auto

Build container for Heroku

docker build -f src/Dockerfile.prod -t registry.heroku.com/secret-dusk-86918/web ./src

Run Heroku container locally

docker run -d --name fastapi-tdd -e PORT=8765 -e DATABASE_URL=sqlite://sqlite.db -p 5003:8765 registry.heroku.com/secret-dusk-86918/web:latest

Push Container to Heroku Registry

docker push registry.heroku.com/secret-dusk-86918/web:latest

Build the image for GitHub

docker build -f src/Dockerfile.prod -t docker.pkg.github.com/derevenetsartyom/tdd-fastapi/summarizer:latest ./src/

Push the image to the Docker registry on GitHub Packages

docker push docker.pkg.github.com/derevenetsartyom/tdd-fastapi/summarizer:latest

About

Text summarization service with Python, FastAPI, and Docker. The service itself is exposed via a RESTful API and deployed to Heroku with Docker.

License:MIT License


Languages

Language:Python 91.6%Language:Dockerfile 4.2%Language:Shell 2.5%Language:Makefile 1.8%