| 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';
🪄 Dive into the world of User CRUD Apps and see how seamlessly Keploy integrates with Sinatra and PostgreSQL. Buckle up, it's gonna be a fun ride! 🎢
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-postgresWe will be using Docker Compose to run the application as well as PostgreSQL in Docker containers.
Capture the test cases:
keploy record -c "docker compose up" --container-name "sinatra-app" --buildDelay 50🔥 Make some API calls. Postman, Hoppscotch, or even curl - take your pick!
To generate testcases we just need to make some API calls.
- 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/2And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up test cases with mocks! Explore the keploy directory and you'll discover your handiwork in tests directory and mocks.yml.
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
- Install dependencies:
bundle install- Set up environment variables:
export DATABASE_HOST=localhost
export DATABASE_PORT=5432
export DATABASE_NAME=keploy_demo
export DATABASE_USER=postgres
export DATABASE_PASSWORD=postgres- Create the database:
createdb keploy_demokeploy record -c "bundle exec ruby app.rb"Then make API calls as shown above.
keploy test -c "bundle exec ruby app.rb" --delay 5Congrats on the journey so far! You've seen Keploy's power, flexed your Ruby 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. 😊🚀