Coder Social home page Coder Social logo

0xtheprodev / fastapi-clean-example Goto Github PK

View Code? Open in Web Editor NEW
293.0 4.0 51.0 103 KB

Clean Architecture Example using FastAPI framework

License: MIT License

Python 100.00%
fastapi python3 swagger-ui clean-architecture hexagonal-architecture repository-pattern openapi3 pydantic swagger graphql

fastapi-clean-example's Introduction

fastapi-clean-example

Python FastAPI OpenAPI Swagger GraphQL Code style: black Typed with: pydantic Open Issues Closed Issues Open Pulls Closed Pulls Contributors Activity

Description

Example Application Interface using FastAPI framework in Python 3

This example showcases Repository Pattern in Hexagonal Architecture (also known as Clean Architecture). Here we have two Entities - Books and Authors, whose relationships have been exploited to create CRUD endpoint in REST under OpenAPI standard.

Installation

  • Install all the project dependency using Pipenv:

    $ pipenv install --dev
  • Run the application from command prompt:

    $ pipenv run uvicorn main:app --reload
  • You can also open a shell inside virtual environment:

    $ pipenv shell
  • Open localhost:8000/docs for API Documentation

  • Open localhost:8000/graphql for GraphQL Documentation

Note: In case you are not able to access pipenv from you PATH locations, replace all instances of pipenv with python3 -m pipenv.

Testing

For Testing, unittest module is used for Test Suite and Assertion, whereas pytest is being used for Test Runner and Coverage Reporter.

  • Run the following command to initiate test:
    $ pipenv run pytest
  • To include Coverage Reporting as well:
    $ pipenv run pytest --cov-report xml --cov .

License

ยฉ MIT License

fastapi-clean-example's People

Contributors

0xtheprodev avatar omar-dulaimi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fastapi-clean-example's Issues

Incomplete Testing

Hi there, Currently I've seen that it have very limited testing, usually all of the functions are just checked if they are being called however, in real scenarios, It is not sufficient.

Ask : Nestjs Architecture

Hi @Progyan1997 , first of all thanks for sharing this Project. ๐Ÿ™๐Ÿป

I am used to Nestjs, and it was mind-blowing to finally found modern Python project that structured in similar way to Nestjs.

By the way, is it just me or it is kinda inspired by NestJs project structure ?

Dependency Injection

Hi there, I've seen that database instance is created in constructor of Repositories, however The dependency injection should be utilized here for proper testing, By this way, we are unable to use mock, or any different session.

Testing the repository, service layer and suggestions on how to test routes.

Dear Progyan,

I am writing to inquire about your approach to testing the FastAPI clean code project. I have a similar project and I am writing unit tests and integration tests for it. However, testing the repository layer and service layer has been challenging for me.

I saw how you implemented tests in this project, and you assert if the repository functions are called once, but do not assert what the function call returns. Could you please explain why you took this approach?

You also did something similar in the service layer. I am curious to know why you chose this approach instead of testing the return values and expected results at least in the service layer.

How would you suggest i test the service layer against some expected results, using the (Given, When, Then) approach? and also your suggestions on how to test the routes/endpoints

Thank you for your time

SQLAlchemy Session issue

Hi , I came a lot of Fast-api project template and came accross to this issue

def get_db_connection():
    db = scoped_session(SessionLocal)
    try:
        yield db
    finally:
        db.close()

I was wondering, whether this approach already considers issues like this and this ?

Error wile running the project locally

Hey,

I'm a python noob, faced this error and not sure if I'm missing a dependency or the project.
Here's the message:

ModuleNotFoundError: No module named 'configs.Environment'

question on service-to-service

Is it an issue that we expose models directly? For example in BookService we expose model in get method, and we also use models of Author repository.
This implies that we are allowed to use those interfaces - CRUD, read all properties, call other relations, etc.
Is exposing schemas objects between domains is a better solution?

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.