| id | samples-sinatra-postgres | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| title | Sample User CRUD App (Sinatra + Postgres) | ||||||||
| sidebar_label | Sinatra + Postgres | ||||||||
| description | A sample User CRUD application built with Ruby Sinatra and PostgreSQL to demonstrate how Keploy records API calls and database interactions automatically. | ||||||||
| tags |
|
||||||||
| keyword |
|
import ProductTier from '@site/src/components/ProductTier';
Get started with the User CRUD Application.
import Link from '@docusaurus/Link' import EnterpriseInstallReminder from '@site/src/components/EnterpriseInstallReminder';
git clone https://github.com/keploy/samples-ruby.git && cd samples-ruby/sinatra-postgresCapture the test cases:
keploy record -c "docker compose up" --container-name "sinatra-app" --buildDelay 50To generate testcases we need to make some API calls. Open a new terminal window and run the following commands:
- Health check:
curl http://localhost:4567/- Create a user:
curl -X POST http://localhost:4567/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'- Create another user:
curl -X POST http://localhost:4567/users \
-H "Content-Type: application/json" \
-d '{"name": "Jane Smith", "email": "jane@example.com"}'- Get all users:
curl http://localhost:4567/users- Get a specific user:
curl http://localhost:4567/users/1- Delete a user:
curl -X DELETE http://localhost:4567/users/2Once you are done, you can stop the recording in the first terminal (Ctrl+C). Keploy will save the test cases in the keploy directory.
keploy test -c "docker compose up" --containerName "sinatra-app" --delay 10You can also check the test summary from your CLI.
- Ruby 3.0+ installed
- PostgreSQL installed and running
- Keploy installed
- Start PostgreSQL Container:
Since we are running the app locally, we need a database. We can spin up a PostgreSQL container easily:
docker run -d \
--name postgres-local \
-p 5432:5432 \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=keploy_demo \
postgres:15- Install dependencies:
bundle install- Set up environment variables:
Note: We use 127.0.0.1 instead of localhost to ensure IPv4 connection.
export DATABASE_HOST=127.0.0.1
export DATABASE_PORT=5432
export DATABASE_NAME=keploy_demo
export DATABASE_USER=postgres
export DATABASE_PASSWORD=postgreskeploy record -c "bundle exec ruby app.rb"Then make API calls as shown above (in a separate terminal).
keploy test -c "bundle exec ruby app.rb" --delay 5You have successfully recorded and replayed API tests for a Ruby Sinatra application using Keploy.






