Implement tests
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { createDatabaseClient } from './connection';
|
||||
import { SingleConnectionDatabaseManager } from './SingleConnection';
|
||||
|
||||
jest.mock('./connection');
|
||||
|
||||
describe('SingleConnectionDatabaseManager', () => {
|
||||
const createConfig = (data: any) =>
|
||||
ConfigReader.fromConfigs([
|
||||
{
|
||||
context: '',
|
||||
data,
|
||||
},
|
||||
]);
|
||||
|
||||
const defaultConfigOptions = {
|
||||
backend: {
|
||||
database: {
|
||||
client: 'pg',
|
||||
connection: {
|
||||
host: 'localhost',
|
||||
user: 'foo',
|
||||
password: 'bar',
|
||||
database: 'foodb',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const defaultConfig = () => createConfig(defaultConfigOptions);
|
||||
|
||||
// This is similar to the ts-jest `mocked` helper.
|
||||
const mocked = (f: Function) => f as jest.Mock;
|
||||
|
||||
describe('SingleConnectionDatabaseManager.fromConfig', () => {
|
||||
it('accesses the backend.database key', () => {
|
||||
const getConfig = jest.fn();
|
||||
const config = defaultConfig();
|
||||
config.getConfig = getConfig;
|
||||
|
||||
SingleConnectionDatabaseManager.fromConfig(config);
|
||||
|
||||
expect(getConfig.mock.calls[0][0]).toEqual('backend.database');
|
||||
});
|
||||
});
|
||||
|
||||
describe('SingleConnectionDatabaseManager.forPlugin', () => {
|
||||
const manager = SingleConnectionDatabaseManager.fromConfig(defaultConfig());
|
||||
|
||||
it('connects to a database scoped to the plugin', async () => {
|
||||
const pluginId = 'test1';
|
||||
await manager.forPlugin(pluginId).getClient();
|
||||
|
||||
expect(mocked(createDatabaseClient).mock.calls).toHaveLength(1);
|
||||
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
|
||||
|
||||
const callArgs = mockCalls[0];
|
||||
expect(callArgs[0].get()).toEqual(defaultConfigOptions.backend.database);
|
||||
expect(callArgs[1].connection.database).toEqual(
|
||||
`backstage_plugin_${pluginId}`,
|
||||
);
|
||||
});
|
||||
|
||||
it('allows plugins to get alternative databases', async () => {
|
||||
const pluginId = 'test1';
|
||||
const pluginManager = manager.forPlugin(pluginId);
|
||||
await pluginManager.getClient();
|
||||
|
||||
const secondaryDatabase = 'extra';
|
||||
await pluginManager.getClient(secondaryDatabase);
|
||||
|
||||
expect(mocked(createDatabaseClient).mock.calls).toHaveLength(2);
|
||||
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-2);
|
||||
|
||||
const mainCallArgs = mockCalls[0];
|
||||
const secondaryCallArgs = mockCalls[1];
|
||||
expect(secondaryCallArgs[1].connection.database).toEqual(
|
||||
`backstage_plugin_${pluginId}_${secondaryDatabase}`,
|
||||
);
|
||||
expect(mainCallArgs[1].connection.database).not.toEqual(
|
||||
secondaryCallArgs[1].connection.database,
|
||||
);
|
||||
});
|
||||
|
||||
it('provides different plugins different databases', async () => {
|
||||
const plugin1Id = 'test1';
|
||||
const plugin2Id = 'test2';
|
||||
await manager.forPlugin(plugin1Id).getClient();
|
||||
await manager.forPlugin(plugin2Id).getClient();
|
||||
|
||||
expect(mocked(createDatabaseClient).mock.calls).toHaveLength(2);
|
||||
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-2);
|
||||
|
||||
const plugin1CallArgs = mockCalls[0];
|
||||
const plugin2CallArgs = mockCalls[1];
|
||||
expect(plugin1CallArgs[1].connection.database).not.toEqual(
|
||||
plugin2CallArgs[1].connection.database,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -18,6 +18,18 @@ import { mergeDatabaseConfig } from './config';
|
||||
|
||||
describe('config', () => {
|
||||
describe(mergeDatabaseConfig, () => {
|
||||
it('does not mutate the input object', () => {
|
||||
const input = {
|
||||
original: 'key',
|
||||
};
|
||||
const override = {
|
||||
added: 'value',
|
||||
};
|
||||
|
||||
mergeDatabaseConfig(input, override);
|
||||
expect(input).not.toHaveProperty('added');
|
||||
});
|
||||
|
||||
it('does not require overrides', () => {
|
||||
expect(
|
||||
mergeDatabaseConfig({
|
||||
|
||||
Reference in New Issue
Block a user