Files
lolympus-tournament-support/apps/api/src/modules/global/database/connection.service.spec.ts
T

129 lines
3.9 KiB
TypeScript

import mongoose from 'mongoose';
import {
ConnectionService,
resolveMongoAppName,
resolveMongoConnectionOptions,
resolveMongoDbName,
} from './connection.service';
function createFakeConnection(connectionId: string) {
const models: Record<string, unknown> = {};
return {
readyState: mongoose.ConnectionStates.connected,
models,
model: jest.fn((name: string) => {
const model = { connectionId, modelName: name };
models[name] = model;
return model;
}),
};
}
describe('ConnectionService', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('shares one MongoDB connection across concurrent initialization calls', async () => {
const connection = createFakeConnection('shared');
const createConnection = jest
.spyOn(mongoose, 'createConnection')
.mockReturnValue({
asPromise: jest.fn().mockResolvedValue(connection),
} as never);
const service = new ConnectionService({
get: jest.fn().mockResolvedValue('mongodb://localhost/test'),
} as never);
const [first, second] = await Promise.all([
service.getConnection(),
service.getConnection(),
]);
expect(first).toBe(connection);
expect(second).toBe(connection);
expect(createConnection).toHaveBeenCalledTimes(1);
});
it('rebuilds models from the replacement connection after disconnect', async () => {
const firstConnection = createFakeConnection('first');
const secondConnection = createFakeConnection('second');
jest
.spyOn(mongoose, 'createConnection')
.mockReturnValueOnce({
asPromise: jest.fn().mockResolvedValue(firstConnection),
} as never)
.mockReturnValueOnce({
asPromise: jest.fn().mockResolvedValue(secondConnection),
} as never);
const service = new ConnectionService({
get: jest.fn().mockResolvedValue('mongodb://localhost/test'),
} as never);
const firstModel = await service.getModel('match');
firstConnection.readyState = mongoose.ConnectionStates.disconnected;
const secondModel = await service.getModel('match');
expect(firstModel).toMatchObject({ connectionId: 'first' });
expect(secondModel).toMatchObject({ connectionId: 'second' });
expect(secondModel).not.toBe(firstModel);
});
});
describe('resolveMongoDbName', () => {
it('uses production database only for prod', () => {
expect(resolveMongoDbName('prod')).toBe('lolympus');
});
it('uses isolated databases for non-production environments', () => {
expect(resolveMongoDbName('dev')).toBe('lolympus-dev');
expect(resolveMongoDbName('staging')).toBe('lolympus-staging');
expect(resolveMongoDbName('')).toBe('lolympus-dev');
expect(resolveMongoDbName('local')).toBe('lolympus-dev');
});
});
describe('resolveMongoAppName', () => {
it('identifies api environment and process for MongoDB connection tracking', () => {
expect(resolveMongoAppName('staging', 1234)).toBe(
'lolympus-api:staging:1234',
);
});
it('uses dev as the fallback environment label', () => {
expect(resolveMongoAppName('', 1234)).toBe('lolympus-api:dev:1234');
});
});
describe('resolveMongoConnectionOptions', () => {
it('disables automatic collection and index creation by default', () => {
expect(
resolveMongoConnectionOptions('lolympus-dev', 'lolympus-api:dev:1'),
).toEqual({
dbName: 'lolympus-dev',
appName: 'lolympus-api:dev:1',
minPoolSize: 10,
maxPoolSize: 20,
autoIndex: false,
autoCreate: false,
});
});
it('enables automatic index creation when explicitly requested', () => {
expect(
resolveMongoConnectionOptions(
'lolympus-staging',
'lolympus-api:staging:1',
'true',
),
).toEqual({
dbName: 'lolympus-staging',
appName: 'lolympus-api:staging:1',
minPoolSize: 10,
maxPoolSize: 20,
autoIndex: true,
autoCreate: false,
});
});
});