add tests

Signed-off-by: Brian Forbis <bforbis@athenahealth.com>
This commit is contained in:
Brian Forbis
2023-04-19 12:01:26 -04:00
parent 6609a1707b
commit 9955591788
2 changed files with 80 additions and 1 deletions
@@ -165,6 +165,7 @@ describe('DatabaseManager', () => {
user: 'foo',
password: 'bar',
port: '5432',
application_name: 'backstage_plugin_pluginwithoutconfig',
},
});
@@ -741,5 +742,83 @@ describe('DatabaseManager', () => {
expect(baseConfig.data.role).toEqual('backstage-plugin');
});
it('Defaults the application_name for postgres clients', async () => {
const testManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'pg',
connection: {},
},
},
}),
);
await testManager.forPlugin('testplugin').getClient();
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, _] = mockCalls[0];
expect(baseConfig.get()).toMatchObject({
connection: {
application_name: 'backstage_plugin_testplugin',
},
});
});
it('Allows manually setting the application_name for postgres clients', async () => {
const testManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'pg',
connection: {
application_name: 'backstage_custom_app_name',
},
},
},
}),
);
await testManager.forPlugin('testplugin').getClient();
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, overrides] = mockCalls[0];
expect(baseConfig.get().connection.application_name).toBe(
'backstage_custom_app_name',
);
expect(overrides.connection.application_name).toBeUndefined();
});
it('Allows manually setting the application_name for individual plugin client', async () => {
const testManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'pg',
connection: {
host: 'localhost',
user: 'foo',
password: 'bar',
database: 'foodb',
application_name: 'backstage_custom_app_name',
},
plugin: {
overrideplugin: {
connection: {
application_name: 'custom_plugin',
},
},
},
},
},
}),
);
await testManager.forPlugin('overrideplugin').getClient();
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, _] = mockCalls[0];
expect(baseConfig.get().connection.application_name).toBe(
'custom_plugin',
);
});
});
});
@@ -270,7 +270,7 @@ export class DatabaseManager {
if (client === 'pg') {
(
connection as Knex.PgConnectionConfig
baseConnection as Knex.PgConnectionConfig
).application_name ||= `backstage_plugin_${pluginId}`;
}