Skip to content

Commit fd8d964

Browse files
committed
refactor(NODE-4815): let maybeTruncate have a default
1 parent 1d9b2b9 commit fd8d964

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/mongo_logger.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ export type LoggableEvent =
255255
export interface LogConvertible extends Record<string, any> {
256256
toLog(): Record<string, any>;
257257
}
258-
export function maybeTruncate(ejson: string, maxDocumentLength: number): string {
258+
259+
/** @internal */
260+
export function maybeTruncate(
261+
ejson: string,
262+
maxDocumentLength: number = DEFAULT_MAX_DOCUMENT_LENGTH
263+
): string {
259264
if (maxDocumentLength === 0) {
260265
return ejson;
261266
}

test/unit/mongo_logger.test.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,22 +1216,47 @@ describe('class MongoLogger', function () {
12161216
});
12171217

12181218
describe('maybeTruncate', function () {
1219-
const largeDoc = {};
1219+
let largeEjsonString: string;
1220+
let smallEjsonString: string;
12201221
before(function () {
1221-
for (let i = 0; i < 1000; i++) {
1222+
const largeDoc = {};
1223+
for (let i = 0; i < DEFAULT_MAX_DOCUMENT_LENGTH; i++) {
12221224
largeDoc[`test${i}`] = `Hello_${i}`;
12231225
}
1226+
largeEjsonString = EJSON.stringify(largeDoc);
1227+
const smallDoc = { test: 'Hello' };
1228+
smallEjsonString = EJSON.stringify(smallDoc);
12241229
});
12251230

12261231
context('when maxDocumentLength = 0', function () {
12271232
it('does not truncate document', function () {
1228-
const ejson = EJSON.stringify(largeDoc);
1229-
expect(ejson).to.equal(maybeTruncate(ejson, 0));
1233+
expect(maybeTruncate(largeEjsonString, 0)).to.equal(largeEjsonString);
12301234
});
12311235
});
12321236

12331237
context('when maxDocumentLength is non-zero', function () {
1234-
context('when document has length greater than maxDocumentLength', function () {});
1235-
context('when document has length less than or equal to maxDocumentLength', function () {});
1238+
context('when document has length greater than maxDocumentLength', function () {
1239+
it('truncates ejson string to length of maxDocumentLength + 3', function () {
1240+
expect(maybeTruncate(largeEjsonString)).to.have.lengthOf(DEFAULT_MAX_DOCUMENT_LENGTH + 3);
1241+
});
1242+
it('ends with "..."', function () {
1243+
expect(maybeTruncate(largeEjsonString)).to.match(/^.*\.\.\.$/);
1244+
});
1245+
});
1246+
1247+
context('when document has length less than or equal to maxDocumentLength', function () {
1248+
it('does not truncate document', function () {
1249+
expect(maybeTruncate(smallEjsonString)).to.equal(smallEjsonString);
1250+
});
1251+
it('does not end with "..."', function () {
1252+
expect(maybeTruncate(smallEjsonString)).to.not.match(/^.*\.\.\./);
1253+
});
1254+
1255+
it('produces valid relaxed EJSON', function () {
1256+
expect(() => {
1257+
EJSON.parse(maybeTruncate(smallEjsonString));
1258+
}).to.not.throw();
1259+
});
1260+
});
12361261
});
12371262
});

0 commit comments

Comments
 (0)