From 78964467f6ecbf850838e903a9bb2ce5d3ed43f6 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Tue, 25 Jun 2024 10:48:31 +0530 Subject: [PATCH] chore: log when metadata is array of objects - add test-cases --- src/adapters/network.test.js | 132 ++++++++++++++++++++++++++++++++--- src/logger.js | 22 +++--- 2 files changed, 136 insertions(+), 18 deletions(-) diff --git a/src/adapters/network.test.js b/src/adapters/network.test.js index b026d414f4..5c9fe2dd9b 100644 --- a/src/adapters/network.test.js +++ b/src/adapters/network.test.js @@ -44,7 +44,7 @@ describe('logging in http methods', () => { mockLoggerInstance.info.mockClear(); loggerUtil.getMatchedMetadata.mockClear(); }); - test('when proper metadata is sent should call logger without error', async () => { + test('when proper metadata(object) is sent should call logger without error', async () => { const statTags = { metadata: { destType: 'DT', @@ -105,6 +105,32 @@ describe('logging in http methods', () => { endpointPath: '/m/n/o', requestMethod: 'post', }; + loggerUtil.getMatchedMetadata.mockReturnValue([]); + + axios.post.mockResolvedValueOnce({ + status: 200, + data: { a: 1, b: 2, c: 'abc' }, + headers: { + 'Content-Type': 'apllication/json', + 'X-Some-Header': 'headsome', + }, + }); + await expect(httpPOST('https://some.web.com/m/n/o', {}, {}, statTags)).resolves.not.toThrow( + Error, + ); + expect(loggerUtil.getMatchedMetadata).toHaveBeenCalledTimes(2); + + expect(mockLoggerInstance.info).toHaveBeenCalledTimes(0); + }); + + test('when metadata is string should call logger without error', async () => { + const statTags = { + metadata: 'random metadata', + destType: 'DT', + feature: 'feat', + endpointPath: '/m/n/o', + requestMethod: 'post', + }; loggerUtil.getMatchedMetadata.mockReturnValue([statTags.metadata]); axios.post.mockResolvedValueOnce({ @@ -120,21 +146,111 @@ describe('logging in http methods', () => { ); expect(loggerUtil.getMatchedMetadata).toHaveBeenCalledTimes(2); - expect(mockLoggerInstance.info).toHaveBeenCalledTimes(2); + expect(mockLoggerInstance.info).toHaveBeenCalledTimes(0); + }); - expect(mockLoggerInstance.info).toHaveBeenNthCalledWith(1, ' [DT] /m/n/o request', { - body: {}, - url: 'https://some.web.com/m/n/o', - method: 'post', + test('when proper metadata(Array) is sent should call logger without error', async () => { + const metadata = [ + { + destType: 'DT', + destinationId: 'd1', + workspaceId: 'w1', + sourceId: 's1', + jobId: 1, + }, + { + destType: 'DT', + destinationId: 'd1', + workspaceId: 'w1', + sourceId: 's1', + jobId: 2, + }, + { + destType: 'DT', + destinationId: 'd1', + workspaceId: 'w1', + sourceId: 's1', + jobId: 3, + }, + ]; + const statTags = { + metadata, + destType: 'DT', + feature: 'feat', + endpointPath: '/m/n/o', + requestMethod: 'post', + }; + loggerUtil.getMatchedMetadata.mockReturnValue(statTags.metadata); + + axios.post.mockResolvedValueOnce({ + status: 200, + data: { a: 1, b: 2, c: 'abc' }, + headers: { + 'Content-Type': 'apllication/json', + 'X-Some-Header': 'headsome', + }, }); + await expect(httpPOST('https://some.web.com/m/n/o', {}, {}, statTags)).resolves.not.toThrow( + Error, + ); + expect(loggerUtil.getMatchedMetadata).toHaveBeenCalledTimes(2); - expect(mockLoggerInstance.info).toHaveBeenNthCalledWith(2, ' [DT] /m/n/o response', { - body: { a: 1, b: 2, c: 'abc' }, + expect(mockLoggerInstance.info).toHaveBeenCalledTimes(metadata.length * 2); + + [1, 2, 3].forEach((i) => { + expect(mockLoggerInstance.info).toHaveBeenNthCalledWith(i, ' [DT] /m/n/o request', { + body: {}, + destType: 'DT', + destinationId: 'd1', + workspaceId: 'w1', + sourceId: 's1', + url: 'https://some.web.com/m/n/o', + method: 'post', + }); + + expect(mockLoggerInstance.info).toHaveBeenNthCalledWith( + i + metadata.length, + ' [DT] /m/n/o response', + { + destType: 'DT', + destinationId: 'd1', + workspaceId: 'w1', + sourceId: 's1', + body: { a: 1, b: 2, c: 'abc' }, + status: 200, + headers: { + 'Content-Type': 'apllication/json', + 'X-Some-Header': 'headsome', + }, + }, + ); + }); + }); + + test('when proper metadata(Array of strings,numbers) is sent should call logger without error', async () => { + const metadata = [1, 2, '3']; + const statTags = { + metadata, + destType: 'DT', + feature: 'feat', + endpointPath: '/m/n/o', + requestMethod: 'post', + }; + loggerUtil.getMatchedMetadata.mockReturnValue(statTags.metadata); + + axios.post.mockResolvedValueOnce({ status: 200, + data: { a: 1, b: 2, c: 'abc' }, headers: { 'Content-Type': 'apllication/json', 'X-Some-Header': 'headsome', }, }); + await expect(httpPOST('https://some.web.com/m/n/o', {}, {}, statTags)).resolves.not.toThrow( + Error, + ); + expect(loggerUtil.getMatchedMetadata).toHaveBeenCalledTimes(2); + + expect(mockLoggerInstance.info).toHaveBeenCalledTimes(0); }); }); diff --git a/src/logger.js b/src/logger.js index 5bfeadd8c2..4c80c938bb 100644 --- a/src/logger.js +++ b/src/logger.js @@ -88,16 +88,18 @@ const log = (logMethod, logArgs) => { if (logInfo) { const { metadata, ...otherLogInfoArgs } = logInfo; if (Array.isArray(metadata)) { - metadata.forEach((m) => { - logMethod( - message, - { - ...getLogMetadata(m), - ...otherLogInfoArgs, - }, - ...otherArgs, - ); - }); + metadata + .filter((m) => typeof m === 'object' && !Array.isArray(m)) + .forEach((m) => { + logMethod( + message, + { + ...getLogMetadata(m), + ...otherLogInfoArgs, + }, + ...otherArgs, + ); + }); return; } logMethod(