Skip to content

Commit fa77088

Browse files
committed
fix: Use session getter instead of kSession
1 parent 083b029 commit fa77088

File tree

1 file changed

+110
-123
lines changed

1 file changed

+110
-123
lines changed

test/functional/cursor.test.js

Lines changed: 110 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const { ReadPreference } = require('../../src/read_preference');
1212
const { ServerType } = require('../../src/sdam/common');
1313
const { formatSort } = require('../../src/sort');
1414
const { FindCursor } = require('../../src/cursor/find_cursor');
15-
const kSession = Symbol('session');
1615

1716
describe('Cursor', function () {
1817
before(function () {
@@ -109,6 +108,7 @@ describe('Cursor', function () {
109108
}
110109
});
111110

111+
112112
it('cursor should trigger getMore', {
113113
// Add a tag that our runner can trigger on
114114
// in this case we are setting that node needs to be higher than 0.10.X to run
@@ -3785,7 +3785,7 @@ describe('Cursor', function () {
37853785
expect(doc).to.exist;
37863786
const clonedCursor = cursor.clone();
37873787
expect(clonedCursor.cursorOptions.session).to.not.exist;
3788-
expect(clonedCursor[kSession]).to.not.exist;
3788+
expect(clonedCursor.session).to.not.exist;
37893789
})
37903790
.finally(() => {
37913791
return cursor.close();
@@ -3805,7 +3805,7 @@ describe('Cursor', function () {
38053805
expect(doc).to.exist;
38063806
const clonedCursor = cursor.clone();
38073807
expect(clonedCursor.cursorOptions.session).to.not.exist;
3808-
expect(clonedCursor[kSession]).to.not.exist;
3808+
expect(clonedCursor.session).to.not.exist;
38093809
})
38103810
.finally(() => {
38113811
return cursor.close();
@@ -3914,66 +3914,6 @@ describe('Cursor', function () {
39143914
const expectedDocs = [
39153915
{ _id: 0, b: 1, c: 0 },
39163916
{ _id: 1, b: 1, c: 0 },
3917-
{ _id: 2, b: 1, c: 0 }
3918-
];
3919-
const config = {
3920-
client: client,
3921-
configuration: configuration,
3922-
collectionName: 'stream-test-transform',
3923-
transformFunc: doc => ({ _id: doc._id, b: doc.a.b, c: doc.a.c }),
3924-
expectedSet: new Set(expectedDocs)
3925-
};
3926-
3927-
testTransformStream(config, done);
3928-
});
3929-
3930-
it('stream should return a stream of unmodified docs if no transform function applied', function (done) {
3931-
const configuration = this.configuration;
3932-
const client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
3933-
const expectedDocs = [
3934-
{ _id: 0, a: { b: 1, c: 0 } },
3935-
{ _id: 1, a: { b: 1, c: 0 } },
3936-
{ _id: 2, a: { b: 1, c: 0 } }
3937-
];
3938-
const config = {
3939-
client: client,
3940-
configuration: configuration,
3941-
collectionName: 'transformStream-test-notransform',
3942-
transformFunc: null,
3943-
expectedSet: new Set(expectedDocs)
3944-
};
3945-
3946-
testTransformStream(config, done);
3947-
});
3948-
3949-
it.skip('should apply parent read preference to count command', function (done) {
3950-
// NOTE: this test is skipped because mongo orchestration does not test sharded clusters
3951-
// with secondaries. This behavior should be unit tested
3952-
3953-
const configuration = this.configuration;
3954-
const client = configuration.newClient(
3955-
{ w: 1, readPreference: ReadPreference.SECONDARY },
3956-
{ maxPoolSize: 1, connectWithNoPrimary: true }
3957-
);
3958-
3959-
client.connect((err, client) => {
3960-
expect(err).to.not.exist;
3961-
this.defer(() => client.close());
3962-
3963-
const db = client.db(configuration.db);
3964-
let collection, cursor, spy;
3965-
const close = e => cursor.close(() => client.close(() => done(e)));
3966-
3967-
Promise.resolve()
3968-
.then(() => new Promise(resolve => setTimeout(() => resolve(), 500)))
3969-
.then(() => db.createCollection('test_count_readPreference'))
3970-
.then(() => (collection = db.collection('test_count_readPreference')))
3971-
.then(() => collection.find())
3972-
.then(_cursor => (cursor = _cursor))
3973-
.then(() => (spy = sinon.spy(cursor.topology, 'command')))
3974-
.then(() => cursor.count())
3975-
.then(() =>
3976-
expect(spy.firstCall.args[2])
39773917
.to.have.nested.property('readPreference.mode')
39783918
.that.equals('secondary')
39793919
)
@@ -4025,6 +3965,53 @@ describe('Cursor', function () {
40253965
});
40263966
});
40273967

3968+
describe('#stream', function () {
3969+
context('when the stream is closed', function () {
3970+
it('emits the close event once only', function () {
3971+
const configuration = this.configuration;
3972+
const client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
3973+
3974+
return client.connect(err => {
3975+
expect(err).to.not.exist;
3976+
this.defer(() => client.close());
3977+
3978+
const collection = client.db().collection('documents');
3979+
collection.drop(() => {
3980+
const docs = [{ a: 1 }, { a: 2 }, { a: 3 }];
3981+
collection.insertMany(docs, err => {
3982+
expect(err).to.not.exist;
3983+
3984+
const cursor = collection.find({}, { sort: { a: 1 } });
3985+
cursor.hasNext((err, hasNext) => {
3986+
expect(err).to.not.exist;
3987+
expect(hasNext).to.be.true;
3988+
3989+
const collected = [];
3990+
const stream = new Writable({
3991+
objectMode: true,
3992+
write: (chunk, encoding, next) => {
3993+
collected.push(chunk);
3994+
next(undefined, chunk);
3995+
}
3996+
});
3997+
3998+
const cursorStream = cursor.stream();
3999+
4000+
cursorStream.on('end', () => {
4001+
expect(collected).to.have.length(3);
4002+
expect(collected).to.eql(docs);
4003+
done();
4004+
});
4005+
4006+
cursorStream.pipe(stream);
4007+
});
4008+
});
4009+
});
4010+
});
4011+
});
4012+
});
4013+
});
4014+
40284015
describe('transforms', function () {
40294016
it('should correctly apply map transform to cursor as readable stream', function (done) {
40304017
const configuration = this.configuration;
@@ -4083,24 +4070,70 @@ describe('Cursor', function () {
40834070
context('sort', function () {
40844071
const findSort = (input, output) =>
40854072
withMonitoredClient('find', function (client, events, done) {
4086-
const db = client.db('test');
4087-
db.collection('test_sort_dos', (err, collection) => {
4073+
});
4074+
4075+
describe('transforms', function () {
4076+
it('should correctly apply map transform to cursor as readable stream', function (done) {
4077+
const configuration = this.configuration;
4078+
const client = configuration.newClient();
4079+
client.connect(err => {
4080+
expect(err).to.not.exist;
4081+
this.defer(() => client.close());
4082+
4083+
const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x }));
4084+
const coll = client.db(configuration.db).collection('cursor_stream_mapping');
4085+
coll.insertMany(docs, err => {
40884086
expect(err).to.not.exist;
4089-
const cursor = collection.find({}, { sort: input });
4090-
cursor.next(err => {
4091-
expect(err).to.not.exist;
4092-
expect(events[0].command.sort).to.deep.equal(output);
4093-
cursor.close(done);
4087+
4088+
const bag = [];
4089+
const stream = coll
4090+
.find()
4091+
.project({ _id: 0, name: 1 })
4092+
.map(doc => ({ mapped: doc }))
4093+
.stream()
4094+
.on('data', doc => bag.push(doc));
4095+
4096+
stream.on('error', done).on('end', () => {
4097+
expect(bag.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name })));
4098+
done();
40944099
});
40954100
});
40964101
});
4102+
});
40974103

4098-
const cursorSort = (input, output) =>
4104+
it('should correctly apply map transform when converting cursor to array', function (done) {
4105+
const configuration = this.configuration;
4106+
const client = configuration.newClient();
4107+
client.connect(err => {
4108+
expect(err).to.not.exist;
4109+
this.defer(() => client.close());
4110+
4111+
const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x }));
4112+
const coll = client.db(configuration.db).collection('cursor_toArray_mapping');
4113+
coll.insertMany(docs, err => {
4114+
expect(err).to.not.exist;
4115+
4116+
coll
4117+
.find()
4118+
.project({ _id: 0, name: 1 })
4119+
.map(doc => ({ mapped: doc }))
4120+
.toArray((err, mappedDocs) => {
4121+
expect(err).to.not.exist;
4122+
expect(mappedDocs.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name })));
4123+
done();
4124+
});
4125+
});
4126+
});
4127+
});
4128+
});
4129+
4130+
context('sort', function () {
4131+
const findSort = (input, output) =>
40994132
withMonitoredClient('find', function (client, events, done) {
41004133
const db = client.db('test');
41014134
db.collection('test_sort_dos', (err, collection) => {
41024135
expect(err).to.not.exist;
4103-
const cursor = collection.find({}).sort(input);
4136+
const cursor = collection.find({}, { sort: input });
41044137
cursor.next(err => {
41054138
expect(err).to.not.exist;
41064139
expect(events[0].command.sort).to.deep.equal(output);
@@ -4109,61 +4142,15 @@ describe('Cursor', function () {
41094142
});
41104143
});
41114144

4112-
it('should use find options object', findSort({ alpha: 1 }, { alpha: 1 }));
4113-
it('should use find options string', findSort('alpha', { alpha: 1 }));
4114-
it('should use find options shallow array', findSort(['alpha', 1], { alpha: 1 }));
4115-
it('should use find options deep array', findSort([['alpha', 1]], { alpha: 1 }));
4116-
4117-
it('should use cursor.sort object', cursorSort({ alpha: 1 }, { alpha: 1 }));
4118-
it('should use cursor.sort string', cursorSort('alpha', { alpha: 1 }));
4119-
it('should use cursor.sort shallow array', cursorSort(['alpha', 1], { alpha: 1 }));
4120-
it('should use cursor.sort deep array', cursorSort([['alpha', 1]], { alpha: 1 }));
4121-
4122-
it('formatSort - one key', () => {
4123-
expect(formatSort('alpha')).to.deep.equal({ alpha: 1 });
4124-
expect(formatSort(['alpha'])).to.deep.equal({ alpha: 1 });
4125-
expect(formatSort('alpha', 1)).to.deep.equal({ alpha: 1 });
4126-
expect(formatSort('alpha', 'asc')).to.deep.equal({ alpha: 1 });
4127-
expect(formatSort([['alpha', 'asc']])).to.deep.equal({ alpha: 1 });
4128-
expect(formatSort('alpha', 'ascending')).to.deep.equal({ alpha: 1 });
4129-
expect(formatSort({ alpha: 1 })).to.deep.equal({ alpha: 1 });
4130-
expect(formatSort('beta')).to.deep.equal({ beta: 1 });
4131-
expect(formatSort(['beta'])).to.deep.equal({ beta: 1 });
4132-
expect(formatSort('beta', -1)).to.deep.equal({ beta: -1 });
4133-
expect(formatSort('beta', 'desc')).to.deep.equal({ beta: -1 });
4134-
expect(formatSort('beta', 'descending')).to.deep.equal({ beta: -1 });
4135-
expect(formatSort({ beta: -1 })).to.deep.equal({ beta: -1 });
4136-
expect(formatSort({ alpha: { $meta: 'hi' } })).to.deep.equal({
4137-
alpha: { $meta: 'hi' }
4138-
});
4139-
});
4140-
4141-
it('formatSort - multi key', () => {
4142-
expect(formatSort(['alpha', 'beta'])).to.deep.equal({ alpha: 1, beta: 1 });
4143-
expect(formatSort({ alpha: 1, beta: 1 })).to.deep.equal({ alpha: 1, beta: 1 });
4144-
expect(
4145-
formatSort([
4146-
['alpha', 'asc'],
4147-
['beta', 'ascending']
4148-
])
4149-
).to.deep.equal({ alpha: 1, beta: 1 });
4150-
expect(formatSort({ alpha: { $meta: 'hi' }, beta: 'ascending' })).to.deep.equal({
4151-
alpha: { $meta: 'hi' },
4152-
beta: 1
4153-
});
4154-
});
4155-
4156-
it('should use allowDiskUse option on sort', {
4157-
metadata: { requires: { mongodb: '>=4.4' } },
4158-
test: withMonitoredClient('find', function (client, events, done) {
4145+
const cursorSort = (input, output) =>
4146+
withMonitoredClient('find', function (client, events, done) {
41594147
const db = client.db('test');
4160-
db.collection('test_sort_allow_disk_use', (err, collection) => {
4148+
db.collection('test_sort_dos', (err, collection) => {
41614149
expect(err).to.not.exist;
4162-
const cursor = collection.find({}).sort(['alpha', 1]).allowDiskUse();
4150+
const cursor = collection.find({}).sort(input);
41634151
cursor.next(err => {
41644152
expect(err).to.not.exist;
4165-
const { command } = events.shift();
4166-
expect(command.sort).to.deep.equal({ alpha: 1 });
4153+
expect(events[0].command.sort).to.deep.equal(output);
41674154
expect(command.allowDiskUse).to.be.true;
41684155
cursor.close(done);
41694156
});

0 commit comments

Comments
 (0)