Skip to content

Commit

Permalink
fix(NODE-3150): added bsonRegExp flag
Browse files Browse the repository at this point in the history
  • Loading branch information
andymina committed Jun 14, 2021
1 parent 08254de commit d36e73c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 57 deletions.
3 changes: 2 additions & 1 deletion src/cmap/auth/mongodb_aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const AWS_EC2_PATH = '/latest/meta-data/iam/security-credentials';
const bsonOptions: BSONSerializeOptions = {
promoteLongs: true,
promoteValues: true,
promoteBuffers: false
promoteBuffers: false,
bsonRegExp: false
};

interface AWSSaslContinuePayload {
Expand Down
3 changes: 2 additions & 1 deletion src/cmap/wire_protocol/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export function applyCommonQueryOptions(
raw: typeof options.raw === 'boolean' ? options.raw : false,
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false
});

if (options.session) {
Expand Down
2 changes: 1 addition & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ const DB_OPTIONS_ALLOW_LIST = [
'raw',
'authSource',
'ignoreUndefined',
'promoteLongs',
'readConcern',
'retryMiliSeconds',
'numberOfRetries',
'loggerLevel',
'logger',
'promoteBuffers',
'promoteLongs',
'bsonRegExp',
'promoteValues',
'compression',
'retryWrites'
Expand Down
1 change: 1 addition & 0 deletions src/operations/create_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ILLEGAL_COMMAND_FIELDS = new Set([
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'serializeFunctions',
'ignoreUndefined'
]);
Expand Down
1 change: 1 addition & 0 deletions src/operations/map_reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const exclusionList = [
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'serializeFunctions',
'ignoreUndefined',
'scope' // this option is reformatted thus exclude the original
Expand Down
1 change: 1 addition & 0 deletions src/sdam/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const DEBUG_FIELDS = [
'promoteLongs',
'promoteValues',
'promoteBuffers',
'bsonRegExp',
'servername'
];

Expand Down
103 changes: 49 additions & 54 deletions test/unit/bson_regex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,67 @@

const { expect } = require('chai');
const { BSONRegExp } = require('../../src/index');
const mock = require('../tools/mock');

describe('BSONRegExp', () => {
let server;
afterEach(() => mock.cleanup());
beforeEach(async () => {
server = await mock.createServer();
server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster || doc.hello) {
request.reply({ ...mock.DEFAULT_ISMASTER });
} else if (doc.insert) {
// insertOne handle
request.reply({ ok: 1 });
} else if (doc.find) {
// findOne handle
request.reply({
ok: 1,
cursor: { id: 1, firstBatch: [{ regex: new BSONRegExp('abc', 'imx') }] }
});
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
});
describe('bsonRegExp option', () => {
it('should respond with BSONRegExp class with flag passed to db', async function () {
let client;
try {
// create and connect to client
client = this.configuration.newClient();
await client.connect();

describe('option passed to client', () => {});
describe('option passed to db', () => {});
describe('option passed to collection', () => {});
const db = client.db('a', { bsonRegExp: true });
const collection = db.collection('b');

// Start here
describe('bsonRegex option passed to operation', () => {
// it('should respond with BSONRexExp class', async function () {
// // create and connect to client
// const client = this.configuration.newClient(`mongodb://${server.uri()}/`); // bsonRegex
// await client.connect();
await collection.insertOne({ regex: new BSONRegExp('abc', 'imx') });
const res = await collection.findOne({ regex: new BSONRegExp('abc', 'imx') });

// const db = client.db('a');
// const collection = db.collection('b');
expect(res).has.property('regex').that.is.instanceOf(BSONRegExp);
} finally {
await client.close();
}
});

// await collection.insertOne({ regex: new BSONRegExp('abc', 'imx') });
// const res = await collection.findOne(
// { regex: new BSONRegExp('abc', 'imx') },
// { bsonRegExp: true }
// );
it('should respond with BSONRegExp class with flag passed to collection', async function () {
let client;
try {
// create and connect to client
client = this.configuration.newClient(); // bsonRegex
await client.connect();

// expect(res).has.property('regex').that.is.instanceOf(BSONRegExp);
// });
const db = client.db('a');
const collection = db.collection('b', { bsonRegExp: true });

it('should respond with BSONRexExp class REAL MONGO', async function () {
// create and connect to client
const client = this.configuration.newClient(); // bsonRegex
await client.connect();
await collection.insertOne({ regex: new BSONRegExp('abc', 'imx') });
const res = await collection.findOne({ regex: new BSONRegExp('abc', 'imx') });

const db = client.db('a');
const collection = db.collection('b');
expect(res).has.property('regex').that.is.instanceOf(BSONRegExp);
} finally {
await client.close();
}
});

await collection.insertOne({ regex: new BSONRegExp('abc', 'imx') });
const res = await collection.findOne(
{ regex: new BSONRegExp('abc', 'imx') },
{ bsonRegExp: false }
);
it('should respond with BSONRegExp class with flag passed to operation', async function () {
let client;
try {
// create and connect to client
client = this.configuration.newClient(); // bsonRegex
await client.connect();

expect(res).has.property('regex').that.is.instanceOf(BSONRegExp);
const db = client.db('a');
const collection = db.collection('b');

await client.close();
await collection.insertOne({ regex: new BSONRegExp('abc', 'imx') });
const res = await collection.findOne(
{ regex: new BSONRegExp('abc', 'imx') },
{ bsonRegExp: true }
);

expect(res).has.property('regex').that.is.instanceOf(BSONRegExp);
} finally {
await client.close();
}
});
});
});

0 comments on commit d36e73c

Please sign in to comment.