|
1 | | -# FASTAPI |
2 | | -FastAPI boilerplate |
| 1 | +# **FASTAPI Boilerplate** |
| 2 | +A FastAPI boilerplate for efficient project setup. |
3 | 3 |
|
4 | | -## Setup |
| 4 | +## **Cloning the Repository** |
5 | 5 |
|
6 | | -1. Create a virtual environment. |
7 | | - ```sh |
8 | | - python3 -m venv .venv |
9 | | - ``` |
10 | | -2. Activate virtual environment. |
11 | | -```sh |
12 | | - source /path/to/venv/bin/activate` |
13 | | -``` |
14 | | -3. Install project dependencies `pip install -r requirements.txt` |
15 | | -4. Create a .env file by copying the .env.sample file |
16 | | -`cp .env.sample .env` |
| 6 | +1. **Fork the repository** and clone it: |
| 7 | + ```sh |
| 8 | + git clone https://github.com/<username>/hng_boilerplate_python_fastapi_web.git |
| 9 | + ``` |
| 10 | +2. **Navigate into the project directory**: |
| 11 | + ```sh |
| 12 | + cd hng_boilerplate_python_fastapi_web |
| 13 | + ``` |
| 14 | +3. **Switch to the development branch** (if not already on `dev`): |
| 15 | + ```sh |
| 16 | + git checkout dev |
| 17 | + ``` |
17 | 18 |
|
18 | | -5. Start server. |
19 | | - ```sh |
20 | | - python main.py |
21 | | -``` |
22 | 19 |
|
23 | | -## **DATABASE TEST SETUP** |
| 20 | +## **Setup Instructions** |
| 21 | + |
| 22 | +1. **Create a virtual environment**: |
| 23 | + ```sh |
| 24 | + python3 -m venv .venv |
| 25 | + ``` |
| 26 | +2. **Activate the virtual environment**: |
| 27 | + - On macOS/Linux: |
| 28 | + ```sh |
| 29 | + source .venv/bin/activate |
| 30 | + ``` |
| 31 | + - On Windows (PowerShell): |
| 32 | + ```sh |
| 33 | + .venv\Scripts\Activate |
| 34 | + ``` |
| 35 | +3. **Install project dependencies**: |
| 36 | + ```sh |
| 37 | + pip install -r requirements.txt |
| 38 | + ``` |
| 39 | +4. **Create a `.env` file** from `.env.sample`: |
| 40 | + ```sh |
| 41 | + cp .env.sample .env |
| 42 | + ``` |
| 43 | +5. **Start the server**: |
| 44 | + ```sh |
| 45 | + python main.py |
| 46 | + ``` |
24 | 47 |
|
25 | | -To set up the database, follow the following steps: |
| 48 | +--- |
26 | 49 |
|
27 | | -**Cloning** |
28 | | -- clone the repository using `git clone https://github.com/hngprojects/hng_boilerplate_python_fastapi_web` |
29 | | -- `cd` into the directory hng_boilerplate_python_fastapi_web |
30 | | -- switch branch using `git checkout backend` |
| 50 | +## **Database Setup** |
31 | 51 |
|
32 | | -**Environment Setup** |
33 | | -- run `pip install -r requrements.txt` to install dependencies |
34 | | -- create a `.env` file in the root directory and copy the content of `.env.sample` and update it accordingly |
| 52 | +### **Replacing Placeholders in Database Setup** |
35 | 53 |
|
36 | | -**Create your local database** |
37 | | -```bash |
38 | | -sudo -u root psql |
| 54 | +When setting up the database, you need to replace **placeholders** with your actual values. Below is a breakdown of where to replace them: |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## **Step 1: Create a Database User** |
| 59 | +```sql |
| 60 | +CREATE USER user WITH PASSWORD 'your_password'; |
39 | 61 | ``` |
| 62 | +🔹 **Replace:** |
| 63 | +- `user` → Your **preferred database username** (e.g., `fastapi_user`). |
| 64 | +- `'your_password'` → A **secure password** for the user (e.g., `'StrongP@ssw0rd'`). |
| 65 | + |
| 66 | +✅ **Example:** |
| 67 | +```sql |
| 68 | +CREATE USER fastapi_user WITH PASSWORD 'StrongP@ssw0rd'; |
| 69 | +``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## **Step 2: Create the Database** |
40 | 74 | ```sql |
41 | | -CREATE USER user WITH PASSWORD 'your desired password'; |
42 | 75 | CREATE DATABASE hng_fast_api; |
| 76 | +``` |
| 77 | +🔹 **Replace:** |
| 78 | +- `hng_fast_api` → Your **preferred database name** (e.g., `fastapi_db`). |
| 79 | + |
| 80 | +✅ **Example:** |
| 81 | +```sql |
| 82 | +CREATE DATABASE fastapi_db; |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## **Step 3: Grant Permissions** |
| 88 | +```sql |
43 | 89 | GRANT ALL PRIVILEGES ON DATABASE hng_fast_api TO user; |
44 | 90 | ``` |
| 91 | +🔹 **Replace:** |
| 92 | +- `hng_fast_api` → The **database name you used** in Step 2. |
| 93 | +- `user` → The **username you created** in Step 1. |
45 | 94 |
|
46 | | -**Starting the database** |
47 | | -after cloning the database, dont run |
48 | | -`alembic revision --autogenerate -m 'initial migration'` |
49 | | -but run |
50 | | -`alembic upgrade head` |
| 95 | +✅ **Example:** |
| 96 | +```sql |
| 97 | +GRANT ALL PRIVILEGES ON DATABASE fastapi_db TO fastapi_user; |
| 98 | +``` |
51 | 99 |
|
52 | | -if you make changes to any table locally, then run the below command. |
53 | | -```bash |
54 | | -alembic revision --autogenerate -m 'initial migration' |
55 | | -alembic upgrade head |
| 100 | +--- |
| 101 | + |
| 102 | +## **Step 4: Update `.env` File** |
| 103 | +Edit the `.env` file to match your setup. |
| 104 | + |
| 105 | +```env |
| 106 | +DATABASE_URL=postgresql://user:your_password@localhost/hng_fast_api |
56 | 107 | ``` |
| 108 | +🔹 **Replace:** |
| 109 | +- `user` → Your **database username**. |
| 110 | +- `your_password` → Your **database password**. |
| 111 | +- `hng_fast_api` → Your **database name**. |
57 | 112 |
|
58 | | -**create dummy data** |
59 | | -```bash |
60 | | -python3 seed.py |
| 113 | +✅ **Example:** |
| 114 | +```env |
| 115 | +DATABASE_URL=postgresql://fastapi_user:StrongP@ssw0rd@localhost/fastapi_db |
61 | 116 | ``` |
62 | 117 |
|
| 118 | +--- |
63 | 119 |
|
64 | | -**Adding tables and columns to models** |
| 120 | +## **Step 5: Verify Connection** |
| 121 | +After setting up the database, test the connection: |
65 | 122 |
|
66 | | -After creating new tables, or adding new models. Make sure to run alembic revision --autogenerate -m "Migration messge" |
| 123 | +```sh |
| 124 | +psql -U user -d hng_fast_api -h localhost |
| 125 | +``` |
| 126 | +🔹 **Replace:** |
| 127 | +- `user` → Your **database username**. |
| 128 | +- `hng_fast_api` → Your **database name**. |
67 | 129 |
|
68 | | -After creating new tables, or adding new models. Make sure you import the new model properly in th 'api/v1/models/__init__.py file |
| 130 | +✅ **Example:** |
| 131 | +```sh |
| 132 | +psql -U fastapi_user -d fastapi_db -h localhost |
| 133 | +``` |
69 | 134 |
|
70 | | -After importing it in the init file, you need not import it in the /alembic/env.py file anymore |
| 135 | +## **Step 6: Run database migrations** |
| 136 | + ```sh |
| 137 | + alembic upgrade head |
| 138 | + ``` |
| 139 | + _Do NOT run `alembic revision --autogenerate -m 'initial migration'` initially!_ |
71 | 140 |
|
| 141 | +## **Step 7: If making changes to database models, update migrations** |
| 142 | +```sh |
| 143 | + alembic revision --autogenerate -m 'your migration message' |
| 144 | + alembic upgrade head |
| 145 | + ``` |
| 146 | +## **Step 8: Seed dummy data** |
| 147 | + ```sh |
| 148 | + python3 seed.py |
| 149 | + ``` |
72 | 150 |
|
73 | | -**Adding new routes** |
| 151 | +--- |
74 | 152 |
|
75 | | -To add a new route, confirm if a file relating to that route is not already created. If it is add the route in that file using the already declared router |
| 153 | +## **Adding Tables and Columns** |
76 | 154 |
|
77 | | -If the there is no file relating to the route in the 'api/v1/routes/' directory create a new one following the naming convention |
| 155 | +1. **After creating new tables or modifying models**: |
| 156 | + - Run Alembic migrations: |
| 157 | + ```sh |
| 158 | + alembic revision --autogenerate -m "Migration message" |
| 159 | + alembic upgrade head |
| 160 | + ``` |
| 161 | + - Ensure you **import new models** into `api/v1/models/__init__.py`. |
| 162 | + - You do NOT need to manually import them in `alembic/env.py`. |
78 | 163 |
|
79 | | -After creating the new route file, declare the router and add the prefix as well as the tag |
| 164 | +--- |
80 | 165 |
|
81 | | -The prefix should not include the base prefix ('/api/v1') as it is already includedin the base `api_version_one` router |
| 166 | +## **Adding New Routes** |
82 | 167 |
|
83 | | -After creating the router, import it in the 'api/v1/routes/__init__.py' file and include the router in the `api_version_one` router using |
84 | | -```python |
85 | | -api_version_one.include_router(<router_name>) |
| 168 | +1. **Check if a related route file already exists** in `api/v1/routes/`. |
| 169 | + - If yes, add your route inside the existing file. |
| 170 | + - If no, create a new file following the naming convention. |
| 171 | +2. **Define the router** inside the new route file: |
| 172 | + - Include the prefix (without `/api/v1` since it's already handled). |
| 173 | +3. **Register the router in `api/v1/routes/__init__.py`**: |
| 174 | + ```python |
| 175 | + from .new_route import router as new_router |
| 176 | + api_version_one.include_router(new_router) |
| 177 | + ``` |
| 178 | +
|
| 179 | +--- |
| 180 | +
|
| 181 | +## **Running Tests with Pytest** |
| 182 | +
|
| 183 | +### **Install Pytest** |
| 184 | +Ensure `pytest` is installed in your virtual environment: |
| 185 | +```sh |
| 186 | +pip install pytest |
| 187 | +``` |
| 188 | +
|
| 189 | +### **Run all tests in the project** |
| 190 | +From the **project root directory**, run: |
| 191 | +```sh |
| 192 | +pytest |
| 193 | +``` |
| 194 | +This will automatically discover and execute all test files in the `tests/` directory. |
| 195 | +
|
| 196 | +### **Run tests in a specific directory** |
| 197 | +To run tests in a specific model directory (e.g., `tests/v1/user/`): |
| 198 | +```sh |
| 199 | +pytest tests/v1/user/ |
| 200 | +``` |
| 201 | +
|
| 202 | +### **Run a specific test file** |
| 203 | +To run tests from a specific test file (e.g., `test_signup.py` inside `tests/v1/auth/`): |
| 204 | +```sh |
| 205 | +pytest tests/v1/auth/test_signup.py |
| 206 | +``` |
| 207 | +
|
| 208 | +### **Run a specific test function** |
| 209 | +If you want to run a specific test inside a file, use: |
| 210 | +```sh |
| 211 | +pytest tests/v1/auth/test_signup.py::test_user_signup |
86 | 212 | ``` |
87 | 213 |
|
88 | | -## TEST THE ENDPOINT |
89 | | -- run the following code |
| 214 | +### **Run tests with detailed output** |
| 215 | +For verbose output, add the `-v` flag: |
| 216 | +```sh |
| 217 | +pytest -v |
90 | 218 | ``` |
91 | | -python -m unittest tests/v1/test_login.py |
92 | | -python -m unittest tests/v1/test_signup.py |
| 219 | +
|
| 220 | +### **Run tests and generate coverage report** |
| 221 | +To check test coverage, install `pytest-cov`: |
| 222 | +```sh |
| 223 | +pip install pytest-cov |
| 224 | +``` |
| 225 | +Then run: |
| 226 | +```sh |
| 227 | +pytest --cov=api |
93 | 228 | ``` |
94 | 229 |
|
95 | | -## Issues |
96 | | -if you encounter the following Error, when you run the code below |
| 230 | +--- |
97 | 231 |
|
98 | | -**alembic revision --autogenerate -m 'your migration message'** |
| 232 | +## **Common Migration Issues & Solutions** |
99 | 233 |
|
| 234 | +### **Error: "Target database is not up to date."** |
| 235 | +If you encounter this issue when running: |
| 236 | +```sh |
| 237 | +alembic revision --autogenerate -m 'your migration message' |
100 | 238 | ``` |
101 | | -INFO [alembic.runtime.migration] Context impl PostgresqlImpl. |
102 | | -INFO [alembic.runtime.migration] Will assume transactional DDL. |
103 | | -ERROR [alembic.util.messaging] Target database is not up to date. |
104 | | - FAILED: Target database is not up to date. |
| 239 | +#### **Solution**: |
| 240 | +Run the following command first: |
| 241 | +```sh |
| 242 | +alembic upgrade head |
105 | 243 | ``` |
| 244 | +Then retry: |
| 245 | +```sh |
| 246 | +alembic revision --autogenerate -m 'your migration message' |
| 247 | +``` |
| 248 | +
|
| 249 | +--- |
| 250 | +
|
| 251 | +## **Contribution Guidelines** |
106 | 252 |
|
107 | | -## Solutions |
108 | | -Run the following code below first to update the datebase |
109 | | -**alembic upgrade head** |
110 | | -then, run this again. |
111 | | -**alembic revision --autogenerate -m 'your migration message'** |
| 253 | +- **Test your endpoints and models** before pushing changes. |
| 254 | +- **Push Alembic migrations** if database models are modified. |
| 255 | +- Ensure your code **follows project standards** and **passes tests** before submitting a pull request. |
112 | 256 |
|
113 | | -## update |
114 | | -please make sure to test your endpoint or model before pushing. |
115 | | -push your alembic migrations. |
| 257 | +💯 Happy coding! |
0 commit comments