Skip to content

Commit

Permalink
Removed extraneous defaults in ModelSpecificInstance constructor gene…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
notheotherben committed Apr 27, 2015
1 parent a47e8bb commit f6b21a7
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 99 deletions.
2 changes: 0 additions & 2 deletions lib/ModelSpecificInstance.js

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

2 changes: 1 addition & 1 deletion lib/ModelSpecificInstance.js.map

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

2 changes: 1 addition & 1 deletion lib/ModelSpecificInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import _ = require('lodash');
export = ModelSpecificInstance;

function ModelSpecificInstance<TDocument extends { _id?: any }, TInstance>(model: Model<TDocument, TInstance>, instanceType: ModelInterfaces.InstanceConstructor<TDocument, TInstance>): new (doc: TDocument, isNew?: boolean, isPartial?: boolean) => TInstance {
var constructor = function (doc: TDocument, isNew: boolean = true, isPartial: boolean = false) {
var constructor = function (doc: TDocument, isNew?: boolean, isPartial?: boolean) {
instanceType.call(this, model, doc, isNew, isPartial);
};

Expand Down
2 changes: 1 addition & 1 deletion test/Instance.js.map

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions test/Instance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="../_references.d.ts" />
import Iridium = require('../index');
import MongoDB = require('mongodb');

interface TestDocument {
_id?: string;
Expand Down Expand Up @@ -44,6 +45,22 @@ describe("Instance",() => {

beforeEach(() => core.Test.remove());

it("should default to isNew",() => {
var instance = new core.Test.Instance({
answer: 42
});

chai.expect(instance).to.have.property("_isNew", true);
});

it("should default to !isPartial",() => {
var instance = new core.Test.Instance({
answer: 42
});

chai.expect(instance).to.have.property("_isPartial", false);
});

it("should expose the latest document values",() => {
var instance = core.Test.helpers.wrapDocument({
_id: 'aaaaaa',
Expand Down Expand Up @@ -111,6 +128,25 @@ describe("Instance",() => {
chai.expect(instance.test).to.exist.and.be.a('function');
});

describe("should handle _id in a special manner",() => {
beforeEach(() => core.Test.remove().then(() => core.Test.insert({ answer: 42 })));
afterEach(() => core.Test.remove());

it("get should transform ObjectIDs into hex strings",() => {
return core.Test.get().then(instance => {
chai.expect((<any>instance.document._id)._bsontype).to.equal('ObjectID');
chai.expect(instance._id).to.be.a('string').with.length(24);
});
});

it("set should transform hex strings into ObjectIDs by default",() => {
return core.Test.get().then(instance => {
instance._id = "aaaaaaaaaaaaaaaaaaaaaaaa";
chai.expect(new MongoDB.ObjectID(instance.document._id).toHexString()).to.equal('aaaaaaaaaaaaaaaaaaaaaaaa');
});
});
});

describe("save()",() => {

beforeEach(() => core.Test.remove());
Expand Down
75 changes: 69 additions & 6 deletions test/Model.js

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

2 changes: 1 addition & 1 deletion test/Model.js.map

Large diffs are not rendered by default.

84 changes: 78 additions & 6 deletions test/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,14 @@ describe("Model",() => {
return chai.expect(model.remove({ answer: 10 })).to.eventually.equal(1);
});

it("should allow just the ID to be specified",() => {
return model.get().then(instance => {
return chai.expect(model.remove(instance._id)).to.eventually.exist.and.equal(1);
});
});

it("should allow the removal of all documents",() => {
return chai.expect(model.remove()).to.eventually.equal(4);
return chai.expect(model.remove()).to.eventually.equal(3);
});

it("should support a callback style instead of promises",(done) => {
Expand Down Expand Up @@ -597,6 +603,12 @@ describe("Model",() => {
return chai.expect(model.count()).to.eventually.exist.and.equal(5);
});

it("should allow just the ID to be specified",() => {
return model.get().then(instance => {
return chai.expect(model.count(instance._id)).to.eventually.exist.and.equal(1);
});
});

it("should allow filtering using a selector",() => {
return chai.expect(model.count({ answer: 10 })).to.eventually.exist.and.equal(1);
});
Expand All @@ -610,10 +622,10 @@ describe("Model",() => {
});
});

describe("ensureIndex()",() => {
describe("update()",() => {
var model = new Iridium.Model<TestDocument, Test>(core, Test, 'test', { _id: false, answer: Number });

before(() => {
beforeEach(() => {
return core.connect().then(() => model.remove()).then(() => model.insert([
{ answer: 10 },
{ answer: 11 },
Expand All @@ -624,6 +636,50 @@ describe("Model",() => {
});

after(() => {
return model.remove().then(() => core.close());
});

it("should exist",() => {
chai.expect(model.update).to.exist.and.be.a('function');
});

it("should use multi update by default",() => {
return chai.expect(model.update({ _id: { $exists: true } }, { $inc: { answer: 1 } })).to.eventually.exist.and.equal(5);
});

it("should allow just the ID to be specified",() => {
return model.get().then(instance => {
return chai.expect(model.update(instance._id, { $inc: { answer: 1 } })).to.eventually.exist.and.equal(1);
});
});

it("should allow filtering using a selector",() => {
return chai.expect(model.update({ answer: 10 }, { $inc: { answer: 1 } })).to.eventually.exist.and.equal(1);
});

it("should support a callback style instead of promises",(done) => {
model.update({}, { $inc: { answer: 1 } }, (err, docs) => {
if (err) return done(err);
chai.expect(docs).to.exist.and.equal(5);
return done();
});
});
});

describe("ensureIndex()",() => {
var model = new Iridium.Model<TestDocument, Test>(core, Test, 'test', { _id: false, answer: Number });

beforeEach(() => {
return core.connect().then(() => model.remove()).then(() => model.insert([
{ answer: 10 },
{ answer: 11 },
{ answer: 12 },
{ answer: 13 },
{ answer: 14 }
]));
});

afterEach(() => {
return model.remove().then(() => model.dropIndexes()).then(() => core.close());
});

Expand All @@ -634,6 +690,14 @@ describe("Model",() => {
it("should allow the creation of indexes",() => {
return chai.expect(model.ensureIndex({ answer: 1 }, { unique: true })).to.eventually.exist;
});

it("should allow the use of callbacks instead of promises",(done) => {
model.ensureIndex({ answer: 1 },(err, index) => {
if (err) return done(err);
chai.expect(index).to.exist;
return done();
});
});
});

describe("ensureIndexes()",() => {
Expand Down Expand Up @@ -686,15 +750,23 @@ describe("Model",() => {
});

it("should remove the specified index",() => {
return chai.expect(model.dropIndex('answer_1')).to.eventually.be.ok;
return chai.expect(model.dropIndex('answer_1')).to.eventually.be.true;
});

it("should remove the specified index using its definition",() => {
return chai.expect(model.dropIndex({ answer: 1 })).to.eventually.be.ok;
return chai.expect(model.dropIndex({ answer: 1 })).to.eventually.be.true;
});

it("should support removing a compound indexe using its definition",() => {
return chai.expect(model.ensureIndex({ _id: 1, answer: 1 }).then(() => model.dropIndex({ _id: 1, answer: 1 }))).to.eventually.be.ok;
return chai.expect(model.ensureIndex({ _id: 1, answer: 1 }).then(() => model.dropIndex({ _id: 1, answer: 1 }))).to.eventually.be.true;
});

it("should allow the use of callbacks instead of promises",(done) => {
model.dropIndex({ answer: 1 },(err, index) => {
if (err) return done(err);
chai.expect(index).to.be.true;
return done();
});
});
});

Expand Down

0 comments on commit f6b21a7

Please sign in to comment.