-
-
Notifications
You must be signed in to change notification settings - Fork 281
Add Ruby + PostgreSQL quickstart guide #773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nsanjayboruds
wants to merge
4
commits into
keploy:main
Choose a base branch
from
Nsanjayboruds:add-ruby-postgresql-quickstart
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+318
β1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8e7ca4f
Add Ruby + PostgreSQL quickstart guide
Nsanjayboruds 4f2b408
Address Copilot review comments - improve Docker Compose workflow
Nsanjayboruds eadbf29
Fix Ruby quickstart - improve Docker workflow and clarity
Nsanjayboruds 7219218
Merge branch 'main' into add-ruby-postgresql-quickstart
Nsanjayboruds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
287 changes: 287 additions & 0 deletions
287
versioned_docs/version-4.0.0/quickstart/ruby-sinatra-postgres.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,287 @@ | ||
| --- | ||
| id: samples-ruby | ||
| title: Sample Books API | ||
| sidebar_label: Ruby + Postgres | ||
| description: The following sample app showcases how to use the Ruby (Sinatra) framework and the Keploy Platform. | ||
| tags: | ||
| - ruby | ||
| - quickstart | ||
| - samples | ||
| - examples | ||
| - tutorial | ||
| - sinatra | ||
| - postgresql | ||
| - ruby-framework | ||
| keyword: | ||
| - Sinatra Framework | ||
| - PostgreSQL | ||
| - Ruby | ||
| - API Test generator | ||
| - Auto case generation | ||
| --- | ||
|
|
||
| import InstallReminder from '@site/src/components/InstallReminder'; | ||
| import SectionDivider from '@site/src/components/SectionDivider'; | ||
|
|
||
| ## Introduction | ||
|
|
||
| πͺ Dive into the world of Books CRUD API and see how seamlessly Keploy integrates with [Ruby (Sinatra)](http://sinatrarb.com/) and [PostgreSQL](https://www.postgresql.org/). Buckle up, it's gonna be a fun ride! π’ | ||
|
|
||
| <InstallReminder /> | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| ### For Local Setup: | ||
| - Ruby 3.2 or higher | ||
| - PostgreSQL 15 or higher | ||
| - Bundler (`gem install bundler`) | ||
|
|
||
| ### For Docker Setup: | ||
| - Docker (20.10 or higher) | ||
| - Docker Compose (v2.0 or higher) | ||
|
|
||
| <SectionDivider /> | ||
|
|
||
| ## Using Docker Compose π³ | ||
|
|
||
| We will be using Docker compose to run the application as well as PostgreSQL on Docker container. | ||
|
|
||
| ### Clone the Application π§ͺ | ||
|
|
||
| ```bash | ||
| git clone https://github.com/Nsanjayboruds/keploy-ruby-postgresql-quickstart.git && cd keploy-ruby-postgresql-quickstart | ||
| ``` | ||
|
|
||
| ### Lights, Camera, Record! π₯ | ||
|
|
||
| Capture the test-cases- | ||
|
|
||
| ```bash | ||
| keploy record -c "docker compose up --build" --container-name "ruby-books-app" | ||
| ``` | ||
|
|
||
| This will: | ||
| - Start a PostgreSQL container | ||
| - Build and start the Ruby application container | ||
| - Initialize the database with sample data | ||
| - Expose the API on port 8000 | ||
|
|
||
| π₯**Make some API calls**. Postman, Hoppscotch or even curl - take your pick! | ||
|
|
||
| ### Generate Testcases | ||
|
|
||
| To generate testcases we just need to **make some API calls.** | ||
|
|
||
| #### 1. Get All Books | ||
|
|
||
| ```bash | ||
| curl http://localhost:8000/books | ||
| ``` | ||
|
|
||
| #### 2. Get a Specific Book | ||
|
|
||
| ```bash | ||
| curl http://localhost:8000/books/1 | ||
| ``` | ||
|
|
||
| #### 3. Create a New Book | ||
|
|
||
| ```bash | ||
| curl -X POST http://localhost:8000/books \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "title": "The Hobbit", | ||
| "author": "J.R.R. Tolkien", | ||
| "isbn": "9780547928227", | ||
| "published_year": 1937 | ||
| }' | ||
| ``` | ||
|
|
||
| #### 4. Update a Book | ||
|
|
||
| ```bash | ||
| curl -X PUT http://localhost:8000/books/1 \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "title": "The Great Gatsby (Updated)", | ||
| "author": "F. Scott Fitzgerald", | ||
| "isbn": "9780743273565", | ||
| "published_year": 1925 | ||
| }' | ||
| ``` | ||
|
|
||
| #### 5. Delete a Book | ||
|
|
||
| ```bash | ||
| curl -X DELETE http://localhost:8000/books/1 | ||
| ``` | ||
|
|
||
| And 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 the `tests` directory and `mocks.yml`. | ||
|
|
||
| ### Stop the Running Services | ||
|
|
||
| ```bash | ||
| docker compose down | ||
| ``` | ||
|
|
||
| Want to see if everything works as expected? | ||
|
|
||
| ### Run Tests π§ͺ | ||
|
|
||
| Time to put things to the test π§ͺ | ||
|
|
||
| ```bash | ||
| keploy test -c "docker compose up" --container-name "ruby-books-app" --delay 10 | ||
| ``` | ||
|
|
||
| After tests complete, stop the services: | ||
|
|
||
| ```bash | ||
| docker compose down | ||
| ``` | ||
|
|
||
| > 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.yml`, or fiddle with the request or response in `test-x.yml`. Run the tests again and see the magic unfold!β¨π©βπ»π¨βπ»β¨ | ||
|
|
||
| <SectionDivider /> | ||
|
|
||
| ## Running App Locally on Linux/WSL π§ | ||
|
|
||
| We'll be running our sample application locally on Linux/WSL. There are 2 ways you can run this application: | ||
|
|
||
| ### Clone the Application π§ͺ | ||
|
|
||
| ```bash | ||
| git clone https://github.com/Nsanjayboruds/keploy-ruby-postgresql-quickstart.git && cd keploy-ruby-postgresql-quickstart | ||
| ``` | ||
|
|
||
| ### Install Dependencies | ||
|
|
||
| ```bash | ||
| bundle install | ||
| ``` | ||
|
|
||
| ### Set up PostgreSQL Database | ||
|
|
||
| Create the database: | ||
|
|
||
| ```bash | ||
| createdb booksdb | ||
| ``` | ||
|
|
||
| Initialize the database with the schema: | ||
|
|
||
| ```bash | ||
| psql -d booksdb -f init.sql | ||
| ``` | ||
|
|
||
| ### Configure Environment Variables | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| # Edit .env if needed for your local PostgreSQL configuration | ||
| ``` | ||
|
|
||
| ### Start the Application with Keploy | ||
|
|
||
| If the app is already running, stop it before you start recording. | ||
|
|
||
| ```bash | ||
| keploy record -c "bundle exec ruby app.rb" | ||
| ``` | ||
|
|
||
| The API will be available at `http://localhost:8000` | ||
|
|
||
| ### Verify the Setup | ||
|
|
||
| ```bash | ||
| curl http://localhost:8000/health | ||
| ``` | ||
|
|
||
| Expected Response: | ||
| ```json | ||
| {"status":"healthy","service":"Ruby Books API"} | ||
| ``` | ||
|
|
||
| ### Lights, Camera, Record! π₯ | ||
|
|
||
| If you stopped the app after verification, restart recording: | ||
|
|
||
| ```bash | ||
| keploy record -c "bundle exec ruby app.rb" | ||
| ``` | ||
|
|
||
| π₯**Make some API calls**. Postman, Hoppscotch or even curl - take your pick! | ||
|
|
||
| ### Generate Testcases | ||
|
|
||
| To generate testcases we just need to **make some API calls.** | ||
|
|
||
| #### 1. Get All Books | ||
|
|
||
| ```bash | ||
| curl http://localhost:8000/books | ||
| ``` | ||
|
|
||
| #### 2. Get a Specific Book | ||
|
|
||
| ```bash | ||
| curl http://localhost:8000/books/1 | ||
| ``` | ||
|
|
||
| #### 3. Create a New Book | ||
|
|
||
| ```bash | ||
| curl -X POST http://localhost:8000/books \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "title": "The Hobbit", | ||
| "author": "J.R.R. Tolkien", | ||
| "isbn": "9780547928227", | ||
| "published_year": 1937 | ||
| }' | ||
| ``` | ||
|
|
||
| #### 4. Update a Book | ||
|
|
||
| ```bash | ||
| curl -X PUT http://localhost:8000/books/1 \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "title": "The Great Gatsby (Updated)", | ||
| "author": "F. Scott Fitzgerald", | ||
| "isbn": "9780743273565", | ||
| "published_year": 1925 | ||
| }' | ||
| ``` | ||
|
|
||
| #### 5. Delete a Book | ||
|
|
||
| ```bash | ||
| curl -X DELETE http://localhost:8000/books/1 | ||
| ``` | ||
|
|
||
| And 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 the `tests` directory and `mocks.yml`. | ||
|
|
||
| Want to see if everything works as expected? | ||
|
|
||
| ### Run Tests π§ͺ | ||
|
|
||
| Time to put things to the test π§ͺ | ||
|
|
||
| ```bash | ||
| keploy test -c "bundle exec ruby app.rb" --delay 10 | ||
| ``` | ||
|
|
||
| After tests complete, stop the app: | ||
|
|
||
| ```bash | ||
| Ctrl+C | ||
| ``` | ||
|
|
||
| > 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.yml`, or fiddle with the request or response in `test-x.yml`. Run the tests again and see the magic unfold!β¨π©βπ»π¨βπ»β¨ | ||
|
|
||
| Happy coding! β¨π©βπ»π¨βπ»β¨ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.