Skip to main content

Sample Student Data CRUD App​

View as Markdown

Using Docker Compose πŸ³β€‹

FreeTeamsScaleEnterprise

πŸͺ„ Dive into the world of User CRUD Apps and see how seamlessly Keploy integrated with FastAPI and PostgreSQL. Buckle up, it's gonna be a fun ride! 🎒

Don’t have Keploy installed yet?

Before running this sample, make sure Keploy is installed on your system.

πŸ‘‰ Go to Installation Guide

Clone the sample Student Data CRUD app πŸ§ͺ​

git clone https://github.com/keploy/samples-python.git && cd samples-python/fastapi-postgres

Point the app at the Postgres container πŸ”Œβ€‹

The app runs in a container, so it must reach Postgres by its compose service name instead of localhost. In both application/main.py and application/database.py, change @localhost:5432 to @postgres:5432 in SQLALCHEMY_DATABASE_URL.

Lights, Camera, Record! πŸŽ₯​

The docker-compose.yml attaches the services to an external keploy-network, so create it once:

docker network create keploy-network

Capture the test-cases-

keploy record -c "docker compose up" --container-name "fastapi-app" --build-delay 50

πŸ”₯Make some API calls. Postman, Hoppscotch or even curl - take your pick!

Let's make URLs short and sweet:

Generate testcases​

To generate testcases we just need to make some API calls.

1. 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"
}'

2. Make a GET request

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

3. Make a PUT request

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

4. Make a GET request

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

5. Make a DELETE request

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

Give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the Keploy directory and you'll discover your handiwork in keploy/test-set-0/tests/ (one file per call, e.g. post-students-1.yaml) and keploy/test-set-0/mocks.yaml.

version: api.keploy.io/v1beta1
kind: Http
name: get-students-1
spec:
metadata: {}
req:
method: GET
proto_major: 1
proto_minor: 1
url: http://127.0.0.1:8000/students/
header:
Accept: "*/*"
Host: 127.0.0.1:8000
User-Agent: curl/8.7.1
body: ""
timestamp: 2026-11-16T12:37:17.039514963Z
resp:
status_code: 200
header:
Content-Length: "109"
Content-Type: application/json
Date: Mon, 16 Nov 2026 12:37:16 GMT
Server: uvicorn
body: '[{"name":"Eva White","email":"evawhite@example.com","password":"sfwbxijuf222","stream":"Mathematics","id":1}]'
status_message: OK
proto_major: 0
proto_minor: 0
timestamp: 2026-11-16T12:37:17.044762963Z
objects: []
assertions:
noise:
header.Date: []
created: 1794832637
app_port: 8000
curl: |
curl --request GET \
--url http://127.0.0.1:8000/students/ \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8000' \
--header 'User-Agent: curl/8.7.1' \

This is how the generated mocks.yaml would look like:-

version: api.keploy.io/v1beta1
kind: PostgresV3
name: mock-28
spec:
metadata:
bindEncoding: wire
class: APP
connID: "2"
destAddr: 172.21.0.2:5432
lifetime: perTest
type: mocks
postgresV3:
type: query
query:
class: APP
lifetime: perTest
sqlAstHash: sha256:c7ada4e1d75ca3577e3832dabf1d290a894d7a9b47164ad99cc809a9f0384b30
sqlNormalized: "SELECT students.\"ID\" AS \"students_ID\", students.\"Name\" AS \"students_Name\", students.\"Email\" AS \"students_Email\", students.\"Hashed Password\" AS \"students_Hashed Password\", students.\"Subject Stream\" AS \"students_Subject Stream\" \nFROM students \n LIMIT $2 OFFSET $1"
invocationId: sha256:c7ada4e1d75ca3577e3832dabf1d290a894d7a9b47164ad99cc809a9f0384b30:2:2026-11-16T12:37:17.042651505Z:24
precedingTxState: in_tx
bindValues:
- !!binary MTAw
- !!binary MA==
bindFormats:
- 0
- 0
response:
rowDescription:
- name: students_ID
tableOid: 16386
colAttrNum: 1
typeOid: 23
typeSize: 4
typeMod: -1
- name: students_Name
tableOid: 16386
colAttrNum: 2
typeOid: 1043
typeSize: -1
typeMod: -1
- name: students_Email
tableOid: 16386
colAttrNum: 3
typeOid: 1043
typeSize: -1
typeMod: -1
- name: students_Hashed Password
tableOid: 16386
colAttrNum: 4
typeOid: 1043
typeSize: -1
typeMod: -1
- name: students_Subject Stream
tableOid: 16386
colAttrNum: 5
typeOid: 1043
typeSize: -1
typeMod: -1
rows:
- - 1
- Eva White
- evawhite@example.com
- sfwbxijuf222
- Mathematics
commandComplete: SELECT 1
sideEffects: {}
reqTimestampMock: 2026-11-16T12:37:17.042651505Z
resTimestampMock: 2026-11-16T12:37:17.043078546Z
connectionId: "2"

Want to see if everything works as expected?

Run Tests​

Time to put things to the test πŸ§ͺ

keploy test -c "docker compose up" --container-name "fastapi-app" --build-delay 50  --delay 10

The --delay flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking.

Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the mocks.yaml, or fiddle with the request or response in the files under tests/. Run the tests again and see the magic unfold!βœ¨πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»βœ¨

Wrapping it up πŸŽ‰β€‹

Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.πŸ˜ŠπŸš€

Happy coding! βœ¨πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»βœ¨

Running App Locally on Linux/WSL πŸ§β€‹

FreeTeamsScaleEnterprise

We'll be running our sample application right on Linux, but just to make things a tad more thrilling, we'll have the database (PostgreSQL) chill on Docker. Ready? Let's get the party started!πŸŽ‰

This section runs on Linux or WSL. On macOS, or on Windows without WSL, use the Docker Compose steps instead β€” natively there, Keploy captures PostgreSQL calls only as raw bytes, which usually don't replay.

Don’t have Keploy installed yet?

Before running this sample, make sure Keploy is installed on your system.

πŸ‘‰ Go to Installation Guide

Clone the sample Student Data CRUD app πŸ§ͺ​

git clone https://github.com/keploy/samples-python.git && cd samples-python/fastapi-postgres

Start the Postgres instance πŸ˜β€‹

The app connects to localhost:5432/studentdb, so start the database first. The compose file attaches it to an external keploy-network, so create that once:

docker network create keploy-network
docker compose up -d postgres

Install the dependencies πŸ“¦β€‹

This section runs the app on the host, so localhost is the right database host. If you changed @localhost:5432 to @postgres:5432 in application/main.py and application/database.py for the Docker Compose steps, change it back. Then install the dependencies, which include uvicorn. Use Python 3.11, because newer versions such as 3.13 can't build some of the pinned packages:

python3.11 -m venv venv && source venv/bin/activate
pip install -r requirements.txt

Lights, Camera, Record! πŸŽ₯​

Capture the test-cases-

keploy record -c "uvicorn application.main:app --reload"

Keep an eye out for the -c flag! It's the command charm to run the app.

Alright, magician! With the app alive and kicking, let's weave some test cases. The spell? Making some API calls! Postman, Hoppscotch, or the classic curl - pick your wand.

Generate testcases​

To generate testcases we just need to make some API calls.

1. 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"
}'

2. Make a GET request

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

3. Make a PUT request

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

4. Make a GET request

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

5. Make a DELETE request

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

Give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the Keploy directory and you'll discover your handiwork in keploy/test-set-0/tests/ (one file per call, e.g. post-students-1.yaml) and keploy/test-set-0/mocks.yaml.

version: api.keploy.io/v1beta1
kind: Http
name: get-students-1
spec:
metadata: {}
req:
method: GET
proto_major: 1
proto_minor: 1
url: http://127.0.0.1:8000/students/
header:
Accept: "*/*"
Host: 127.0.0.1:8000
User-Agent: curl/8.7.1
body: ""
timestamp: 2026-11-16T12:37:17.039514963Z
resp:
status_code: 200
header:
Content-Length: "109"
Content-Type: application/json
Date: Mon, 16 Nov 2026 12:37:16 GMT
Server: uvicorn
body: '[{"name":"Eva White","email":"evawhite@example.com","password":"sfwbxijuf222","stream":"Mathematics","id":1}]'
status_message: OK
proto_major: 0
proto_minor: 0
timestamp: 2026-11-16T12:37:17.044762963Z
objects: []
assertions:
noise:
header.Date: []
created: 1794832637
app_port: 8000
curl: |
curl --request GET \
--url http://127.0.0.1:8000/students/ \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8000' \
--header 'User-Agent: curl/8.7.1' \

This is how the generated mocks.yaml would look like:-

version: api.keploy.io/v1beta1
kind: PostgresV3
name: mock-28
spec:
metadata:
bindEncoding: wire
class: APP
connID: "2"
destAddr: 172.21.0.2:5432
lifetime: perTest
type: mocks
postgresV3:
type: query
query:
class: APP
lifetime: perTest
sqlAstHash: sha256:c7ada4e1d75ca3577e3832dabf1d290a894d7a9b47164ad99cc809a9f0384b30
sqlNormalized: "SELECT students.\"ID\" AS \"students_ID\", students.\"Name\" AS \"students_Name\", students.\"Email\" AS \"students_Email\", students.\"Hashed Password\" AS \"students_Hashed Password\", students.\"Subject Stream\" AS \"students_Subject Stream\" \nFROM students \n LIMIT $2 OFFSET $1"
invocationId: sha256:c7ada4e1d75ca3577e3832dabf1d290a894d7a9b47164ad99cc809a9f0384b30:2:2026-11-16T12:37:17.042651505Z:24
precedingTxState: in_tx
bindValues:
- !!binary MTAw
- !!binary MA==
bindFormats:
- 0
- 0
response:
rowDescription:
- name: students_ID
tableOid: 16386
colAttrNum: 1
typeOid: 23
typeSize: 4
typeMod: -1
- name: students_Name
tableOid: 16386
colAttrNum: 2
typeOid: 1043
typeSize: -1
typeMod: -1
- name: students_Email
tableOid: 16386
colAttrNum: 3
typeOid: 1043
typeSize: -1
typeMod: -1
- name: students_Hashed Password
tableOid: 16386
colAttrNum: 4
typeOid: 1043
typeSize: -1
typeMod: -1
- name: students_Subject Stream
tableOid: 16386
colAttrNum: 5
typeOid: 1043
typeSize: -1
typeMod: -1
rows:
- - 1
- Eva White
- evawhite@example.com
- sfwbxijuf222
- Mathematics
commandComplete: SELECT 1
sideEffects: {}
reqTimestampMock: 2026-11-16T12:37:17.042651505Z
resTimestampMock: 2026-11-16T12:37:17.043078546Z
connectionId: "2"

Want to see if everything works as expected?

Run Tests​

Time to put things to the test πŸ§ͺ

keploy test -c "uvicorn application.main:app --reload" --delay 10

The --delay flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking.

Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the mocks.yaml, or fiddle with the request or response in the files under tests/. Run the tests again and see the magic unfold!βœ¨πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»βœ¨

Wrapping it up πŸŽ‰β€‹

Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible. πŸ˜ŠπŸš€