From 98edccd4cf73c18b7c445ef565ef836c4645e346 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Wed, 27 May 2015 18:12:40 +0200 Subject: [PATCH] Added tests and fixes for cursor results with empty result sets --- lib/Cursor.ts | 1 + test/Model.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/Cursor.ts b/lib/Cursor.ts index 716e5c5..f1378e0 100644 --- a/lib/Cursor.ts +++ b/lib/Cursor.ts @@ -100,6 +100,7 @@ class Cursor { return resolve(result); }); }).then((document) => { + if (!document) return Bluebird.resolve(null); return this.model.handlers.documentReceived(this.conditions, document,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial)); }).nodeify(callback); } diff --git a/test/Model.ts b/test/Model.ts index 4f6b271..5e93c21 100644 --- a/test/Model.ts +++ b/test/Model.ts @@ -437,6 +437,12 @@ describe("Model",() => { count++; }).then(() => chai.expect(count).to.not.equal(5)).then(() => Promise.delay(10)).then(() => count)).to.eventually.equal(5); }); + + it("should be capable of functioning correctly with empty result sets",() => { + return chai.expect(model.find({ nothing: true }).forEach((instance) => { + chai.assert.fail(); + })).to.eventually.not.be.rejected; + }); it("should support using callbacks instead of promises",(done) => { var count = 0; @@ -471,6 +477,12 @@ describe("Model",() => { return count++; }).then(() => count)).to.eventually.equal(5); }); + + it("should be capable of functioning correctly with empty result sets",() => { + return chai.expect(model.find({ nothing: true }).map((instance) => { + chai.assert.fail(); + })).to.eventually.eql([]); + }); it("should support using callbacks instead of promises",(done) => { var count = 0; @@ -486,6 +498,10 @@ describe("Model",() => { it("should return all documents",() => { return chai.expect(model.find().toArray()).to.eventually.exist.and.have.length(5); }); + + it("should be capable of functioning correctly with empty result sets",() => { + return chai.expect(model.find({ nothing: true }).toArray()).to.eventually.eql([]); + }); it("should support a callback style instead of promises",(done) => { model.find().toArray((err, docs) => { @@ -504,6 +520,10 @@ describe("Model",() => { it("which should resolve to the next instance in the query",() => { return chai.expect(model.find().next()).to.eventually.be.an.instanceof(model.Instance); }); + + it("should be capable of functioning correctly with empty result sets",() => { + return chai.expect(model.find({ nothing: true }).next()).to.eventually.not.exist; + }); it("should support using callbacks instead of promises",(done) => { model.find().next((err, instance) => {