Skip to content

Commit e80e73b

Browse files
committed
Add integration tests for new API endpoints, update testing pattern
1 parent d3c66e6 commit e80e73b

File tree

8 files changed

+1081
-535
lines changed

8 files changed

+1081
-535
lines changed

server/express/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
"@types/express": "^4.17.21",
3232
"@types/jest": "^29.5.14",
3333
"@types/node": "^20.10.5",
34+
"@types/supertest": "^6.0.3",
3435
"@types/swagger-jsdoc": "^6.0.4",
3536
"@types/swagger-ui-express": "^4.1.8",
3637
"jest": "^29.7.0",
38+
"supertest": "^7.1.4",
3739
"ts-jest": "^29.1.1",
3840
"ts-node": "^10.9.2",
3941
"typescript": "^5.3.3"

server/express/src/app.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,10 @@ process.on("SIGTERM", () => {
161161
process.exit(0);
162162
});
163163

164-
// Start the server
165-
startServer();
164+
// Export the app for testing
165+
export { app };
166+
167+
// Only start the server if this file is run directly (not imported for testing)
168+
if (require.main === module) {
169+
startServer();
170+
}

server/express/src/utils/errorHandler.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ export function errorHandler(
3838
): void {
3939
// Log the error for debugging purposes
4040
// In production, we recommend using a logging service
41-
console.error("Error occurred:", {
42-
message: err.message,
43-
stack: err.stack,
44-
url: req.url,
45-
method: req.method,
46-
timestamp: new Date().toISOString(),
47-
});
41+
// Suppress error logging during tests to keep test output clean
42+
if (process.env.NODE_ENV !== "test") {
43+
console.error("Error occurred:", {
44+
message: err.message,
45+
stack: err.stack,
46+
url: req.url,
47+
method: req.method,
48+
timestamp: new Date().toISOString(),
49+
});
50+
}
4851

4952
// Determine the appropriate HTTP status code and error message
5053
const errorDetails = parseErrorDetails(err);

server/express/tests/integration/README.md

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,67 @@
11
# Integration Tests
22

3-
This directory contains integration tests for the Express MongoDB application. Unlike unit tests, these tests connect to
4-
a real MongoDB instance to verify end-to-end functionality.
3+
This directory contains integration tests for the Express MongoDB application. Unlike unit tests, these tests make actual HTTP requests to the Express app using `supertest` and connect to a real MongoDB instance to verify end-to-end functionality.
54

65
## Overview
76

8-
The integration tests are organized into two main categories:
7+
The integration tests are organized into three main categories:
98

109
1. **Movie CRUD Integration Tests** (`movie.integration.test.ts`)
11-
- Tests basic database operations (Create, Read, Update, Delete)
12-
- Verifies MongoDB driver functionality with a real database
13-
- Tests pagination, filtering, and sorting
10+
- Tests basic CRUD API endpoints using `supertest`
11+
- Makes actual HTTP requests to the Express app
12+
- Tests pagination, filtering, and sorting via API
1413
- Automatically skipped if `MONGODB_URI` is not set
1514

1615
2. **MongoDB Search Integration Tests** (`mongodbSearch.integration.test.ts`)
17-
- Tests MongoDB Search functionality
18-
- Works with both local MongoDB and Atlas instances
19-
- Tests search index creation, polling, and search queries
16+
- Tests the MongoDB Atlas Search API endpoint using `supertest`
17+
- Makes actual HTTP requests to test the `/api/movies/search` endpoint
18+
- Tests search by plot, directors, cast, pagination, and search operators
2019
- Automatically skipped if `ENABLE_SEARCH_TESTS` is not set
2120

22-
**Note:** Tests use `describeIntegration` and `describeSearch` wrappers (from `setup.ts`) that automatically skip entire
23-
test suites when the required environment variables are not set.
21+
3. **Advanced Endpoints Integration Tests** (`advancedEndpoints.integration.test.ts`)
22+
- Tests advanced API endpoints using `supertest`
23+
- Makes actual HTTP requests to test:
24+
- **Aggregation endpoints**: Movies with comments, statistics by year, directors with most movies
25+
- **Atlas Search endpoint**: Compound queries, phrase matching, fuzzy matching
26+
- **Vector Search endpoint**: Semantic similarity search using embeddings
27+
- Aggregation tests automatically run if `MONGODB_URI` is set
28+
- Atlas Search tests require `ENABLE_SEARCH_TESTS=true`
29+
- Vector Search tests require `VOYAGE_API_KEY` to be set
30+
31+
**Note:** Tests use `describeIntegration`, `describeSearch`, and `describeVectorSearch` wrappers (from `setup.ts` and test files) that automatically skip entire test suites when the required environment variables are not set.
32+
33+
## Testing Approach
34+
35+
These integration tests use **supertest** to make actual HTTP requests to the Express application, testing the complete request/response cycle including:
36+
- Routing
37+
- Request parsing
38+
- Controller logic
39+
- Database operations
40+
- Response formatting
41+
- Error handling
42+
43+
This approach ensures that the API endpoints work correctly from the client's perspective.
2444

2545
## Requirements
2646

27-
### Basic Integration Tests
47+
### Basic Integration Tests (CRUD and Aggregations)
2848

2949
- **MONGODB_URI** environment variable must be set
3050
- MongoDB instance must be accessible (can be local MongoDB or Atlas)
3151

32-
### Search Tests
52+
### Atlas Search Tests
3353

3454
- **MongoDB instance** with Search enabled (local MongoDB or Atlas)
3555
- **MONGODB_URI** environment variable
3656
- **ENABLE_SEARCH_TESTS=true** environment variable to enable tests
3757

58+
### Vector Search Tests
59+
60+
- **MONGODB_URI** environment variable must be set
61+
- **VOYAGE_API_KEY** environment variable must be set with a valid Voyage AI API key
62+
- MongoDB instance must have the `embedded_movies` collection with vector embeddings
63+
- Vector search index must be configured on the `embedded_movies` collection
64+
3865
## Running the Tests
3966

4067
### Run All Integration Tests
@@ -51,6 +78,9 @@ npx jest --config jest.integration.config.json tests/integration/movie.integrati
5178

5279
# Run only Search tests (requires Search-enabled MongoDB)
5380
npx jest --config jest.integration.config.json tests/integration/mongodbSearch.integration.test.ts
81+
82+
# Run only Advanced Endpoints tests (aggregations, search, vector search)
83+
npx jest --config jest.integration.config.json tests/integration/advancedEndpoints.integration.test.ts
5484
```
5585

5686
### Set Environment Variables
@@ -66,15 +96,27 @@ MONGODB_URI=mongodb+srv://username:[email protected]/sample_mflix?ret
6696
6797
# Optional: Enable Search tests (requires Search-enabled MongoDB)
6898
ENABLE_SEARCH_TESTS=true
99+
100+
# Optional: Enable Vector Search tests (requires Voyage AI API key)
101+
VOYAGE_API_KEY=your-voyage-ai-api-key
69102
```
70103

71104
## Test Structure
72105

73106
### Setup and Teardown
74107

108+
All integration tests follow an **idempotent pattern** to ensure they can be run multiple times without side effects:
109+
75110
- **Global Setup** (`beforeAll` in `setup.ts`): Connects to MongoDB once before all tests
76111
- **Global Teardown** (`afterAll` in `setup.ts`): Closes MongoDB connection after all tests
77-
- **Test Cleanup** (`afterEach` in test files): Removes test data after each test to ensure isolation
112+
- **Pre-Test Cleanup** (`beforeAll` in test files): Removes any orphaned test data from previous runs
113+
- **Post-Test Cleanup** (`afterEach` in test files): Removes test data created during each test
114+
115+
This ensures:
116+
- Tests can be run multiple times without conflicts
117+
- The dataset is left in an untouched state after test execution
118+
- Tests are isolated from each other
119+
- Failed test runs don't affect subsequent runs
78120

79121
## Troubleshooting
80122

@@ -94,6 +136,15 @@ If you see "⚠️ Skipping MongoDB Search tests - ENABLE_SEARCH_TESTS not set"
94136
2. Verify your MongoDB instance has Search enabled (available in both local MongoDB and Atlas)
95137
3. For local MongoDB, ensure you're running a version that supports Search
96138

139+
### Vector Search Tests are Skipped
140+
141+
If you see "⚠️ Vector Search tests skipped - VOYAGE_API_KEY not set":
142+
143+
1. Set `VOYAGE_API_KEY` environment variable with your Voyage AI API key
144+
2. Verify your MongoDB instance has the `embedded_movies` collection
145+
3. Ensure the vector search index is configured on the `embedded_movies` collection
146+
4. The `embedded_movies` collection should have documents with `plot_embedding_voyage_3_large` field
147+
97148
### Connection Errors
98149

99150
If tests fail with connection errors:
@@ -132,9 +183,10 @@ If tests timeout, you may need to:
132183

133184
| Aspect | Unit Tests | Integration Tests |
134185
|--------|-----------|-------------------|
186+
| HTTP Requests | Mocked with Jest | Real HTTP requests via supertest |
135187
| Database | Mocked with Jest | Real MongoDB instance |
136188
| Speed | Fast (milliseconds) | Slower (seconds) |
137189
| Dependencies | None | Requires MongoDB |
138190
| Isolation | Complete | Requires cleanup |
139191
| CI/CD | Always run | Conditional (requires MONGODB_URI) |
140-
| Purpose | Test business logic | Test database operations |
192+
| Purpose | Test business logic | Test end-to-end API functionality |

0 commit comments

Comments
 (0)