You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
+
160
200
## Data Model
161
201
162
202
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.
177
217
│ │ │ └── v1
178
218
│ │ │ ├── airlines_controller.rb
179
219
│ │ │ ├── airports_controller.rb
220
+
│ │ │ ├── health_controller.rb
180
221
│ │ │ └── routes_controller.rb
181
222
│ │ ├── application_controller.rb
182
223
│ │ └── concerns
@@ -209,11 +250,15 @@ To begin this tutorial, clone the repo and open it up in the IDE of your choice.
209
250
├── spec
210
251
│ ├── rails_helper.rb
211
252
│ ├── 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
217
262
│ ├── spec_helper.rb
218
263
│ └── swagger_helper.rb
219
264
├── swagger
@@ -224,14 +269,13 @@ To begin this tutorial, clone the repo and open it up in the IDE of your choice.
224
269
│ │ └── application_cable
225
270
│ │ └── connection_test.rb
226
271
│ ├── controllers
227
-
│ ├── integration
228
-
│ │ ├── airlines_spec.rb
229
-
│ │ ├── airports_spec.rb
230
-
│ │ └── routes_spec.rb
231
272
│ ├── models
232
273
```
233
274
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/`
235
279
236
280
In `routes.rb`, we define the routes for the application including the API routes.
237
281
@@ -580,16 +624,81 @@ rescue StandardError => e
580
624
end
581
625
```
582
626
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
# 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
+
583
684
## Running Tests
584
685
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.
586
687
587
-
To run all integration tests, use the following command:
688
+
To run the integration tests:
588
689
589
690
```bash
590
-
bundle exec rspec test/integration/
691
+
bundle exec rspec spec/requests/api/v1/
591
692
```
592
693
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
+
593
702
## Appendix
594
703
595
704
### Extending API by Adding New Entity
@@ -608,11 +717,23 @@ If you would like to add another entity to the APIs, follow these steps:
608
717
resources :hotels
609
718
```
610
719
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. **GenerateSwagger docs**: Run the command `bundle exec rake rswag:specs:swaggerize` to generate the Swagger documentation.
616
737
617
738
Yournew entity is now ready to be used in the API.
0 commit comments