Skip to main content

FastAPI middleware for mocking responses based on Pydantic models response

Project description

fastmock

FastMock framework, mock your FastAPI APIs

Test Coverage Package version


Documentation: https://tpemeja.github.io/fastmock

Source Code: https://github.com/tpemeja/fastmock


FastMock is a Python framework designed to mock FastAPI APIs based on response models.

Key features of this project include:

  • Ease of Use: Simply add the middleware to start mocking your APIs.
  • Flexibility: Mocking can be customized through various parameters, such as activation, data generation, length, status codes, and more.
  • Modularity: The package includes default functions to retrieve mocking parameters from API declarations to request headers. However, all these functions can be modified, allowing you to create your own custom functions.

Requirements

This package is build to be used with FastAPI, please learn on to use it on their well documented website - FastAPI Documentation

To mock an API, you need to describe the API using FastAPI's decorator key responses.

For example:

@app.get("/",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": list[int]},
             status.HTTP_404_NOT_FOUND: {"model": str}
         })

Installation

$ pip install fastmock

---> 100%

Example

Create it

  • Create a file main.py with:
from fastapi import FastAPI, status
from pydantic import BaseModel

from fastmock.decorator import FastMockDecorator
from fastmock.middleware import FastMockMiddleware

app = FastAPI()
app.add_middleware(FastMockMiddleware)


mock = FastMockDecorator()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool | None = None


@app.get("/",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": dict[str, str]}
         })
def read_root():
    return {"Hello": "World"}


@mock(element_size=3)
@app.get("/items",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": list[Item]}
         })
def read_items():
    return []
  • Start the server with uvicorn main:app

Check it

Open your browser at http://127.0.0.1:8000/items.

You will see the JSON response as:

[
  {
    "name": "fuquvERvYTfWVEbYRKgi",
    "price": 18164954265977.8,
    "is_offer": null
  },
  {
    "name": "akMCejCxOhMjgGMPMrcb",
    "price": 40.3130726635657,
    "is_offer": null
  },
  {
    "name": "uEONHBXGCirPDrLJKgXu",
    "price": -9.23356705084994,
    "is_offer": null
  }
]

You just created an API using FastAPI that:

  • Return value generated from defined response model
  • Modify output list size using decorator

Inspiration

  • A project idea that came from my use of FastAPI by tiangolo, the GitHub project inspired me for the structure and documentation of this project.
  • Ideas and code for data generation with Faker inspired by NiyazNz in the fastapi-mock-middleware project.

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastmock-0.1.1.tar.gz (13.5 kB view hashes)

Uploaded Source

Built Distribution

fastmock-0.1.1-py3-none-any.whl (19.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page