|
1 | | -# Pytest FastAPI CRUD Example |
| 1 | +# User Service API Example |
2 | 2 |
|
3 | | -This repo contains the sample code for the article - [Building And Testing FastAPI CRUD APIs With Pytest (Hands-On Tutorial)](https://pytest-with-eric.com/pytest-advanced/pytest-fastapi-testing/) |
| 3 | +## Overview |
4 | 4 |
|
5 | | -This project explains how to Build and Test A CRUD Rest API using FastAPI, SQLite (via SQLAlchemy) and Pytest. |
| 5 | +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. |
6 | 6 |
|
7 | | -# Requirements |
8 | | -* Python (3.12) |
| 7 | +## Architecture |
| 8 | +This project follows a clean architecture pattern, separating concerns to enhance maintainability and scalability. Here's a brief overview: |
9 | 9 |
|
10 | | -This repo uses [Poetry](https://python-poetry.org/) for dependency management. |
| 10 | +- API Layer (FastAPI): Handles HTTP requests and responses, routing, and interaction with the service layer. |
| 11 | +- Service Layer: Contains business logic and communicates with the database layer. |
| 12 | +- Database Layer (SQLite): Manages data persistence and database operations. |
| 13 | +- Testing: Unit tests are written in Pytest to test the service layer functions. |
| 14 | + |
| 15 | +## Getting Started |
| 16 | + |
| 17 | +### Prerequisites and Dependencies |
| 18 | +- Python 3.12 |
| 19 | +- FastAPI |
| 20 | +- SQLite |
| 21 | +- Uvicorn (for running the server) |
| 22 | + |
| 23 | +#### Poetry |
| 24 | + |
| 25 | +This project uses [Poetry](https://python-poetry.org/) for dependency management. |
| 26 | + |
| 27 | +If you're not familiar with Poetry, please follow [these instructions](https://python-poetry.org/docs/#installation) to install it. |
| 28 | + |
| 29 | +Once you've installed Poetry, you can install the dependencies using the following command: |
11 | 30 |
|
12 | | -Please install the dependencies using |
13 | 31 | ```shell |
14 | 32 | $ poetry install |
15 | 33 | ``` |
16 | 34 |
|
17 | | -# How To Run the Unit Tests |
| 35 | +Then run the below command to activate the virtual environment. |
| 36 | + |
| 37 | +```shell |
| 38 | +$ poetry shell |
| 39 | +``` |
| 40 | + |
| 41 | +#### Pip |
| 42 | + |
| 43 | +If you prefer using `pip`, you can create a virtual environment and then install the dependencies using the following command: |
| 44 | + |
| 45 | +```shell |
| 46 | +$ pip install -r requirements.txt |
| 47 | +``` |
| 48 | + |
| 49 | +## How To Run the Server |
| 50 | + |
| 51 | +To run the server, use the following command: |
| 52 | + |
| 53 | +```shell |
| 54 | +$ uvicorn app.main:app --host localhost --port 8000 --reload |
| 55 | +``` |
| 56 | + |
| 57 | +This will spin up the server at `http://localhost:8000` with a local SQLite database `users.db`. |
| 58 | + |
| 59 | +## API Endpoints |
| 60 | + |
| 61 | +### Create User |
| 62 | + |
| 63 | +- `POST /api/users/`: Create a new user. |
| 64 | + |
| 65 | +To create a user, send a POST request to `http://localhost:8000/api/users` with the following JSON payload: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "first_name": "John", |
| 70 | + "last_name": "Doe", |
| 71 | + "address": "123 Fake St", |
| 72 | + "activated": true |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +As we use Pydantic models, the API will validate the request payload and return an error if the payload is invalid. |
| 77 | + |
| 78 | +### Get Users |
| 79 | + |
| 80 | +- `GET /api/users/`: Get all users. |
| 81 | + |
| 82 | +To get all users, send a GET request to `http://localhost:8000/api/users`. |
| 83 | + |
| 84 | +### Get User by ID |
| 85 | + |
| 86 | +- `GET /api/users/{userId}/`: Get a user by ID. |
| 87 | + |
| 88 | +To get a user by ID, send a GET request to `http://localhost:8000/api/users/{userId}`. |
| 89 | + |
| 90 | +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. |
| 91 | + |
| 92 | + |
| 93 | +### Update User |
| 94 | + |
| 95 | +- `PATCH /api/users/{userId}/`: Update a user by ID. |
| 96 | + |
| 97 | +To update a user by ID, send a PATCH request to `http://localhost:8000/api/users/{userId}` with the following JSON payload: |
| 98 | + |
| 99 | +```json |
| 100 | +{ |
| 101 | + "first_name": "Jane", |
| 102 | + "last_name": "Doe", |
| 103 | + "address": "321 Fake St", |
| 104 | + "activated": true |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Delete User |
| 109 | + |
| 110 | +- `DELETE /api/users/{userId}/`: Delete a user by ID. |
| 111 | + |
| 112 | +To delete a user by ID, send a DELETE request to `http://localhost:8000/api/users/{userId}`. |
| 113 | + |
| 114 | +## How To Run the Unit Tests |
18 | 115 | To run the Unit Tests, from the root of the repo run |
19 | 116 | ```shell |
20 | | -pytest |
| 117 | +$ pytest |
21 | 118 | ``` |
22 | 119 |
|
23 | | -If you have any questions about the project please raise an Issue on GitHub. |
| 120 | +This will spin up a test database in SQLite `test_db.db`, run the tests and then tear down the database. |
| 121 | + |
| 122 | +You can use `pytest -v` for verbose output and `pytest -s` to disable output capture for better debugging. |
0 commit comments