Skip to content

Commit

Permalink
fix(cursor) remove deprecated Cursor.nextObject method and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Hallum Clarke authored and mbroadst committed Aug 8, 2017
1 parent 1f8a184 commit 0b43abc
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 228 deletions.
1 change: 1 addition & 0 deletions CHANGES_3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ We removed the following API methods.
- Admin.prototype.profilingLevel
- Admin.prototype.setProfilingLevel
- Admin.prototype.profilingInfo
- Cursor.prototype.nextObject

We've added the following API methods.
- MongoClient.prototype.logout
Expand Down
19 changes: 2 additions & 17 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,16 +655,7 @@ define.classMethod('skip', {callback: false, promise:false, returns: [Cursor]});
* @return {null}
*/

/**
* Get the next available document from the cursor, returns null if no more documents are available.
* @method
* @param {Cursor~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @deprecated
* @return {Promise} returns Promise if no callback passed
*/
Cursor.prototype.nextObject = Cursor.prototype.next;

// Get the next available document from the cursor, returns null if no more documents are available.
var nextObject = function(self, callback) {
if(self.s.state == Cursor.CLOSED || self.isDead && self.isDead()) return handleCallback(callback, MongoError.create({message: "Cursor is closed", driver:true}));
if(self.s.state == Cursor.INIT && self.s.cmd.sort) {
Expand All @@ -683,8 +674,6 @@ var nextObject = function(self, callback) {
});
}

define.classMethod('nextObject', {callback: true, promise:true});

// Trampoline emptying the number of retrieved items
// without incurring a nextTick operation
var loop = function(self, callback) {
Expand All @@ -696,10 +685,6 @@ var loop = function(self, callback) {
return loop;
}

Cursor.prototype.next = Cursor.prototype.nextObject;

define.classMethod('next', {callback: true, promise:true});

/**
* Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
* not all of the elements will be iterated if this cursor had been previouly accessed.
Expand Down Expand Up @@ -1083,7 +1068,7 @@ Cursor.prototype._read = function() {
}

// Get the next item
self.nextObject(function(err, result) {
self.next(function(err, result) {
if(err) {
if(self.listeners('error') && self.listeners('error').length > 0) {
self.emit('error', err);
Expand Down
22 changes: 0 additions & 22 deletions test/functional/crud_api_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,6 @@ exports['should correctly execute find method using crud api'] = {
test.ok(doc != null);

clonedCursor.next(function(err, doc) {
test.equal(null, err);
test.equal(null, doc);
nextObjectMethod();
});
});
});
}

//
// Exercise nextObject legacy method
// -------------------------------------------------
var nextObjectMethod = function() {
var clonedCursor = cursor.clone();
clonedCursor.nextObject(function(err, doc) {
test.equal(null, err);
test.ok(doc != null);

clonedCursor.nextObject(function(err, doc) {
test.equal(null, err);
test.ok(doc != null);

clonedCursor.nextObject(function(err, doc) {
test.equal(null, err);
test.equal(null, doc);
streamMethod();
Expand Down
111 changes: 32 additions & 79 deletions test/functional/cursor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,19 @@ exports.shouldCorrectlyExecuteSortOnCursor = {
test.deepEqual([['a', -1]], cursor.sortValue);finished();

var cursor = collection.find();
cursor.nextObject(function(err, doc) {
cursor.next(function(err, doc) {
try {
cursor.sort(['a']);
} catch(err) {
test.equal("Cursor is closed", err.message);finished();
}
});

collection.find().sort('a', 25).nextObject(function(err, doc) {
collection.find().sort('a', 25).next(function(err, doc) {
test.equal("Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]", err.message);finished();
});

collection.find().sort(25).nextObject(function(err, doc) {
collection.find().sort(25).next(function(err, doc) {
test.equal("Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]", err.message);finished();
});
}
Expand Down Expand Up @@ -584,7 +584,7 @@ exports.shouldCorrectlyReturnErrorsOnIllegalLimitValues = {
}

collection.find(function(err, cursor) {
cursor.nextObject(function(err, doc) {
cursor.next(function(err, doc) {
try {
cursor.limit(1);
} catch(err) {
Expand Down Expand Up @@ -705,7 +705,7 @@ exports.shouldCorrectlyReturnErrorsOnIllegalSkipValues = {
}

var cursor = collection.find()
cursor.nextObject(function(err, doc) {
cursor.next(function(err, doc) {
try {
cursor.skip(1);
} catch(err) {
Expand Down Expand Up @@ -755,8 +755,8 @@ exports.shouldReturnErrorsOnIllegalBatchSizes = {
}

var cursor = collection.find();
cursor.nextObject(function(err, doc) {
cursor.nextObject(function(err, doc) {
cursor.next(function(err, doc) {
cursor.next(function(err, doc) {
try {
cursor.batchSize(1);
test.ok(false);
Expand Down Expand Up @@ -808,13 +808,13 @@ exports.shouldCorrectlyHandleChangesInBatchSizes = {
collection.insert(docs, configuration.writeConcernMax(), function() {
collection.find({}, {batchSize : batchSize}, function(err, cursor) {
//1st
cursor.nextObject(function(err, items) {
//cursor.items should contain 1 since nextObject already popped one
cursor.next(function(err, items) {
//cursor.items should contain 1 since next already popped one
test.equal(1, cursor.bufferedCount());
test.ok(items != null);

//2nd
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(0, cursor.bufferedCount());
test.ok(items != null);

Expand All @@ -823,27 +823,27 @@ exports.shouldCorrectlyHandleChangesInBatchSizes = {
cursor.batchSize(batchSize);

//3rd
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(2, cursor.bufferedCount());
test.ok(items != null);

//4th
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(1, cursor.bufferedCount());
test.ok(items != null);

//5th
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(0, cursor.bufferedCount());
test.ok(items != null);

//6th
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(0, cursor.bufferedCount());
test.ok(items != null);

//No more
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());

Expand Down Expand Up @@ -889,27 +889,27 @@ exports.shouldCorrectlyHandleBatchSize = {
collection.insert(docs, configuration.writeConcernMax(), function() {
collection.find({}, {batchSize : batchSize}, function(err, cursor) {
//1st
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(1, cursor.bufferedCount());
test.ok(items != null);

//2nd
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(0, cursor.bufferedCount());
test.ok(items != null);

//3rd
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(1, cursor.bufferedCount());
test.ok(items != null);

//4th
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(0, cursor.bufferedCount());
test.ok(items != null);

//No more
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());

Expand Down Expand Up @@ -953,23 +953,23 @@ exports.shouldHandleWhenLimitBiggerThanBatchSize = {
collection.insert(docs, configuration.writeConcernMax(), function() {
var cursor = collection.find({}, {batchSize : batchSize, limit : limit});
//1st
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(2, cursor.bufferedCount());

//2nd
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(1, cursor.bufferedCount());

//3rd
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(0, cursor.bufferedCount());

//4th
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(null, err);

//No more
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());

Expand Down Expand Up @@ -1012,15 +1012,15 @@ exports.shouldHandleLimitLessThanBatchSize = {
collection.insert(docs, configuration.writeConcernMax(), function() {
var cursor = collection.find({}, {batchSize : batchSize, limit : limit});
//1st
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(1, cursor.bufferedCount());

//2nd
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.equal(0, cursor.bufferedCount());

//No more
cursor.nextObject(function(err, items) {
cursor.next(function(err, items) {
test.ok(items == null);
test.ok(cursor.isClosed());

Expand Down Expand Up @@ -1343,7 +1343,7 @@ exports.shouldCloseCursorAfterQueryHasBeenSent = {
db.createCollection('test_close_after_query_sent', function(err, collection) {
collection.insert({'a':1}, configuration.writeConcernMax(), function(err, r) {
var cursor = collection.find({'a':1});
cursor.nextObject(function(err, item) {
cursor.next(function(err, item) {
cursor.close(function(err, cursor) {
test.equal(true, cursor.isClosed());
// Let's close the db
Expand Down Expand Up @@ -2699,14 +2699,14 @@ exports['Should correctly handle batchSize of 2'] = {
db.collection('should_correctly_handle_batchSize_2').find({}, {batchSize: 2}, function(error, cursor) {
test.equal(null, err);

cursor.nextObject(function(err, obj) {
cursor.next(function(err, obj) {
test.equal(null, err);
client.close();

cursor.nextObject(function(err, obj) {
cursor.next(function(err, obj) {
test.equal(null, err);

cursor.nextObject(function(err, obj) {
cursor.next(function(err, obj) {
test.ok(err != null);
test.done();
});
Expand Down Expand Up @@ -2941,53 +2941,6 @@ exports['Should correctly apply map to next'] = {
}
}

/**
* @ignore
* @api private
*/
exports['Should correctly apply map to nextObject'] = {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var docs = [];

for(var i = 0; i < 1000; i++) {
var d = new Date().getTime() + i*1000;
docs[i] = {'a':i, createdAt:new Date(d)};
}

var client = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
client.connect(function(err, client) {
var db = client.db(configuration.database);
test.equal(null, err);

var collection = db.collection('map_nextObject');

// insert all docs
collection.insert(docs, configuration.writeConcernMax(), function(err, result) {
test.equal(null, err);
var total = 0;

// Create a cursor for the content
var cursor = collection.find({})
.map(function(x) { return {a:1}; })
.batchSize(5)
.limit(10);
cursor.nextObject(function(err, doc) {
test.equal(null, err);
test.equal(1, doc.a);

client.close();
test.done();
});
})
});
}
}

/**
* @ignore
* @api private
Expand Down
2 changes: 1 addition & 1 deletion test/functional/error_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ exports.shouldCorrectlyHandleExceptionsInCursorNext = {
var db = client.db(configuration.database);
var col = db.collection('shouldCorrectlyHandleExceptionsInCursorNext');
col.insert({a:1}, function(err, result) {
col.find().nextObject(function(err, result) {
col.find().next(function(err, result) {
boom
});
});
Expand Down
Loading

0 comments on commit 0b43abc

Please sign in to comment.