From 5cb156ef3b0b6c23467d3980e58520a3d06b181e Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Wed, 1 Dec 2021 14:19:21 +0100 Subject: [PATCH] Add tests for runMigrations argument when creating a database manager Signed-off-by: Marcus Eide --- .../src/database/DatabaseManager.test.ts | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/packages/backend-common/src/database/DatabaseManager.test.ts b/packages/backend-common/src/database/DatabaseManager.test.ts index f2fe855234..56fb8ddc3e 100644 --- a/packages/backend-common/src/database/DatabaseManager.test.ts +++ b/packages/backend-common/src/database/DatabaseManager.test.ts @@ -36,25 +36,53 @@ describe('DatabaseManager', () => { afterEach(() => jest.resetAllMocks()); describe('DatabaseManager.fromConfig', () => { - it('accesses the backend.database key', () => { - const config = new ConfigReader({ - backend: { - database: { - client: 'pg', - connection: { - host: 'localhost', - user: 'foo', - password: 'bar', - database: 'foodb', - }, + const backendConfig = { + backend: { + database: { + client: 'pg', + connection: { + host: 'localhost', + user: 'foo', + password: 'bar', + database: 'foodb', }, }, - }); + }, + }; + + it('accesses the backend.database key', () => { + const config = new ConfigReader(backendConfig); const getConfigSpy = jest.spyOn(config, 'getConfig'); DatabaseManager.fromConfig(config); expect(getConfigSpy).toHaveBeenCalledWith('backend.database'); }); + + it('runMigrate default value', () => { + const config = new ConfigReader(backendConfig); + const database = DatabaseManager.fromConfig(config); + const client = database.forPlugin('test'); + + expect(client.runMigrations).toBe(true); + }); + + it('runMigrate as a function', () => { + const config = new ConfigReader(backendConfig); + const runMigrate = jest.fn().mockReturnValue(false); + const database = DatabaseManager.fromConfig(config, runMigrate); + const client = database.forPlugin('test'); + + expect(runMigrate).toHaveBeenCalledTimes(1); + expect(client.runMigrations).toBe(false); + }); + + it('runMigrate as a boolean', () => { + const config = new ConfigReader(backendConfig); + const database = DatabaseManager.fromConfig(config, false); + const client = database.forPlugin('test'); + + expect(client.runMigrations).toBe(false); + }); }); describe('DatabaseManager.forPlugin', () => {