1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00

feat(1-3260): update test impl

This commit is contained in:
Thomas Heartman 2025-01-21 15:05:12 +01:00
parent d8526f1911
commit ac7ddb9519
No known key found for this signature in database
GPG Key ID: BD1F880DAED1EE78
3 changed files with 50 additions and 37 deletions

View File

@ -7,6 +7,13 @@ export type IStatTrafficUsage = {
count: number; count: number;
}; };
export type IStatMonthlyTrafficUsage = {
month: Date;
trafficGroup: string;
statusCodeSeries: number;
count: number;
};
export interface IStatTrafficUsageKey { export interface IStatTrafficUsageKey {
day: Date; day: Date;
trafficGroup: string; trafficGroup: string;
@ -17,4 +24,7 @@ export interface ITrafficDataUsageStore
extends Store<IStatTrafficUsage, IStatTrafficUsageKey> { extends Store<IStatTrafficUsage, IStatTrafficUsageKey> {
upsert(trafficDataUsage: IStatTrafficUsage): Promise<void>; upsert(trafficDataUsage: IStatTrafficUsage): Promise<void>;
getTrafficDataUsageForPeriod(period: string): Promise<IStatTrafficUsage[]>; getTrafficDataUsageForPeriod(period: string): Promise<IStatTrafficUsage[]>;
getTrafficDataForMonthRange(
monthsBack: number,
): Promise<IStatMonthlyTrafficUsage[]>;
} }

View File

@ -1,3 +1,4 @@
import { subMonths } from 'date-fns';
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init';
import getLogger from '../../../test/fixtures/no-logger'; import getLogger from '../../../test/fixtures/no-logger';
import type { ITrafficDataUsageStore, IUnleashStores } from '../../types'; import type { ITrafficDataUsageStore, IUnleashStores } from '../../types';
@ -197,43 +198,38 @@ test('can query for data from specific periods', async () => {
}); });
test('can query for monthly aggregation of data for a specified range', async () => { test('can query for monthly aggregation of data for a specified range', async () => {
const data1 = { const now = new Date();
day: new Date(2024, 2, 12),
trafficGroup: 'default-period-query',
statusCodeSeries: 200,
count: 1,
};
const data2 = {
day: new Date(2024, 2, 13),
trafficGroup: 'default-period-query',
statusCodeSeries: 200,
count: 3,
};
const data3 = {
day: new Date(2024, 1, 12),
trafficGroup: 'default-period-query',
statusCodeSeries: 200,
count: 2,
};
const data4 = {
day: new Date(2023, 9, 6),
trafficGroup: 'default-period-query',
statusCodeSeries: 200,
count: 12,
};
await trafficDataUsageStore.upsert(data1);
await trafficDataUsageStore.upsert(data2);
await trafficDataUsageStore.upsert(data3);
await trafficDataUsageStore.upsert(data4);
const traffic_period_usage = const expectedValues: number[] = [];
await trafficDataUsageStore.getTrafficDataUsageForPeriod('2024-03');
expect(traffic_period_usage).toBeDefined();
expect(traffic_period_usage.length).toBe(2);
const traffic_period_usage_older = // fill in with data for the last 13 months
await trafficDataUsageStore.getTrafficDataUsageForPeriod('2023-10'); for (let i = 0; i <= 12; i++) {
expect(traffic_period_usage_older).toBeDefined(); const month = subMonths(now, i).getMonth();
expect(traffic_period_usage_older.length).toBe(1); let monthAggregate = 0;
expect(traffic_period_usage_older[0].count).toBe(12); for (let day = 1; day <= 5; day++) {
const dayValue = i + day;
monthAggregate += dayValue;
const data = {
day: new Date(2024, month, day),
trafficGroup: 'default-period-query',
statusCodeSeries: 200,
count: dayValue,
};
await trafficDataUsageStore.upsert(data);
}
expectedValues.push(monthAggregate);
}
console.log(expectedValues);
for (const monthsBack of [3, 6, 12]) {
const result =
await trafficDataUsageStore.getTrafficDataForMonthRange(monthsBack);
// should have the current month and the preceding n months
expect(result.length).toBe(monthsBack + 1);
// the data should be aggregated correctly
// expect(result.every(data, index) => { data.property === expectedValues[index] })
}
}); });

View File

@ -1,6 +1,7 @@
import type { Db } from '../../db/db'; import type { Db } from '../../db/db';
import type { Logger, LogProvider } from '../../logger'; import type { Logger, LogProvider } from '../../logger';
import type { import type {
IStatMonthlyTrafficUsage,
IStatTrafficUsage, IStatTrafficUsage,
IStatTrafficUsageKey, IStatTrafficUsageKey,
ITrafficDataUsageStore, ITrafficDataUsageStore,
@ -97,4 +98,10 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
); );
return rows.map(mapRow); return rows.map(mapRow);
} }
async getTrafficDataForMonthRange(
monthsBack: number,
): Promise<IStatMonthlyTrafficUsage[]> {
return [];
}
} }