Skip to content

Latest commit

 

History

History
112 lines (81 loc) · 3.18 KB

File metadata and controls

112 lines (81 loc) · 3.18 KB

FastAPI-Postgres CRUD Application

A sample user data CRUD app to test Keploy integration capabilities using FastAPI and PostgreSQL.
Make the following requests to the respective endpoints -

  1. GET students/ - Get all students.
  2. GET students/{id} - Get a student by id.
  3. POST students/ - Create a student.
  4. PUT students/{id} - Update a student by id.
  5. DELETE students/{id} - Delete a student by id.

Installation Setup

git clone https://github.com/keploy/samples-python.git && cd samples-python/fastapi-postgres
pip3 install -r requirements.txt

Installation Keploy

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

 curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh
 keploy

Starting the PostgreSQL Instance

# Start the application
docker-compose up -d

Note: Update the SQLALCHEMY_DATABASE_URL in the database.py file if you want to use a keploy with docker.

Capture the Testcases

This command will start the recording of API calls using ebpf:-

sudo -E PATH=$PATH keploy record -c "uvicorn application.main:app --reload"

Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

Make a POST request

curl --location 'http://127.0.0.1:8000/students/' \
--header 'Content-Type: application/json' \
--data-raw '{
      "name": "Eva White",
      "email": "evawhite@example.com",
      "password": "evawhite111"
    }'
curl --location 'http://127.0.0.1:8000/students/' \
--header 'Content-Type: application/json' \
--data-raw '    {
      "name": "John Doe",
      "email": "johndoe@example.com",
      "password": "johndoe123"
    }'

Make a GET request to get all the data

curl --location 'http://127.0.0.1:8000/students/'

This will return all the data saved in the database.

Make a GET request to get a specific data

curl --location 'http://127.0.0.1:8000/students/1'

Make a PUT request to update a specific data

curl --location --request PUT 'http://127.0.0.1:8000/students/2' \
--header 'Content-Type: application/json' \
--data-raw '    {
        "name": "John Dow",
        "email": "doe.john@example.com",
        "password": "johndoe123",
        "stream": "Arts"
    }'

Make a DELETE request to delete a specific data

curl --location --request DELETE 'http://127.0.0.1:8000/students/1'

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.

Run the Testcases

Now let's run the application in test mode.

sudo -E PATH=$PATH keploy test -c "uvicorn application.main:app --reload" --delay 10

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 😄