Skip to content

Commit

Permalink
Added tests and fixes for cursor results with empty result sets
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed May 27, 2015
1 parent 85a79f1 commit 98edccd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Cursor<TDocument extends { _id?: any }, TInstance> {
return resolve(<any>result);
});
}).then((document) => {
if (!document) return Bluebird.resolve(<TInstance>null);
return this.model.handlers.documentReceived(this.conditions, document,(document, isNew?, isPartial?) => this.model.helpers.wrapDocument(document, isNew, isPartial));
}).nodeify(callback);
}
Expand Down
20 changes: 20 additions & 0 deletions test/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down

0 comments on commit 98edccd

Please sign in to comment.