Skip to content

Commit

Permalink
fix(NODE-4273): pass 'comment' option through to distinct command (#3339
Browse files Browse the repository at this point in the history
)
  • Loading branch information
baileympearson committed Aug 3, 2022
1 parent c70c69e commit 753ecfe
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/operations/distinct.ts
@@ -1,9 +1,8 @@
import type { Document } from '../bson';
import type { Collection } from '../collection';
import { MongoCompatibilityError } from '../error';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { Callback, decorateWithCollation, decorateWithReadConcern, maxWireVersion } from '../utils';
import { Callback, decorateWithCollation, decorateWithReadConcern } from '../utils';
import { CommandOperation, CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

Expand Down Expand Up @@ -61,6 +60,12 @@ export class DistinctOperation extends CommandOperation<any[]> {
cmd.maxTimeMS = options.maxTimeMS;
}

// we check for undefined specifically here to allow falsy values
// eslint-disable-next-line no-restricted-syntax
if (typeof options.comment !== 'undefined') {
cmd.comment = options.comment;
}

// Do we have a readConcern specified
decorateWithReadConcern(cmd, coll, options);

Expand All @@ -71,13 +76,6 @@ export class DistinctOperation extends CommandOperation<any[]> {
return callback(err);
}

if (this.explain && maxWireVersion(server) < 4) {
callback(
new MongoCompatibilityError(`Server ${server.name} does not support explain on distinct`)
);
return;
}

super.executeCommand(server, session, cmd, (err, result) => {
if (err) {
callback(err);
Expand Down
47 changes: 46 additions & 1 deletion test/integration/node-specific/comment_with_falsy_values.test.ts
@@ -1,4 +1,6 @@
import { Long } from '../../../src';
import { expect } from 'chai';

import { Collection, CommandStartedEvent, Long, MongoClient } from '../../../src';
import { TestBuilder, UnifiedTestSuiteBuilder } from '../../tools/utils';

const falsyValues = [0, false, '', Long.ZERO, null, NaN] as const;
Expand Down Expand Up @@ -173,4 +175,47 @@ describe('Comment with falsy values', () => {
.test(testsForChangeStreamsAggregate)
.test(testsForGetMore)
.run();

context('Collection.distinct()', function () {
let client: MongoClient;
let collection: Collection;
let commands: CommandStartedEvent[] = [];

beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
client.on('commandStarted', e => commands.push(e));
await client.connect();
collection = await client.db('comment-falsy-values').createCollection('collection');
commands = [];
});

afterEach(async function () {
await collection.drop();
await client.close();
});

for (const falsyValue of falsyValues) {
it(`distinct should send falsy value ${falsyToString(
falsyValue
)} on the command`, async function () {
await collection.distinct('some-key', {}, { comment: falsyValue }).catch(() => null);

expect(commands).to.have.lengthOf(1);
const distinctCommand = commands.find(command => command.commandName === 'distinct');
expect(distinctCommand).to.exist;

// chai does not narrow types, so TS doesn't know the distinct command exists at this point.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const command = distinctCommand!.command;

expect(command).to.haveOwnProperty('comment');

if (Number.isNaN(falsyValue)) {
expect(command.comment).to.be.NaN;
} else {
expect(command.comment).to.equal(falsyValue);
}
});
}
});
});
186 changes: 186 additions & 0 deletions test/spec/crud/unified/distinct-comment.json
@@ -0,0 +1,186 @@
{
"description": "distinct-comment",
"schemaVersion": "1.0",
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "distinct-comment-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "coll0"
}
}
],
"initialData": [
{
"collectionName": "coll0",
"databaseName": "distinct-comment-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
],
"tests": [
{
"description": "distinct with document comment",
"runOnRequirements": [
{
"minServerVersion": "4.4.14"
}
],
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {},
"comment": {
"key": "value"
}
},
"expectResult": [
11,
22,
33
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {},
"comment": {
"key": "value"
}
},
"commandName": "distinct",
"databaseName": "distinct-comment-tests"
}
}
]
}
]
},
{
"description": "distinct with string comment",
"runOnRequirements": [
{
"minServerVersion": "4.4.0"
}
],
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {},
"comment": "comment"
},
"expectResult": [
11,
22,
33
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {},
"comment": "comment"
},
"commandName": "distinct",
"databaseName": "distinct-comment-tests"
}
}
]
}
]
},
{
"description": "distinct with document comment - pre 4.4, server error",
"runOnRequirements": [
{
"minServerVersion": "3.6.0",
"maxServerVersion": "4.4.13"
}
],
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {},
"comment": {
"key": "value"
}
},
"expectError": {
"isClientError": false
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {},
"comment": {
"key": "value"
}
},
"commandName": "distinct",
"databaseName": "distinct-comment-tests"
}
}
]
}
]
}
]
}
98 changes: 98 additions & 0 deletions test/spec/crud/unified/distinct-comment.yml
@@ -0,0 +1,98 @@
description: "distinct-comment"

schemaVersion: "1.0"

createEntities:
- client:
id: &client0 client0
observeEvents: [ commandStartedEvent ]
- database:
id: &database0 database0
client: *client0
databaseName: &database0Name distinct-comment-tests
- collection:
id: &collection0 collection0
database: *database0
collectionName: &collection0Name coll0

initialData:
- collectionName: *collection0Name
databaseName: *database0Name
documents:
- { _id: 1, x: 11 }
- { _id: 2, x: 22 }
- { _id: 3, x: 33 }

tests:
- description: "distinct with document comment"
runOnRequirements:
# https://jira.mongodb.org/browse/SERVER-44847
# Server supports distinct with comment of any type for comment starting from 4.4.14.
- minServerVersion: "4.4.14"
operations:
- name: distinct
object: *collection0
arguments:
fieldName: &fieldName x
filter: &filter {}
comment: &documentComment { key: "value"}
expectResult: [ 11, 22, 33 ]
expectEvents:
- client: *client0
events:
- commandStartedEvent:
command:
distinct: *collection0Name
key: *fieldName
query: *filter
comment: *documentComment
commandName: distinct
databaseName: *database0Name

- description: "distinct with string comment"
runOnRequirements:
- minServerVersion: "4.4.0"
operations:
- name: distinct
object: *collection0
arguments:
fieldName: *fieldName
filter: *filter
comment: &stringComment "comment"
expectResult: [ 11, 22, 33 ]
expectEvents:
- client: *client0
events:
- commandStartedEvent:
command:
distinct: *collection0Name
key: *fieldName
query: *filter
comment: *stringComment
commandName: distinct
databaseName: *database0Name

- description: "distinct with document comment - pre 4.4, server error"
runOnRequirements:
- minServerVersion: "3.6.0"
maxServerVersion: "4.4.13"
operations:
- name: distinct
object: *collection0
arguments:
fieldName: *fieldName
filter: *filter
comment: *documentComment
expectError:
isClientError: false
expectEvents:
- client: *client0
events:
- commandStartedEvent:
command:
distinct: *collection0Name
key: *fieldName
query: *filter
comment: *documentComment
commandName: distinct
databaseName: *database0Name

0 comments on commit 753ecfe

Please sign in to comment.