GithubHelp home page GithubHelp logo

derevenetsartyom / tdd-fastapi Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 3.0 120 KB

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

Python 91.57% Dockerfile 4.17% Shell 2.49% Makefile 1.77%
docker fastapi tdd fastapi-docker python-3 docker-compose heroku-deployment tortoise-orm

tdd-fastapi's Introduction

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

tdd-fastapi's People

Contributors

derevenetsartyom avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.