Skip to content

Latest commit

 

History

History

README.md

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 coverage
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 -L https:///keploy.io/install.sh && source install.sh

Starting the PostgreSQL Instance

# Start the application
docker-compose up -d postgres

If we have setup our sample-app with docker, we need to update the container name to postgres on line 6, in application/database.py, from postgresql://postgres:postgres@localhost:5432/studentdb to postgresql://postgres:postgres@postgres:5432/studentdb.

Also, we need to update the container name to postgres on line 11, of application/main.py, from postgresql://postgres:postgres@localhost:5432/studentdb to postgresql://postgres:postgres@postgres:5432/studentdb.

Capture the Testcases

This command will start the recording of API calls :

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": "[email protected]",
      "password": "evawhite111"
    }'
curl --location 'http://127.0.0.1:8000/students/' \
--header 'Content-Type: application/json' \
--data-raw '    {
      "name": "John Doe",
      "email": "[email protected]",
      "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": "[email protected]",
        "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.

keploy testcase

Run the Testcases

Now let's run the application in test mode.

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

We will get output something like below -

keploy testcase

By making just 2 api call, we have generated a complete test suite for our application and acheived 72% coverage.

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 😄