Skip to content

Commit

Permalink
Merge pull request #124 from williamkapke/sift-11.1.8
Browse files Browse the repository at this point in the history
update sift to v11.1.8 - Fixes #123
  • Loading branch information
williamkapke committed Mar 13, 2020
2 parents 075991a + abe104f commit 60af4db
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
12 changes: 9 additions & 3 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,13 @@ module.exports = function Collection(db, state) {
debug('%s.%s %j', name, action, selector);

asyncish(function () {
var docs = (options.multi ? sift : first)(selector, state.documents || []) || [];
var docs = state.documents || [];
if (options.multi) {
docs = docs.filter(sift(selector))
}
else {
docs = first(selector, docs) || []
}
if (!Array.isArray(docs)) docs = [docs];
debug('%s.%s %j', name, action, docs);

Expand Down Expand Up @@ -376,7 +382,7 @@ module.exports = function Collection(db, state) {
debug('%s.%s %j', name, action, selector);

asyncish(function () {
var docs = sift(selector, state.documents || []) || [];
var docs = (state.documents || []).filter(sift(selector));
if (!Array.isArray(docs)) docs = [docs];
debug('%s.%s %j', name, action, docs);

Expand Down Expand Up @@ -602,7 +608,7 @@ function upsertClone (selector, data) {
}

function first(query, collection) {
return collection[sift.indexOf(query, collection)];
return collection[collection.findIndex(sift(query))];
}

function count() {
Expand Down
2 changes: 1 addition & 1 deletion lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Cursor = module.exports = function(documents, opts) {

function getDocs(applySkipLimit) {
state = Cursor.OPEN;
var docs = sift(opts.query, documents);
var docs = documents.filter(sift(opts.query));
if (opts.sort) {
// partial implementation of mongodb sorting
// https://docs.mongodb.com/manual/reference/bson-type-comparison-order/
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "William Kapke",
"name": "mongo-mock",
"version": "3.9.1",
"version": "4.0.0",
"description": "Let's pretend we have a real MongoDB",
"main": "mongo-mock.js",
"scripts": {
Expand All @@ -26,7 +26,7 @@
"lodash": "^4.17.13",
"modifyjs": "^0.3.1",
"object-assign-deep": "^0.4.0",
"sift": "^3.3.12"
"sift": "^11.1.8"
},
"devDependencies": {
"mocha": "^5",
Expand Down
47 changes: 47 additions & 0 deletions test/mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,53 @@ describe('mock tests', function () {
});
})

it('should find document where nulled property exists', function (done) {
collection.insert({_id: 'g5f6h2df6g46j', a: true, b: null}, function (errInsert, resultInsert) {
if(errInsert) return done(errInsert);
(!!resultInsert.ops).should.be.true;
collection.find({b:{$exists: true}}).toArray(function (errFind, resultFind) {
if (errFind) return done(errFind);
collection.remove({_id: 'g5f6h2df6g46j'}, function (errRemove) {
if (errRemove) return done(errRemove);
resultFind.length.should.equal(1);
resultFind[0].should.have.property("b", null);
done();
});
});
});
})

it('should not find document where property does not exists', function (done) {
collection.insert({_id: 'weg8h7rt6h5weg69', a: 37}, function (errInsert, resultInsert) {
if(errInsert) return done(errInsert);
(!!resultInsert.ops).should.be.true;
collection.find({_id: 'weg8h7rt6h5weg69', b:{$exists: false}}).toArray(function (errFind, resultFind) {
if (errFind) return done(errFind);
collection.remove({_id: 'weg8h7rt6h5weg69'}, function (errRemove) {
if (errRemove) return done(errRemove);
resultFind.length.should.equal(1);
resultFind[0].should.have.property("a", 37);
done();
});
});
});
})

it('should find document with nulled property and exists false', function (done) {
collection.insert({_id: 'iuk51hf34', a: true, b: null}, function (errInsert, resultInsert) {
if(errInsert) return done(errInsert);
(!!resultInsert.ops).should.be.true;
collection.find({_id: 'iuk51hf34', b:{$exists: false}}).toArray(function (errFind, resultFind) {
if (errFind) return done(errFind);
collection.remove({_id: 'iuk51hf34'}, function (errRemove) {
if (errRemove) return done(errRemove);
resultFind.length.should.equal(0);
done();
});
});
});
})

it('should update one (updateOne)', function (done) {
//query, data, options, callback
collection.updateOne({test:123}, { $set: { foo: { bar: "buzz", fang: "dang" } } }, function (err, opResult) {
Expand Down

0 comments on commit 60af4db

Please sign in to comment.