From ac7ddb951983a9b67cf6b8a3649410b1ca272e31 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 21 Jan 2025 15:05:12 +0100 Subject: [PATCH] feat(1-3260): update test impl --- .../traffic-data-usage-store-type.ts | 10 +++ .../traffic-data-usage-store.test.ts | 70 +++++++++---------- .../traffic-data-usage-store.ts | 7 ++ 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/lib/features/traffic-data-usage/traffic-data-usage-store-type.ts b/src/lib/features/traffic-data-usage/traffic-data-usage-store-type.ts index f92a1640d3..e753028d93 100644 --- a/src/lib/features/traffic-data-usage/traffic-data-usage-store-type.ts +++ b/src/lib/features/traffic-data-usage/traffic-data-usage-store-type.ts @@ -7,6 +7,13 @@ export type IStatTrafficUsage = { count: number; }; +export type IStatMonthlyTrafficUsage = { + month: Date; + trafficGroup: string; + statusCodeSeries: number; + count: number; +}; + export interface IStatTrafficUsageKey { day: Date; trafficGroup: string; @@ -17,4 +24,7 @@ export interface ITrafficDataUsageStore extends Store { upsert(trafficDataUsage: IStatTrafficUsage): Promise; getTrafficDataUsageForPeriod(period: string): Promise; + getTrafficDataForMonthRange( + monthsBack: number, + ): Promise; } diff --git a/src/lib/features/traffic-data-usage/traffic-data-usage-store.test.ts b/src/lib/features/traffic-data-usage/traffic-data-usage-store.test.ts index 33b492281f..5f981e1cd6 100644 --- a/src/lib/features/traffic-data-usage/traffic-data-usage-store.test.ts +++ b/src/lib/features/traffic-data-usage/traffic-data-usage-store.test.ts @@ -1,3 +1,4 @@ +import { subMonths } from 'date-fns'; import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; import getLogger from '../../../test/fixtures/no-logger'; 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 () => { - const data1 = { - 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 now = new Date(); - const traffic_period_usage = - await trafficDataUsageStore.getTrafficDataUsageForPeriod('2024-03'); - expect(traffic_period_usage).toBeDefined(); - expect(traffic_period_usage.length).toBe(2); + const expectedValues: number[] = []; - const traffic_period_usage_older = - await trafficDataUsageStore.getTrafficDataUsageForPeriod('2023-10'); - expect(traffic_period_usage_older).toBeDefined(); - expect(traffic_period_usage_older.length).toBe(1); - expect(traffic_period_usage_older[0].count).toBe(12); + // fill in with data for the last 13 months + for (let i = 0; i <= 12; i++) { + const month = subMonths(now, i).getMonth(); + let monthAggregate = 0; + 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] }) + } }); diff --git a/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts b/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts index 5d288089ba..c55d83336c 100644 --- a/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts +++ b/src/lib/features/traffic-data-usage/traffic-data-usage-store.ts @@ -1,6 +1,7 @@ import type { Db } from '../../db/db'; import type { Logger, LogProvider } from '../../logger'; import type { + IStatMonthlyTrafficUsage, IStatTrafficUsage, IStatTrafficUsageKey, ITrafficDataUsageStore, @@ -97,4 +98,10 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore { ); return rows.map(mapRow); } + + async getTrafficDataForMonthRange( + monthsBack: number, + ): Promise { + return []; + } }