Skip to content

Commit

Permalink
all unit tests added, ready for review
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Nov 2, 2023
1 parent 1105d8b commit a6ee6fa
Showing 1 changed file with 56 additions and 24 deletions.
80 changes: 56 additions & 24 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ const { ReadPreference } = require('../mongodb');
const { MongoCredentials } = require('../mongodb');
const { MongoClient, MongoParseError, ServerApiVersion } = require('../mongodb');
const { MongoLogger } = require('../mongodb');
const {
SeverityLevel,
MongoLoggableComponent,
MongoLoggerEnvOptions
// eslint-disable-next-line no-restricted-modules
} = require('../../src/mongo_logger');
// eslint-disable-next-line no-restricted-modules
const { SeverityLevel, MongoLoggableComponent } = require('../../src/mongo_logger');
const sinon = require('sinon');
const { Writable } = require('stream');

Expand Down Expand Up @@ -846,7 +842,9 @@ describe('MongoOptions', function () {
});
const log = { t: new Date(), c: 'constructorStdErr', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
expect(stderrStub.write).calledWith(
inspect(log, { breakLength: Infinity, compact: true })
);
});
});

Expand Down Expand Up @@ -874,7 +872,9 @@ describe('MongoOptions', function () {
});
const log = { t: new Date(), c: 'constructorStdOut', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
expect(stdoutStub.write).calledWith(
inspect(log, { breakLength: Infinity, compact: true })
);
});
});

Expand All @@ -893,7 +893,7 @@ describe('MongoOptions', function () {
});
});
});
describe.only('component severities', function () {
describe('component severities', function () {
const components = Object.values(MongoLoggableComponent);
const env_component_names = [
'MONGODB_LOG_COMMAND',
Expand All @@ -912,37 +912,69 @@ describe('MongoOptions', function () {
[components[i]]: severityLevel
}
});
expect(client.options.mongoLoggerOptions.componentSeverities[components[i]]).to.equal(
severityLevel
);
for (const [curComponent, curSeverity] of Object.entries(
client.options.mongoLoggerOptions.componentSeverities
)) {
if (curComponent === components[i]) {
expect(curSeverity).to.equal(severityLevel);
} else {
expect(curSeverity).to.equal(SeverityLevel.OFF);
}
}
}
});
}
});
context('when both client and environment option is provided', function () {
for (let i = 0; i < components.length; i++) {
beforeEach(function () {
it(`it stores severity level for ${components[i]} component correctly (client options have precedence)`, function () {
process.env[env_component_names[i]] = 'emergency';
});

afterEach(function () {
process.env[env_component_names[i]] = undefined;
});

it(`it stores severity levels for ${components[i]} component correctly (client options have precedence)`, function () {
for (const severityLevel of Object.values(SeverityLevel)) {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogComponentSeverities: {
[components[i]]: severityLevel
}
});
expect(client.options.mongoLoggerOptions.componentSeverities[components[i]]).to.equal(
severityLevel
);
for (const [curComponent, curSeverity] of Object.entries(
client.options.mongoLoggerOptions.componentSeverities
)) {
if (curComponent === components[i]) {
expect(curSeverity).to.equal(severityLevel);
} else {
expect(curSeverity).to.equal(SeverityLevel.OFF);
}
}
process.env[env_component_names[i]] = undefined;
}
});
};
}
});
context('when default is provided', function () {
it('unspecified components have default value, while specified components retain value', function () {
for (let i = 0; i < components.length; i++) {
for (const severityLevel of Object.values(SeverityLevel)) {
for (const severityLevel2 of Object.values(SeverityLevel)) {
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogComponentSeverities: {
[components[i]]: severityLevel,
default: severityLevel2
}
});
for (const [curComponent, curSeverity] of Object.entries(
client.options.mongoLoggerOptions.componentSeverities
)) {
if (curComponent === components[i]) {
expect(curSeverity).to.equal(severityLevel);
} else {
expect(curSeverity).to.equal(severityLevel2);
}
}
}
}
}
});
});
});
});
Expand Down

0 comments on commit a6ee6fa

Please sign in to comment.