Skip to content

Commit 5e6fc4c

Browse files
authored
Merge pull request #154 from share/fix-map-add-tests
Fix $map query transform, add tests for $map and $explain
2 parents 0ca1235 + 220dfbf commit 5e6fc4c

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,7 @@ var cursorOperationsMap = {
15211521
},
15221522
$map: function(cursor, fn, cb) {
15231523
cursor.map(fn)
1524+
.toArray()
15241525
.then(function(result) {
15251526
cb(null, result);
15261527
}, cb);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"mongodb6": "npm:mongodb@^6.0.0",
2121
"nyc": "^14.1.1",
2222
"ot-json1": "^1.0.1",
23+
"rich-text": "^4.1.0",
2324
"sharedb-mingo-memory": "^1.0.0 || ^2.0.0 || ^3.0.0",
2425
"sinon": "^9.2.4",
2526
"sinon-chai": "^3.7.0"

test/test_mongo.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,61 @@ describe('mongo db', function() {
100100
});
101101
});
102102

103+
it('$map maps docs with output in extra', function(done) {
104+
var snapshots = [
105+
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1}},
106+
{type: 'json0', id: 'test2', v: 1, data: {x: 2, y: 2}},
107+
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2}}
108+
];
109+
var query = {
110+
y: 2,
111+
$sort: {x: 1},
112+
$map: function(doc) {
113+
return doc.x;
114+
}
115+
};
116+
117+
var db = this.db;
118+
async.each(snapshots, function(snapshot, cb) {
119+
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
120+
}, function(err) {
121+
if (err) return done(err);
122+
db.query('testcollection', query, null, null, function(err, results, extra) {
123+
if (err) return done(err);
124+
125+
// Since $map can return non-docs, the output is delivered in extra.
126+
expect(results).eql([]);
127+
expect(extra).to.deep.equal([2, 3]);
128+
done();
129+
});
130+
});
131+
});
132+
133+
it('$explain delivers output in extra', function(done) {
134+
var snapshots = [
135+
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1}},
136+
{type: 'json0', id: 'test2', v: 1, data: {x: 2, y: 2}},
137+
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2}}
138+
];
139+
var query = {$explain: true, y: 2};
140+
141+
var db = this.db;
142+
async.each(snapshots, function(snapshot, cb) {
143+
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
144+
}, function(err) {
145+
if (err) return done(err);
146+
db.query('testcollection', query, null, null, function(err, results, extra) {
147+
if (err) return done(err);
148+
149+
expect(results).eql([]);
150+
// Just check for the presence of an explain result. The specific structure
151+
// could vary between Mongo versions.
152+
expect(extra).to.be.an('object');
153+
done();
154+
});
155+
});
156+
});
157+
103158
it('$sort, $skip and $limit should order, skip and limit', function(done) {
104159
var snapshots = [
105160
{type: 'json0', v: 1, data: {x: 1}, id: 'test1', m: null},

0 commit comments

Comments
 (0)