Skip to content

Commit dae45a0

Browse files
chore: create new folder structure with placeholders
1 parent 831f067 commit dae45a0

File tree

12 files changed

+104
-0
lines changed

12 files changed

+104
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, expect, it, jest, beforeEach, afterEach } from '@jest/globals';
2+
import mongoose from 'mongoose';
3+
import { connectDB } from '../../index';
4+
5+
// Mock mongoose
6+
jest.mock('mongoose');
7+
const mockedMongoose = mongoose as jest.Mocked<typeof mongoose>;
8+
9+
// Mock console methods
10+
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
11+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
12+
const processExitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {
13+
throw new Error('process.exit called');
14+
});
15+
16+
describe('Database Connection', () => {
17+
beforeEach(() => {
18+
jest.clearAllMocks();
19+
process.env.MONGO_URI = 'mongodb+srv://test:test@test.mongodb.net/test?retryWrites=false&ssl=true';
20+
});
21+
22+
afterEach(() => {
23+
consoleLogSpy.mockClear();
24+
consoleErrorSpy.mockClear();
25+
processExitSpy.mockClear();
26+
});
27+
28+
it('should connect to MongoDB successfully', async () => {
29+
mockedMongoose.connect.mockResolvedValueOnce(mongoose);
30+
31+
await connectDB();
32+
33+
expect(mockedMongoose.connect).toHaveBeenCalledTimes(1);
34+
expect(mockedMongoose.connect).toHaveBeenCalledWith(
35+
process.env.MONGO_URI,
36+
{
37+
retryWrites: false,
38+
ssl: true
39+
}
40+
);
41+
expect(consoleLogSpy).toHaveBeenCalledWith('Connected to Azure Cosmos DB (MongoDB API)');
42+
});
43+
44+
it('should handle connection failure and exit process', async () => {
45+
const mockError = new Error('Connection failed');
46+
mockedMongoose.connect.mockRejectedValueOnce(mockError);
47+
48+
await expect(connectDB()).rejects.toThrow('process.exit called');
49+
50+
expect(mockedMongoose.connect).toHaveBeenCalledTimes(1);
51+
expect(consoleErrorSpy).toHaveBeenCalledWith('MongoDB connection failed:', mockError);
52+
expect(processExitSpy).toHaveBeenCalledWith(1);
53+
});
54+
55+
it('should use environment variable for connection string', async () => {
56+
const testUri = 'mongodb+srv://custom:uri@test.mongodb.net/custom?retryWrites=false&ssl=true';
57+
process.env.MONGO_URI = testUri;
58+
mockedMongoose.connect.mockResolvedValueOnce(mongoose);
59+
60+
await connectDB();
61+
62+
expect(mockedMongoose.connect).toHaveBeenCalledWith(testUri, {
63+
retryWrites: false,
64+
ssl: true
65+
});
66+
});
67+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import mongoose from 'mongoose';
2+
3+
const connectDB = async (): Promise<void> => {
4+
try {
5+
await mongoose.connect(process.env.MONGO_URI as string, {
6+
retryWrites: false,
7+
ssl: true
8+
});
9+
console.log('Connected to Azure Cosmos DB (MongoDB API)');
10+
} catch (error) {
11+
console.error('MongoDB connection failed:', error);
12+
process.exit(1);
13+
}
14+
};
15+
16+
export default connectDB;

server/src/database/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Connection setup
2+
export { default as connectDB } from './connection/mongoose';
3+
4+
// Type exports
5+
// export * from './types/user.type';
6+
// export * from './types/survey.type';
7+
// export * from './types/seed.type';
8+
9+
// Repository exports
10+
// export * from './repositories/user.repository';
11+
// export * from './repositories/survey.repository';
12+
// export * from './repositories/seed.repository';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement Seed mongoose model
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement Survey mongoose model
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement User mongoose model
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement Seed repository
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement Survey repository
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement User repository
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement Seed type interface + Zod validation

0 commit comments

Comments
 (0)