Skip to content

Commit cd0e09c

Browse files
committed
test: ensure output db exists for sharded clusters
1 parent 81e8897 commit cd0e09c

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

test/functional/mapreduce.test.js

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -364,44 +364,54 @@ describe('MapReduce', function() {
364364
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
365365
client.connect(function(err, client) {
366366
var db = client.db(configuration.db);
367+
const outDb = client.db('outputCollectionDb');
367368

368369
// Create a test collection
369370
db.createCollection('test_map_reduce_functions', function(err, collection) {
370-
// Insert some documents to perform map reduce over
371-
collection.insert(
372-
[{ user_id: 1 }, { user_id: 2 }],
373-
configuration.writeConcernMax(),
374-
function(err) {
375-
test.equal(null, err);
376-
// Map function
377-
var map = function() {
378-
emit(this.user_id, 1); // eslint-disable-line
379-
};
380-
// Reduce function
381-
var reduce = function() {
382-
return 1;
383-
};
384-
385-
// Perform the map reduce
386-
collection.mapReduce(
387-
map,
388-
reduce,
389-
{ out: { replace: 'tempCollection', db: 'outputCollectionDb' } },
390-
function(err, collection) {
391-
// Mapreduce returns the temporary collection with the results
392-
collection.findOne({ _id: 1 }, function(err, result) {
393-
test.equal(1, result.value);
394-
395-
collection.findOne({ _id: 2 }, function(err, result) {
371+
// create the output collection
372+
outDb.createCollection('tempCollection', err => {
373+
test.equal(null, err);
374+
375+
// Insert some documents to perform map reduce over
376+
collection.insert(
377+
[{ user_id: 1 }, { user_id: 2 }],
378+
configuration.writeConcernMax(),
379+
function(err) {
380+
test.equal(null, err);
381+
// Map function
382+
var map = function() {
383+
emit(this.user_id, 1); // eslint-disable-line
384+
};
385+
// Reduce function
386+
var reduce = function() {
387+
return 1;
388+
};
389+
390+
// Perform the map reduce
391+
collection.mapReduce(
392+
map,
393+
reduce,
394+
{ out: { replace: 'tempCollection', db: 'outputCollectionDb' } },
395+
function(err, collection) {
396+
test.equal(null, err);
397+
398+
// Mapreduce returns the temporary collection with the results
399+
collection.findOne({ _id: 1 }, function(err, result) {
400+
test.equal(null, err);
396401
test.equal(1, result.value);
397402

398-
client.close(done);
403+
collection.findOne({ _id: 2 }, function(err, result) {
404+
test.equal(null, err);
405+
test.equal(1, result.value);
406+
407+
client.close(done);
408+
});
399409
});
400-
});
401-
}
402-
);
403-
}
404-
);
410+
}
411+
);
412+
}
413+
);
414+
});
405415
});
406416
});
407417
}

0 commit comments

Comments
 (0)