1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/test/e2e/helpers/database-init.js
Christopher Kolstad c17a1980a2
Add service layer
This simplifies stores to just be storage interaction, they no longer react to events.

Controllers now call services and awaits the result from the call.

When the service calls are returned the database is updated.
This simplifies testing dramatically, cause you know that your state is
updated when returned from a call, rather than hoping the store has
picked up the event (which really was a command) and reacted to it.

Events are still emitted from eventStore, so other parts of the app can
react to events as they're being sent out.

As part of the move to services, we now also emit an application-created
event when we see a new client application.

Fixes: #685
Fixes: #595
2021-01-21 10:59:19 +01:00

111 lines
3.3 KiB
JavaScript

'use strict';
const { EventEmitter } = require('events');
const migrator = require('../../../migrator');
const { createStores } = require('../../../lib/db');
const { createDb } = require('../../../lib/db/db-pool');
const dbConfig = require('./database-config');
const dbState = require('./database.json');
// require('db-migrate-shared').log.silence(false);
// because of migrator bug
delete process.env.DATABASE_URL;
// because of db-migrate bug (https://github.com/Unleash/unleash/issues/171)
process.setMaxListeners(0);
async function resetDatabase(stores) {
return Promise.all([
stores.db('strategies').del(),
stores.db('features').del(),
stores.db('client_applications').del(),
stores.db('client_instances').del(),
stores.db('context_fields').del(),
stores.db('users').del(),
stores.db('projects').del(),
stores.db('tags').del(),
stores.db('tag_types').del(),
]);
}
function createStrategies(store) {
return dbState.strategies.map(s => store.createStrategy(s));
}
function createContextFields(store) {
return dbState.contextFields.map(c => store._createContextField(c));
}
function createApplications(store) {
return dbState.applications.map(a => store.upsert(a));
}
function createClientInstance(store) {
return dbState.clientInstances.map(i => store.insert(i));
}
function createProjects(store) {
return dbState.projects.map(i => store.create(i));
}
function createFeatures(store) {
return dbState.features.map(f => store.createFeature(f));
}
async function tagFeatures(tagStore, store) {
await tagStore.createTag({ value: 'Tester', type: 'simple' });
return dbState.features.map(f =>
store.tagFeature(f.name, {
value: 'Tester',
type: 'simple',
}),
);
}
function createTagTypes(store) {
return dbState.tag_types.map(t => store.createTagType(t));
}
async function setupDatabase(stores) {
await Promise.all(createStrategies(stores.strategyStore));
await Promise.all(createContextFields(stores.contextFieldStore));
await Promise.all(createFeatures(stores.featureToggleStore));
await Promise.all(createClientInstance(stores.clientInstanceStore));
await Promise.all(createApplications(stores.clientApplicationsStore));
await Promise.all(createProjects(stores.projectStore));
await Promise.all(createTagTypes(stores.tagTypeStore));
await tagFeatures(stores.tagStore, stores.featureTagStore);
}
module.exports = async function init(databaseSchema = 'test', getLogger) {
const options = {
db: dbConfig.getDb(),
databaseSchema,
minPool: 1,
maxPool: 1,
getLogger,
};
const db = createDb(options);
const eventBus = new EventEmitter();
await db.raw(`DROP SCHEMA IF EXISTS ${options.databaseSchema} CASCADE`);
await db.raw(`CREATE SCHEMA IF NOT EXISTS ${options.databaseSchema}`);
await migrator(options);
await db.destroy();
const stores = await createStores(options, eventBus);
stores.clientMetricsStore.setMaxListeners(0);
await resetDatabase(stores);
await setupDatabase(stores);
return {
stores,
reset: async () => {
await resetDatabase(stores);
await setupDatabase(stores);
},
};
};