Skip to content

Commit

Permalink
Added some more model and instance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Apr 24, 2015
1 parent 5d18479 commit f4f9d9b
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 8 deletions.
82 changes: 81 additions & 1 deletion test/Instance.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/Instance.js.map

Large diffs are not rendered by default.

104 changes: 103 additions & 1 deletion test/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import Iridium = require('../index');
interface TestDocument {
id?: string;
answer: number;
lots?: number[];
less?: { [key: string]: number };
}

class Test extends Iridium.Instance<TestDocument, Test> implements TestDocument {
id: string;
answer: number;
lots: number[];
less: { [key: string]: number };

test() {
return true;
Expand All @@ -24,7 +28,12 @@ class TestDB extends Iridium.Core {
super("mongodb://localhost/test");
}

Test = new Iridium.Model<TestDocument, Test>(this, Test, 'test', { id: false, answer: Number });
Test = new Iridium.Model<TestDocument, Test>(this, Test, 'test', {
id: false,
answer: Number,
lots: { $required: false, $type: [Number] },
less: { $required: false, $propertyType: Number }
});
}

describe("Instance",() => {
Expand Down Expand Up @@ -193,6 +202,12 @@ describe("Instance",() => {
});
});

it("should set _isNew to true if the instance was removed from the database",() => {
return core.Test.get().then(instance => {
core.Test.remove().then(() => instance.update()).then(() => chai.expect((<any>instance)._isNew).to.be.true);
});
});

it("should return a promise for the instance",() => {
return core.Test.get().then((instance) => {
core.Test.update({ id: instance.id }, {
Expand Down Expand Up @@ -227,6 +242,12 @@ describe("Instance",() => {
});
});

it("should set _isNew to true if the instance was removed from the database",() => {
return core.Test.get().then(instance => {
core.Test.remove().then(() => instance.refresh()).then(() => chai.expect((<any>instance)._isNew).to.be.true);
});
});

it("should return a promise for the instance",() => {
return core.Test.get().then((instance) => {
core.Test.update({ id: instance.id }, {
Expand Down Expand Up @@ -261,6 +282,19 @@ describe("Instance",() => {
return core.Test.get().then((instance) => chai.expect(instance.remove()).to.eventually.equal(instance));
});

it("shouldn't mind if the object has already been removed",() => {
return core.Test.get().then(instance => {
return chai.expect(core.Test.remove().then(() => instance.remove())).to.eventually.not.be.rejected;
});
});

it("should be a no-op if the object is marked as _isNew",() => {
return core.Test.get().then(instance => {
var newInstance = new core.Test.Instance(instance.document);
return newInstance.remove();
}).then(() => chai.expect(core.Test.count()).to.eventually.equal(1));
});

it("should allow the use of a callback instead of promises",(done) => {
core.Test.get().then((instance) => {
instance.remove((err, result) => {
Expand All @@ -271,4 +305,72 @@ describe("Instance",() => {
});
});
});

describe("delete()",() => {
beforeEach(() => core.Test.remove().then(() => core.Test.insert({ answer: 1 })));

it("should remove the document from the database",() => {
return chai.expect(core.Test.get().then((instance) => instance.delete()).then(() => core.Test.get())).to.eventually.be.null;
});

it("should set the instance's isNew property to true",() => {
return chai.expect(core.Test.get().then((instance) => instance.delete())).to.eventually.have.property('_isNew', true);
});

it("should return a promise for the instance",() => {
return core.Test.get().then((instance) => chai.expect(instance.delete()).to.eventually.equal(instance));
});

it("shouldn't mind if the object has already been removed",() => {
return core.Test.get().then(instance => {
return chai.expect(core.Test.remove().then(() => instance.delete())).to.eventually.not.be.rejected;
});
});

it("should allow the use of a callback instead of promises",(done) => {
core.Test.get().then((instance) => {
instance.delete((err, result) => {
if (err) return done(err);
chai.expect(result).to.equal(instance);
return done();
});
});
});
});

describe("first()",() => {
beforeEach(() => core.Test.remove().then(() => core.Test.insert({ answer: 1, lots: [1, 2, 3, 4], less: { 'a': 1, 'b': 2 } })));

it("should return the first object which matches the predicate over an array",() => {
return chai.expect(core.Test.get().then(instance => instance.first(instance.lots, lot => lot == 2))).to.eventually.equal(2);
});

it("should return the first object which matches the predicate over an object",() => {
return chai.expect(core.Test.get().then(instance => instance.first(instance.less, (value, key) => key == 'a'))).to.eventually.equal(1);
});

it("should return null if no item was found",() => {
return chai.expect(core.Test.get().then(instance => instance.first(instance.lots, lot => lot > 100))).to.eventually.be.null;
});
});

describe("select()",() => {
beforeEach(() => core.Test.remove().then(() => core.Test.insert({ answer: 1, lots: [1, 2, 3, 4], less: { 'a': 1, 'b': 2 } })));

it("should return the objects which match the predicate over an array",() => {
return chai.expect(core.Test.get().then(instance => instance.select(instance.lots, lot => lot > 2))).to.eventually.eql([3, 4]);
});

it("should return the properties which match the predicate over an object",() => {
return chai.expect(core.Test.get().then(instance => instance.select(instance.less,(value, key) => key == 'a'))).to.eventually.eql({ 'a': 1 });
});

it("should return an empty array if no items matched over an array",() => {
return chai.expect(core.Test.get().then(instance => instance.select(instance.lots, lot => lot > 100))).to.eventually.be.eql([]);
});

it("should return an empty object if no items matched over an object",() => {
return chai.expect(core.Test.get().then(instance => instance.select(instance.less, lot => lot > 100))).to.eventually.be.eql({});
});
});
});
30 changes: 28 additions & 2 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.

0 comments on commit f4f9d9b

Please sign in to comment.