mirror of
https://github.com/immich-app/immich.git
synced 2024-12-28 06:31:58 +00:00
chore: database service unit tests (#13345)
This commit is contained in:
parent
bd779ff437
commit
79ae4e211b
2 changed files with 314 additions and 236 deletions
|
@ -43,6 +43,7 @@ describe(DatabaseService.name, () => {
|
|||
expect(sut).toBeDefined();
|
||||
});
|
||||
|
||||
describe('onBootstrap', () => {
|
||||
it('should throw an error if PostgreSQL version is below minimum supported version', async () => {
|
||||
databaseMock.getPostgresVersion.mockResolvedValueOnce('13.10.0');
|
||||
|
||||
|
@ -195,6 +196,21 @@ describe(DatabaseService.name, () => {
|
|||
expect(loggerMock.fatal).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error if installed version is not in version range', async () => {
|
||||
databaseMock.getExtensionVersion.mockResolvedValue({
|
||||
availableVersion: minVersionInRange,
|
||||
installedVersion: versionAboveRange,
|
||||
});
|
||||
|
||||
await expect(sut.onBootstrap()).rejects.toThrow(
|
||||
`The ${extensionName} extension version is ${versionAboveRange}, but Immich only supports`,
|
||||
);
|
||||
|
||||
expect(databaseMock.updateVectorExtension).not.toHaveBeenCalled();
|
||||
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
|
||||
expect(loggerMock.fatal).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it(`should raise error if ${extension} extension upgrade failed`, async () => {
|
||||
databaseMock.getExtensionVersion.mockResolvedValue({
|
||||
availableVersion: updateInRange,
|
||||
|
@ -239,6 +255,21 @@ describe(DatabaseService.name, () => {
|
|||
expect(loggerMock.fatal).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it(`should throw an error if reindexing fails`, async () => {
|
||||
databaseMock.shouldReindex.mockResolvedValue(true);
|
||||
databaseMock.reindex.mockRejectedValue(new Error('Error reindexing'));
|
||||
|
||||
await expect(sut.onBootstrap()).rejects.toBeDefined();
|
||||
|
||||
expect(databaseMock.shouldReindex).toHaveBeenCalledTimes(1);
|
||||
expect(databaseMock.reindex).toHaveBeenCalledTimes(1);
|
||||
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
|
||||
expect(loggerMock.fatal).not.toHaveBeenCalled();
|
||||
expect(loggerMock.warn).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Could not run vector reindexing checks.'),
|
||||
);
|
||||
});
|
||||
|
||||
it(`should not reindex ${extension} indices if not needed`, async () => {
|
||||
databaseMock.shouldReindex.mockResolvedValue(false);
|
||||
|
||||
|
@ -321,4 +352,51 @@ describe(DatabaseService.name, () => {
|
|||
expect(databaseMock.updateVectorExtension).not.toHaveBeenCalled();
|
||||
expect(databaseMock.runMigrations).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleConnectionError', () => {
|
||||
beforeAll(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should not override interval', () => {
|
||||
sut.handleConnectionError(new Error('Error'));
|
||||
expect(loggerMock.error).toHaveBeenCalled();
|
||||
|
||||
sut.handleConnectionError(new Error('foo'));
|
||||
expect(loggerMock.error).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should reconnect when interval elapses', async () => {
|
||||
databaseMock.reconnect.mockResolvedValue(true);
|
||||
|
||||
sut.handleConnectionError(new Error('error'));
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
|
||||
expect(databaseMock.reconnect).toHaveBeenCalledTimes(1);
|
||||
expect(loggerMock.log).toHaveBeenCalledWith('Database reconnected');
|
||||
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
expect(databaseMock.reconnect).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should try again when reconnection fails', async () => {
|
||||
databaseMock.reconnect.mockResolvedValueOnce(false);
|
||||
|
||||
sut.handleConnectionError(new Error('error'));
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
|
||||
expect(databaseMock.reconnect).toHaveBeenCalledTimes(1);
|
||||
expect(loggerMock.warn).toHaveBeenCalledWith(expect.stringContaining('Database connection failed'));
|
||||
|
||||
databaseMock.reconnect.mockResolvedValueOnce(true);
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
expect(databaseMock.reconnect).toHaveBeenCalledTimes(2);
|
||||
expect(loggerMock.log).toHaveBeenCalledWith('Database reconnected');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -17,10 +17,10 @@ export default defineConfig({
|
|||
'src/services/index.ts',
|
||||
],
|
||||
thresholds: {
|
||||
lines: 80,
|
||||
statements: 80,
|
||||
branches: 85,
|
||||
functions: 80,
|
||||
lines: 85,
|
||||
statements: 85,
|
||||
branches: 90,
|
||||
functions: 85,
|
||||
},
|
||||
},
|
||||
server: {
|
||||
|
|
Loading…
Reference in a new issue