2024-01-12 10:25:59 +01:00
|
|
|
import dbInit, { ITestDb } from '../../test/e2e/helpers/database-init';
|
2023-06-13 15:54:20 +02:00
|
|
|
import getLogger from '../../test/fixtures/no-logger';
|
2024-01-12 10:25:59 +01:00
|
|
|
import {
|
|
|
|
IFeatureStrategiesStore,
|
|
|
|
IFeatureStrategy,
|
|
|
|
IFeatureToggleStore,
|
|
|
|
IStrategyStore,
|
|
|
|
PartialSome,
|
|
|
|
} from '../types';
|
2023-06-13 15:54:20 +02:00
|
|
|
|
2024-01-12 10:25:59 +01:00
|
|
|
let db: ITestDb;
|
2023-06-13 15:54:20 +02:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
db = await dbInit('feature_strategy_store_serial', getLogger);
|
|
|
|
getLogger.setMuteError(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
if (db) {
|
|
|
|
await db.destroy();
|
|
|
|
}
|
|
|
|
getLogger.setMuteError(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('returns 0 if no custom strategies', async () => {
|
|
|
|
// Arrange
|
2024-01-12 10:25:59 +01:00
|
|
|
const featureStrategiesStore: IFeatureStrategiesStore =
|
2023-06-13 15:54:20 +02:00
|
|
|
db.stores.featureStrategiesStore;
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const inUseCount =
|
|
|
|
await featureStrategiesStore.getCustomStrategiesInUseCount();
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(inUseCount).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('returns 0 if no custom strategies are in use', async () => {
|
|
|
|
// Arrange
|
2024-01-12 10:25:59 +01:00
|
|
|
const featureToggleStore: IFeatureToggleStore =
|
|
|
|
db.stores.featureToggleStore;
|
|
|
|
const featureStrategiesStore: IFeatureStrategiesStore =
|
2023-06-13 15:54:20 +02:00
|
|
|
db.stores.featureStrategiesStore;
|
2024-01-12 10:25:59 +01:00
|
|
|
const strategyStore: IStrategyStore = db.stores.strategyStore;
|
2023-06-13 15:54:20 +02:00
|
|
|
|
2024-01-12 10:25:59 +01:00
|
|
|
await featureToggleStore.create('default', {
|
2023-06-13 15:54:20 +02:00
|
|
|
name: 'test-toggle-2',
|
2023-12-22 14:33:16 +01:00
|
|
|
createdByUserId: 9999,
|
2023-06-13 15:54:20 +02:00
|
|
|
});
|
|
|
|
|
2024-01-12 10:25:59 +01:00
|
|
|
await strategyStore.createStrategy({
|
2023-06-13 15:54:20 +02:00
|
|
|
name: 'strategy-2',
|
|
|
|
parameters: [],
|
|
|
|
description: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const inUseCount =
|
|
|
|
await featureStrategiesStore.getCustomStrategiesInUseCount();
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(inUseCount).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('counts custom strategies in use', async () => {
|
|
|
|
// Arrange
|
2024-01-12 10:25:59 +01:00
|
|
|
const featureToggleStore: IFeatureToggleStore =
|
|
|
|
db.stores.featureToggleStore;
|
|
|
|
const featureStrategiesStore: IFeatureStrategiesStore =
|
2023-06-13 15:54:20 +02:00
|
|
|
db.stores.featureStrategiesStore;
|
2024-01-12 10:25:59 +01:00
|
|
|
const strategyStore: IStrategyStore = db.stores.strategyStore;
|
2023-06-13 15:54:20 +02:00
|
|
|
|
|
|
|
await featureToggleStore.create('default', {
|
|
|
|
name: 'test-toggle',
|
2023-12-22 14:33:16 +01:00
|
|
|
createdByUserId: 9999,
|
2023-06-13 15:54:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
await strategyStore.createStrategy({
|
|
|
|
name: 'strategy-1',
|
|
|
|
parameters: [],
|
|
|
|
description: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
await featureStrategiesStore.createStrategyFeatureEnv({
|
|
|
|
projectId: 'default',
|
|
|
|
featureName: 'test-toggle',
|
|
|
|
strategyName: 'strategy-1',
|
|
|
|
environment: 'default',
|
|
|
|
parameters: {},
|
|
|
|
constraints: [],
|
2023-07-13 16:04:55 +02:00
|
|
|
variants: [],
|
2023-06-13 15:54:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const inUseCount =
|
|
|
|
await featureStrategiesStore.getCustomStrategiesInUseCount();
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(inUseCount).toEqual(1);
|
|
|
|
});
|
2023-07-26 11:31:26 +02:00
|
|
|
|
|
|
|
const baseStrategy: PartialSome<IFeatureStrategy, 'id' | 'createdAt'> = {
|
|
|
|
projectId: 'default',
|
|
|
|
featureName: 'test-toggle-increment',
|
|
|
|
strategyName: 'strategy-1',
|
|
|
|
environment: 'default',
|
|
|
|
parameters: {},
|
|
|
|
constraints: [],
|
|
|
|
variants: [],
|
|
|
|
};
|
|
|
|
test('increment sort order on each new insert', async () => {
|
2024-01-12 10:25:59 +01:00
|
|
|
const featureToggleStore: IFeatureToggleStore =
|
|
|
|
db.stores.featureToggleStore;
|
|
|
|
const featureStrategiesStore: IFeatureStrategiesStore =
|
2023-07-26 11:31:26 +02:00
|
|
|
db.stores.featureStrategiesStore;
|
|
|
|
|
|
|
|
await featureToggleStore.create('default', {
|
|
|
|
name: 'test-toggle-increment',
|
2023-12-22 14:33:16 +01:00
|
|
|
createdByUserId: 9999,
|
2023-07-26 11:31:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const { id: firstId } =
|
|
|
|
await featureStrategiesStore.createStrategyFeatureEnv({
|
|
|
|
...baseStrategy,
|
|
|
|
featureName: 'test-toggle-increment',
|
|
|
|
strategyName: 'strategy-1',
|
|
|
|
// sort order implicitly 0
|
|
|
|
});
|
|
|
|
const { id: secondId } =
|
|
|
|
await featureStrategiesStore.createStrategyFeatureEnv({
|
|
|
|
...baseStrategy,
|
|
|
|
featureName: 'test-toggle-increment',
|
|
|
|
strategyName: 'strategy-2',
|
|
|
|
sortOrder: 50, // explicit sort order
|
|
|
|
});
|
|
|
|
const { id: thirdId } =
|
|
|
|
await featureStrategiesStore.createStrategyFeatureEnv({
|
|
|
|
...baseStrategy,
|
|
|
|
featureName: 'test-toggle-increment',
|
|
|
|
strategyName: 'strategy-2',
|
|
|
|
// implicit sort order incremented by 1
|
|
|
|
});
|
|
|
|
|
|
|
|
const firstStrategy = await featureStrategiesStore.getStrategyById(firstId);
|
2023-11-27 13:42:58 +01:00
|
|
|
const secondStrategy =
|
|
|
|
await featureStrategiesStore.getStrategyById(secondId);
|
2023-07-26 11:31:26 +02:00
|
|
|
const thirdStrategy = await featureStrategiesStore.getStrategyById(thirdId);
|
|
|
|
|
|
|
|
expect(firstStrategy.sortOrder).toEqual(0);
|
|
|
|
expect(secondStrategy.sortOrder).toEqual(50);
|
|
|
|
expect(thirdStrategy.sortOrder).toEqual(51);
|
|
|
|
});
|