{"id":22121051,"url":"https://github.com/pytest-with-eric/pytest-fastapi-crud-example","last_synced_at":"2025-07-25T13:31:48.674Z","repository":{"id":237287837,"uuid":"613803678","full_name":"Pytest-with-Eric/pytest-fastapi-crud-example","owner":"Pytest-with-Eric","description":"Testing a FastAPI CRUID API using Pytest","archived":false,"fork":false,"pushed_at":"2024-05-09T15:35:14.000Z","size":47,"stargazers_count":13,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-05-09T16:46:24.956Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pytest-with-eric.com/pytest-advanced/pytest-fastapi-testing/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pytest-with-Eric.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-14T09:57:21.000Z","updated_at":"2024-05-09T15:35:18.000Z","dependencies_parsed_at":"2024-04-30T18:30:41.247Z","dependency_job_id":"17ccfbed-ce7d-49f5-92cb-fd806f4eda90","html_url":"https://github.com/Pytest-with-Eric/pytest-fastapi-crud-example","commit_stats":null,"previous_names":["ericsalesdeandrade/pytest-fastapi-crud-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pytest-with-Eric%2Fpytest-fastapi-crud-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pytest-with-Eric%2Fpytest-fastapi-crud-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pytest-with-Eric%2Fpytest-fastapi-crud-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pytest-with-Eric%2Fpytest-fastapi-crud-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pytest-with-Eric","download_url":"https://codeload.github.com/Pytest-with-Eric/pytest-fastapi-crud-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227578936,"owners_count":17788960,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-01T14:33:15.722Z","updated_at":"2024-12-01T14:33:16.328Z","avatar_url":"https://github.com/Pytest-with-Eric.png","language":"Python","readme":"# User Service API Example\n\n## Overview\n\nThis 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.\n\n## Architecture\nThis project follows a clean architecture pattern, separating concerns to enhance maintainability and scalability. Here's a brief overview:\n\n- API Layer (FastAPI): Handles HTTP requests and responses, routing, and interaction with the service layer.\n- Service Layer: Contains business logic and communicates with the database layer.\n- Database Layer (SQLite): Manages data persistence and database operations.\n- Testing: Unit tests are written in Pytest to test the service layer functions.\n\n## Getting Started\n\n### Prerequisites and Dependencies\n- Python 3.12\n- FastAPI\n- SQLite\n- Uvicorn (for running the server)\n\n#### Poetry\n\nThis project uses [Poetry](https://python-poetry.org/) for dependency management. \n\nIf you're not familiar with Poetry, please follow [these instructions](https://python-poetry.org/docs/#installation) to install it.\n\nOnce you've installed Poetry, you can install the dependencies using the following command:\n\n```shell\n$ poetry install\n```\n\nThen run the below command to activate the virtual environment.\n\n```shell\n$ poetry shell\n```\n\n#### Pip\n\nIf you prefer using `pip`, you can create a virtual environment and then install the dependencies using the following command:\n\n```shell\n$ pip install -r requirements.txt\n```\n\n## How To Run the Server\n\nTo run the server, use the following command:\n\n```shell\n$ uvicorn app.main:app --host localhost --port 8000 --reload\n```\n\nThis will spin up the server at `http://localhost:8000` with a local SQLite database `users.db`.\n\n## API Endpoints\n\n### Create User\n\n- `POST /api/users/`: Create a new user.\n\nTo create a user, send a POST request to `http://localhost:8000/api/users` with the following JSON payload:\n\n```json\n{\n    \"first_name\": \"John\",\n    \"last_name\": \"Doe\",\n    \"address\": \"123 Fake St\",\n    \"activated\": true\n}\n```\n\nAs we use Pydantic models, the API will validate the request payload and return an error if the payload is invalid.\n\n### Get Users\n\n- `GET /api/users/`: Get all users.\n\nTo get all users, send a GET request to `http://localhost:8000/api/users`.\n\n### Get User by ID\n\n- `GET /api/users/{userId}/`: Get a user by ID.\n\nTo get a user by ID, send a GET request to `http://localhost:8000/api/users/{userId}`. \n\nIf 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.\n\n\n### Update User\n\n- `PATCH /api/users/{userId}/`: Update a user by ID.\n\nTo update a user by ID, send a PATCH request to `http://localhost:8000/api/users/{userId}` with the following JSON payload:\n\n```json\n{\n    \"first_name\": \"Jane\",\n    \"last_name\": \"Doe\",\n    \"address\": \"321 Fake St\",\n    \"activated\": true\n}\n```\n\n### Delete User\n\n- `DELETE /api/users/{userId}/`: Delete a user by ID.\n\nTo delete a user by ID, send a DELETE request to `http://localhost:8000/api/users/{userId}`.\n\n## How To Run the Unit Tests\nTo run the Unit Tests, from the root of the repo run\n```shell\n$ pytest \n```\n\nThis will spin up a test database in SQLite `test_db.db`, run the tests and then tear down the database. \n\nYou can use `pytest -v` for verbose output and `pytest -s` to disable output capture for better debugging.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpytest-with-eric%2Fpytest-fastapi-crud-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpytest-with-eric%2Fpytest-fastapi-crud-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpytest-with-eric%2Fpytest-fastapi-crud-example/lists"}