Skip to content

Commit 13cca0f

Browse files
committed
Add health check endpoint to Ruby Couchbase ORM and Rails tutorials
- Introduced a health check endpoint (`GET /api/v1/health`) for monitoring application health and Couchbase connectivity. - Updated documentation with example responses for healthy and unhealthy states. - Enhanced test structure section to clarify integration and Swagger documentation tests. - Organized API tests by entity, including new hotel-related specs. - Improved clarity and consistency in the tutorial content.
1 parent b067b79 commit 13cca0f

File tree

2 files changed

+330
-34
lines changed

2 files changed

+330
-34
lines changed

tutorial/markdown/ruby/ruby-couchbase-orm/quickstart-ruby-couchbase-orm.md

Lines changed: 139 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,46 @@ The application will run on port 3000 of your local machine (http://localhost:30
157157

158158
![Swagger Documentation](swagger_documentation.png)
159159

160+
## Health Check Endpoint
161+
162+
The application provides a health check endpoint for monitoring the service and its dependencies:
163+
164+
**Endpoint:** `GET /api/v1/health`
165+
166+
**Response (200 OK) - All services healthy:**
167+
```json
168+
{
169+
"status": "healthy",
170+
"timestamp": "2025-12-02T10:00:00Z",
171+
"services": {
172+
"couchbase": {
173+
"status": "up",
174+
"message": "Connected to Couchbase bucket: travel-sample"
175+
}
176+
}
177+
}
178+
```
179+
180+
**Response (503 Service Unavailable) - Service unhealthy:**
181+
```json
182+
{
183+
"status": "unhealthy",
184+
"timestamp": "2025-12-02T10:00:00Z",
185+
"services": {
186+
"couchbase": {
187+
"status": "down",
188+
"message": "Connection error details"
189+
}
190+
}
191+
}
192+
```
193+
194+
**Usage:**
195+
- Monitor application health in production environments
196+
- Use for Kubernetes/Docker container readiness and liveness probes
197+
- Verify Couchbase connectivity before running tests
198+
- Integrate with monitoring and alerting systems
199+
160200
## Data Model
161201

162202
For this tutorial, we use three collections, `airport`, `airline` and `route` that contain sample airports, airlines and airline routes respectively. The route collection connects the airports and airlines as seen in the figure below. We use these connections in the quickstart to generate airports that are directly connected and airlines connecting to a destination airport. Note that these are just examples to highlight how you can use SQL++ queries to join the collections.
@@ -177,6 +217,7 @@ To begin this tutorial, clone the repo and open it up in the IDE of your choice.
177217
│ │ │ └── v1
178218
│ │ │ ├── airlines_controller.rb
179219
│ │ │ ├── airports_controller.rb
220+
│ │ │ ├── health_controller.rb
180221
│ │ │ └── routes_controller.rb
181222
│ │ ├── application_controller.rb
182223
│ │ └── concerns
@@ -209,11 +250,15 @@ To begin this tutorial, clone the repo and open it up in the IDE of your choice.
209250
├── spec
210251
│ ├── rails_helper.rb
211252
│ ├── requests
212-
│ │ └── api
213-
│ │ └── v1
214-
│ │ ├── airlines_spec.rb
215-
│ │ ├── airports_spec.rb
216-
│ │ └── routes_spec.rb
253+
│ │ ├── api
254+
│ │ │ └── v1
255+
│ │ │ ├── airlines_spec.rb
256+
│ │ │ ├── airports_spec.rb
257+
│ │ │ └── routes_spec.rb
258+
│ │ └── swagger
259+
│ │ ├── airlines_spec.rb
260+
│ │ ├── airports_spec.rb
261+
│ │ └── routes_spec.rb
217262
│ ├── spec_helper.rb
218263
│ └── swagger_helper.rb
219264
├── swagger
@@ -224,14 +269,13 @@ To begin this tutorial, clone the repo and open it up in the IDE of your choice.
224269
│ │ └── application_cable
225270
│ │ └── connection_test.rb
226271
│ ├── controllers
227-
│ ├── integration
228-
│ │ ├── airlines_spec.rb
229-
│ │ ├── airports_spec.rb
230-
│ │ └── routes_spec.rb
231272
│ ├── models
232273
```
233274
234-
We have separated out the API code into separate files by the entity (collection) in the `api` folder. The tests are similarly separated out by entity in the `tests` folder.
275+
We have separated out the API code into separate files by the entity (collection) in the `api` folder. The application uses two types of tests:
276+
- **Integration tests** in `spec/requests/api/v1/` verify actual API functionality
277+
- **Swagger documentation specs** in `spec/requests/swagger/` generate OpenAPI documentation
278+
- The generated `swagger.yaml` file appears in `swagger/v1/`
235279
236280
In `routes.rb`, we define the routes for the application including the API routes.
237281
@@ -580,16 +624,81 @@ rescue StandardError => e
580624
end
581625
```
582626

627+
## Test Structure
628+
629+
The application uses two types of tests with clear separation of concerns:
630+
631+
### Integration Tests (`spec/requests/api/v1/`)
632+
633+
Integration tests verify the actual functionality of the API endpoints:
634+
635+
- **Purpose:** Verify actual API functionality with real database operations
636+
- **Coverage:** Full CRUD operations, error cases, input validation
637+
- **Database:** Uses real Couchbase travel-sample data
638+
- **Cleanup:** Tests clean up created data using `ensure` blocks
639+
- **Run:** `bundle exec rspec spec/requests/api/v1/`
640+
641+
Example integration test structure:
642+
```ruby
643+
describe 'POST /api/v1/airlines/{id}' do
644+
it 'creates an airline' do
645+
post "/api/v1/airlines/airline_test", params: { airline: airline_params }
646+
647+
expect(response).to have_http_status(:created)
648+
expect(JSON.parse(response.body)).to include(airline_params)
649+
ensure
650+
# Cleanup: delete the created airline
651+
delete "/api/v1/airlines/airline_test"
652+
end
653+
end
654+
```
655+
656+
### Swagger Documentation Tests (`spec/requests/swagger/`)
657+
658+
Swagger specs generate OpenAPI documentation without performing actual database operations:
659+
660+
- **Purpose:** Generate OpenAPI/Swagger documentation for the API
661+
- **Coverage:** API contract definition and request/response schemas
662+
- **Database:** Uses existing travel-sample IDs, no data mutations
663+
- **Speed:** Fast execution with no setup/teardown overhead
664+
- **Run:** `bundle exec rake rswag:specs:swaggerize`
665+
666+
Example swagger spec structure:
667+
```ruby
668+
response '201', 'airline created' do
669+
let(:id) { 'airline_new_123' }
670+
let(:airline) { { name: 'Test Airline', iata: 'TA', icao: 'TST', callsign: 'TEST', country: 'US' } }
671+
672+
run_test! do |response|
673+
# Documentation-only - actual testing in spec/requests/api/v1/airlines_spec.rb
674+
end
675+
end
676+
```
677+
678+
**Benefits of this separation:**
679+
- Fast swagger documentation generation
680+
- Comprehensive integration testing without impacting documentation
681+
- Clear distinction between API contracts and functional testing
682+
- Easier maintenance with focused test purposes
683+
583684
## Running Tests
584685

585-
We have defined integration tests using [RSpec](https://rspec.info/) for all the API end points. The integration tests use the same database configuration as the application. For the tests, we perform the operation using the API and confirm the results by checking the documents in the database. For example, to check the creation of the document by the API, we would call the API to create the document and then read the same document directly from the database using the CouchbaseClient and compare them. After the tests, the documents are cleaned up.
686+
We have defined integration tests using [RSpec](https://rspec.info/) for all the API end points. The integration tests use the same database configuration as the application. For the tests, we perform the operation using the API and confirm the results by checking the documents in the database. For example, to check the creation of the document by the API, we would call the API to create the document and then read the same document directly from the database and compare them. After the tests, the documents are cleaned up.
586687

587-
To run all integration tests, use the following command:
688+
To run the integration tests:
588689

589690
```bash
590-
bundle exec rspec test/integration/
691+
bundle exec rspec spec/requests/api/v1/
591692
```
592693

694+
To generate the Swagger/OpenAPI documentation:
695+
696+
```bash
697+
bundle exec rake rswag:specs:swaggerize
698+
```
699+
700+
**Note:** The integration tests verify actual API functionality, while the swagger specs are documentation-only and generate the OpenAPI specification file.
701+
593702
## Appendix
594703

595704
### Extending API by Adding New Entity
@@ -608,11 +717,23 @@ If you would like to add another entity to the APIs, follow these steps:
608717
resources :hotels
609718
```
610719

611-
5. **Add integration tests**: Create a new test file in the `test/integration/` folder (e.g., `test/integration/hotels_spec.rb`) to test your API endpoints similar to `airlines_spec.rb`.
612-
613-
6. **Add Swagger documentation**: Create a new spec file in the `spec/requests/api/v1/` folder (e.g., `spec/requests/api/v1/hotels_spec.rb`) for Swagger documentation similar to the airlines spec.
614-
615-
7. **Generate Swagger docs**: Run the command `rake rswag:specs:swaggerize` to generate the Swagger documentation.
720+
5. **Add integration tests**: Create a new test file in the `spec/requests/api/v1/` folder (e.g., `spec/requests/api/v1/hotels_spec.rb`) with comprehensive tests for all CRUD operations. These tests should verify actual functionality by performing database operations and asserting results.
721+
722+
6. **Add Swagger documentation**: Create a documentation-only spec file in the `spec/requests/swagger/` folder (e.g., `spec/requests/swagger/hotels_spec.rb`). This should focus on API contract definition without database operations:
723+
- Use `run_test! do |response|` blocks with comments referencing the integration tests
724+
- Use existing travel-sample data IDs for GET operations
725+
- Use placeholder IDs for create/update/delete operations
726+
- Example:
727+
```ruby
728+
response '200', 'hotel found' do
729+
let(:id) { 'hotel_123' }
730+
run_test! do |response|
731+
# Documentation-only - actual testing in spec/requests/api/v1/hotels_spec.rb
732+
end
733+
end
734+
```
735+
736+
7. **Generate Swagger docs**: Run the command `bundle exec rake rswag:specs:swaggerize` to generate the Swagger documentation.
616737

617738
Your new entity is now ready to be used in the API.
618739

0 commit comments

Comments
 (0)