|
| 1 | +# FastAPI-Postgres CRUD Application |
| 2 | + |
| 3 | +A sample user data CRUD app to test Keploy integration capabilities using [FastAPI](https://fastapi.tiangolo.com/) and [PostgreSQL](https://www.postgresql.org/). <br> |
| 4 | +Make the following requests to the respective endpoints - |
| 5 | + |
| 6 | +1. `GET students/` - Get all students. |
| 7 | +2. `GET students/{id}` - Get a student by id. |
| 8 | +3. `POST students/` - Create a student. |
| 9 | +4. `PUT students/{id}` - Update a student by id. |
| 10 | +5. `DELETE students/{id}` - Delete a student by id. |
| 11 | + |
| 12 | +## Installation Setup |
| 13 | + |
| 14 | +```bash |
| 15 | +git clone https://github.com/keploy/samples-python.git && cd samples-python/fastapi-postgres |
| 16 | +pip3 install -r requirements.txt |
| 17 | +``` |
| 18 | + |
| 19 | +## Installation Keploy |
| 20 | + |
| 21 | +Keploy can be installed on Linux directly and on Windows with the help of WSL. Based on your system architecture, install the keploy latest binary release |
| 22 | + |
| 23 | +**1. AMD Architecture** |
| 24 | + |
| 25 | +```shell |
| 26 | +curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp |
| 27 | + |
| 28 | +sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy |
| 29 | +``` |
| 30 | + |
| 31 | +<details> |
| 32 | +<summary> 2. ARM Architecture </summary> |
| 33 | + |
| 34 | +```shell |
| 35 | +curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz -C /tmp |
| 36 | + |
| 37 | +sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy |
| 38 | +``` |
| 39 | + |
| 40 | +</details> |
| 41 | + |
| 42 | +### Starting the PostgreSQL Instance |
| 43 | + |
| 44 | +```bash |
| 45 | +# Start the application |
| 46 | +docker-compose up -d |
| 47 | +``` |
| 48 | + |
| 49 | +### Capture the Testcases |
| 50 | + |
| 51 | +This command will start the recording of API calls using ebpf:- |
| 52 | + |
| 53 | +```shell |
| 54 | +sudo -E keploy record -c "uvicorn application.main:app --reload" |
| 55 | +``` |
| 56 | + |
| 57 | +Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks. |
| 58 | + |
| 59 | +### Make a POST request |
| 60 | + |
| 61 | +```bash |
| 62 | +curl --location 'http://127.0.0.1:8000/students/' \ |
| 63 | +--header 'Content-Type: application/json' \ |
| 64 | +--data-raw '{ |
| 65 | + "name": "Eva White", |
| 66 | + "email": "evawhite@example.com", |
| 67 | + "password": "evawhite111" |
| 68 | + }' |
| 69 | +``` |
| 70 | + |
| 71 | +```bash |
| 72 | +curl --location 'http://127.0.0.1:8000/students/' \ |
| 73 | +--header 'Content-Type: application/json' \ |
| 74 | +--data-raw ' { |
| 75 | + "name": "John Doe", |
| 76 | + "email": "johndoe@example.com", |
| 77 | + "password": "johndoe123" |
| 78 | + }' |
| 79 | +``` |
| 80 | + |
| 81 | +### Make a GET request to get all the data |
| 82 | + |
| 83 | +```bash |
| 84 | +curl --location 'http://127.0.0.1:8000/students/' |
| 85 | +``` |
| 86 | + |
| 87 | +This will return all the data saved in the database. |
| 88 | + |
| 89 | +### Make a GET request to get a specific data |
| 90 | + |
| 91 | +```bash |
| 92 | +curl --location 'http://127.0.0.1:8000/students/1' |
| 93 | +``` |
| 94 | + |
| 95 | +### Make a PUT request to update a specific data |
| 96 | + |
| 97 | +```bash |
| 98 | +curl --location --request PUT 'http://127.0.0.1:8000/students/2' \ |
| 99 | +--header 'Content-Type: application/json' \ |
| 100 | +--data-raw ' { |
| 101 | + "name": "John Dow", |
| 102 | + "email": "doe.john@example.com", |
| 103 | + "password": "johndoe123", |
| 104 | + "stream": "Arts" |
| 105 | + }' |
| 106 | +``` |
| 107 | + |
| 108 | +### Make a DELETE request to delete a specific data |
| 109 | + |
| 110 | +```bash |
| 111 | +curl --location --request DELETE 'http://127.0.0.1:8000/students/1' |
| 112 | +``` |
| 113 | + |
| 114 | +Now all these API calls were captured as **editable** testcases and written to `keploy/tests` folder. The keploy directory would also have `mocks` file that contains all the outputs of postgres operations. |
| 115 | + |
| 116 | +## Run the Testcases |
| 117 | + |
| 118 | +Now let's run the application in test mode. |
| 119 | + |
| 120 | +```shell |
| 121 | +sudo -E keploy test -c "uvicorn application.main:app --reload" --delay 10 |
| 122 | +``` |
| 123 | + |
| 124 | +So, no need to setup fake database/apis like Postgres or write mocks for them. Keploy automatically mocks them and, **The application thinks it's talking to Postgres 😄** |
0 commit comments