From 3cdba9d31b8c24ecafdfea0a334f7acfcf4ea545 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Wed, 10 Aug 2022 22:16:23 -0700 Subject: [PATCH] Fixes #346 - metric/statistics by date range --- src/routers/metric.js | 4 ++-- tests/metric.test.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/routers/metric.js b/src/routers/metric.js index 5d74ff1..4526223 100644 --- a/src/routers/metric.js +++ b/src/routers/metric.js @@ -44,8 +44,8 @@ router.get('/metric/statistics/', auth, [ query('result').optional().isBoolean(), query('component').optional().isLength({ max: 50 }), query('group').optional().isLength({ max: 30 }), - query('dateBefore').optional().isDate(), - query('dateAfter').optional().isDate() + query('dateBefore').optional().isISO8601(), + query('dateAfter').optional().isISO8601() ], validate, async (req, res) => { try { const result = await getStatistics(req); diff --git a/tests/metric.test.js b/tests/metric.test.js index 8eaed37..70395cc 100644 --- a/tests/metric.test.js +++ b/tests/metric.test.js @@ -65,6 +65,30 @@ describe('Fetch overall statistics', () => { expect(response.body.switchers[0].switcher).toEqual('KEY_1'); }); + test('METRIC_SUITE - Should return statistics filtered by Switcher KEY and Date Range - One entry', async () => { + const response = await request(app) + .get(`/metric/statistics?domainid=${domainId}&key=KEY_1&statistics=all&dateBefore=2019-12-14 17:01&dateAfter=2019-12-14 16:59`) + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send().expect(200); + + // Response validation + expect(response.body).not.toBeNull(); + expect(response.body.reasons.length).toEqual(1); + expect(response.body.switchers[0].switcher).toEqual('KEY_1'); + }); + + test('METRIC_SUITE - Should return statistics filtered by Switcher KEY and Date Range - Two entries', async () => { + const response = await request(app) + .get(`/metric/statistics?domainid=${domainId}&key=KEY_1&statistics=all&dateBefore=2019-12-14 18:02&dateAfter=2019-12-14 16:59`) + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send().expect(200); + + // Response validation + expect(response.body).not.toBeNull(); + expect(response.body.reasons.length).toEqual(2); + expect(response.body.switchers[0].switcher).toEqual('KEY_1'); + }); + test('METRIC_SUITE - Should return statistics filtered by Result', async () => { const response = await request(app) .get(`/metric/statistics?domainid=${domainId}&statistics=all&result=true`) @@ -122,12 +146,12 @@ describe('Fetch overall statistics', () => { .send().expect(422); await request(app) - .get(`/metric/statistics?domainid=${domainId}&statistics=all&dateBefore=2020`) + .get(`/metric/statistics?domainid=${domainId}&statistics=all&dateBefore=202000`) .set('Authorization', `Bearer ${adminMasterAccountToken}`) .send().expect(422); await request(app) - .get(`/metric/statistics?domainid=${domainId}&statistics=all&dateAfter=2020`) + .get(`/metric/statistics?domainid=${domainId}&statistics=all&dateAfter=2020000`) .set('Authorization', `Bearer ${adminMasterAccountToken}`) .send().expect(422); });