mirror of
https://github.com/immich-app/immich.git
synced 2024-12-28 22:51:59 +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();
|
expect(sut).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('onBootstrap', () => {
|
||||||
it('should throw an error if PostgreSQL version is below minimum supported version', async () => {
|
it('should throw an error if PostgreSQL version is below minimum supported version', async () => {
|
||||||
databaseMock.getPostgresVersion.mockResolvedValueOnce('13.10.0');
|
databaseMock.getPostgresVersion.mockResolvedValueOnce('13.10.0');
|
||||||
|
|
||||||
|
@ -195,6 +196,21 @@ describe(DatabaseService.name, () => {
|
||||||
expect(loggerMock.fatal).not.toHaveBeenCalled();
|
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 () => {
|
it(`should raise error if ${extension} extension upgrade failed`, async () => {
|
||||||
databaseMock.getExtensionVersion.mockResolvedValue({
|
databaseMock.getExtensionVersion.mockResolvedValue({
|
||||||
availableVersion: updateInRange,
|
availableVersion: updateInRange,
|
||||||
|
@ -239,6 +255,21 @@ describe(DatabaseService.name, () => {
|
||||||
expect(loggerMock.fatal).not.toHaveBeenCalled();
|
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 () => {
|
it(`should not reindex ${extension} indices if not needed`, async () => {
|
||||||
databaseMock.shouldReindex.mockResolvedValue(false);
|
databaseMock.shouldReindex.mockResolvedValue(false);
|
||||||
|
|
||||||
|
@ -322,3 +353,50 @@ describe(DatabaseService.name, () => {
|
||||||
expect(databaseMock.runMigrations).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',
|
'src/services/index.ts',
|
||||||
],
|
],
|
||||||
thresholds: {
|
thresholds: {
|
||||||
lines: 80,
|
lines: 85,
|
||||||
statements: 80,
|
statements: 85,
|
||||||
branches: 85,
|
branches: 90,
|
||||||
functions: 80,
|
functions: 85,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
|
|
Loading…
Reference in a new issue