Skip to content

Latest commit

 

History

History
179 lines (120 loc) · 3.76 KB

File metadata and controls

179 lines (120 loc) · 3.76 KB
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
sinatra
ruby
quickstart
samples
examples
tutorial
ruby-framework
postgresql
keyword
Sinatra Framework
Ruby
PostgreSQL
API Test generator
Auto case generation

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';

Get Started

Clone the application

git clone https://github.com/keploy/samples-ruby.git && cd samples-ruby/sinatra-postgres

Capture the test cases

Capture the test cases:

keploy record -c "docker compose up" --container-name "sinatra-app" --buildDelay 50

Keploy Record Sinatra

Generate testcases

To generate testcases we need to make some API calls. Open a new terminal window and run the following commands:

  1. Health check:
curl http://localhost:4567/
  1. Create a user:
curl -X POST http://localhost:4567/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'
  1. Create another user:
curl -X POST http://localhost:4567/users \
-H "Content-Type: application/json" \
-d '{"name": "Jane Smith", "email": "jane@example.com"}'
  1. Get all users:
curl http://localhost:4567/users
  1. Get a specific user:
curl http://localhost:4567/users/1
  1. Delete a user:
curl -X DELETE http://localhost:4567/users/2

Generate Testcases Sinatra

Once you are done, you can stop the recording in the first terminal (Ctrl+C). Keploy will save the test cases in the keploy directory.

Run the testcases

keploy test -c "docker compose up" --containerName "sinatra-app" --delay 10

Keploy Test Sinatra

You can also check the test summary from your CLI.

Running App Locally on Linux/WSL

Prerequisites

  • Ruby 3.0+ installed
  • PostgreSQL installed and running
  • Keploy installed

Setup

  1. 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
  1. Install dependencies:
bundle install

Bundle Install Sinatra

  1. 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=postgres

Record Test Cases

keploy record -c "bundle exec ruby app.rb"

Keploy Record Local

Then make API calls as shown above (in a separate terminal).

Generate Testcases Local

Run Tests

keploy test -c "bundle exec ruby app.rb" --delay 5

Keploy Test Local

Conclusion

You have successfully recorded and replayed API tests for a Ruby Sinatra application using Keploy.