Git Product home page Git Product logo

pytest-fastapi-crud-example's Introduction

User Service API Example

Overview

This is a simple User Service CRUD (Create, Read, Update, Delete) API built with FastAPI and SQLite. The API allows you to create, read, update, and delete users. It uses Pydantic models for request and response validation and SQLAlchemy for database operations.

Architecture

This project follows a clean architecture pattern, separating concerns to enhance maintainability and scalability. Here's a brief overview:

  • API Layer (FastAPI): Handles HTTP requests and responses, routing, and interaction with the service layer.
  • Service Layer: Contains business logic and communicates with the database layer.
  • Database Layer (SQLite): Manages data persistence and database operations.
  • Testing: Unit tests are written in Pytest to test the service layer functions.

Getting Started

Prerequisites and Dependencies

  • Python 3.12
  • FastAPI
  • SQLite
  • Uvicorn (for running the server)

Poetry

This project uses Poetry for dependency management.

If you're not familiar with Poetry, please follow these instructions to install it.

Once you've installed Poetry, you can install the dependencies using the following command:

$ poetry install

Then run the below command to activate the virtual environment.

$ poetry shell

Pip

If you prefer using pip, you can create a virtual environment and then install the dependencies using the following command:

$ pip install -r requirements.txt

How To Run the Server

To run the server, use the following command:

$ uvicorn app.main:app --host localhost --port 8000 --reload

This will spin up the server at http://localhost:8000 with a local SQLite database users.db.

API Endpoints

Create User

  • POST /api/users/: Create a new user.

To create a user, send a POST request to http://localhost:8000/api/users with the following JSON payload:

{
    "first_name": "John",
    "last_name": "Doe",
    "address": "123 Fake St",
    "activated": true
}

As we use Pydantic models, the API will validate the request payload and return an error if the payload is invalid.

Get Users

  • GET /api/users/: Get all users.

To get all users, send a GET request to http://localhost:8000/api/users.

Get User by ID

  • GET /api/users/{userId}/: Get a user by ID.

To get a user by ID, send a GET request to http://localhost:8000/api/users/{userId}.

If the user with the specified ID does not exist, the API will return a 404 Not Found response. The same logic is carried out for the Update and Delete endpoints.

Update User

  • PATCH /api/users/{userId}/: Update a user by ID.

To update a user by ID, send a PATCH request to http://localhost:8000/api/users/{userId} with the following JSON payload:

{
    "first_name": "Jane",
    "last_name": "Doe",
    "address": "321 Fake St",
    "activated": true
}

Delete User

  • DELETE /api/users/{userId}/: Delete a user by ID.

To delete a user by ID, send a DELETE request to http://localhost:8000/api/users/{userId}.

How To Run the Unit Tests

To run the Unit Tests, from the root of the repo run

$ pytest 

This will spin up a test database in SQLite test_db.db, run the tests and then tear down the database.

You can use pytest -v for verbose output and pytest -s to disable output capture for better debugging.

pytest-fastapi-crud-example's People

Contributors

ericsalesdeandrade avatar

Stargazers

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

Watchers

 avatar

pytest-fastapi-crud-example's Issues

requirements.txt file issues

When doing a pip install -r requirements.txt with your file on my m1 mac with a virtualenv set up, I got failures. Looking at the file it seems there are quite a few unnecessary packages there, including the one that failed.
So I started minimal and installed each missing package found when running the unit test.

The result is a much more minimal file, and it works.
anyio==3.7.0 certifi==2023.5.7 fastapi==0.95.2 fastapi-utils==0.2.1 h11==0.14.0 httpcore==0.17.2 httpx==0.24.1 idna==3.4 iniconfig==2.0.0 packaging==23.1 pluggy==1.0.0 pydantic==1.10.8 pytest==7.3.1 sniffio==1.3.0 SQLAlchemy==1.4.48 starlette==0.27.0 typing_extensions==4.6.2

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.