Skip to content

Commit

Permalink
chore: log when metadata is array of objects
Browse files Browse the repository at this point in the history
- add test-cases
  • Loading branch information
Sai Sankeerth committed Jun 25, 2024
1 parent 2aec7b8 commit 7896446
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 18 deletions.
132 changes: 124 additions & 8 deletions src/adapters/network.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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({
Expand All @@ -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);
});
});
22 changes: 12 additions & 10 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7896446

Please sign in to comment.