From f3d5a17586a703dc72a5481c6d6d0b4f136ec935 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Thu, 23 Apr 2015 08:01:14 +0200 Subject: [PATCH] Added validation tests --- test/Validation.d.ts | 1 + test/Validation.js | 175 ++++++++++++++++++++++++++++++++++++ test/Validation.js.map | 1 + test/Validation.ts | 197 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 374 insertions(+) create mode 100644 test/Validation.d.ts create mode 100644 test/Validation.js create mode 100644 test/Validation.js.map create mode 100644 test/Validation.ts diff --git a/test/Validation.d.ts b/test/Validation.d.ts new file mode 100644 index 0000000..e86106e --- /dev/null +++ b/test/Validation.d.ts @@ -0,0 +1 @@ +/// diff --git a/test/Validation.js b/test/Validation.js new file mode 100644 index 0000000..deb0c2a --- /dev/null +++ b/test/Validation.js @@ -0,0 +1,175 @@ +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +/// +var Iridium = require('../index'); +var Person = (function (_super) { + __extends(Person, _super); + function Person() { + _super.apply(this, arguments); + } + return Person; +})(Iridium.Instance); +describe("Validation", function () { + var core = new Iridium.Core({ database: 'test' }); + var model = new Iridium.Model(core, Person, 'test', { + name: String, + dateOfBirth: Date, + siblings: [{ + name: String, + related: Boolean, + ageDifference: Number + }] + }); + before(function () { return core.connect(); }); + after(function () { return model.remove().then(function () { return core.close(); }); }); + beforeEach(function () { return model.remove(); }); + describe("inserting", function () { + it("should successfully validate single documents which match the schema", function () { + return chai.expect(model.insert({ + name: 'John', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jane', + related: true, + ageDifference: -2 + }] + })).to.eventually.be.ok; + }); + it("should fail to validate single documents which do not match the schema", function () { + return chai.expect(model.insert({ + name: 'John', + dateOfBirth: 0, + siblings: [{ + name: 'Jane', + related: true, + ageDifference: -2 + }] + })).to.eventually.be.rejected; + }); + it("should not insert a document into the database if it fails validation", function () { + return model.insert({ + name: 'John', + dateOfBirth: 0, + siblings: [{ + name: 'Jane', + related: true, + ageDifference: -2 + }] + }).catch(function () { return chai.expect(model.findOne({ dateOfBirth: 0 })).to.eventually.be.null; }); + }); + it("should successfully validate multiple documents which match the schema", function () { + return chai.expect(model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: false, + ageDifference: -2 + }] + }, { + name: 'Jack', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }])).to.eventually.be.ok; + }); + it("should fail to validate multiple documents which do not match the schema", function () { + return chai.expect(model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: 'related', + ageDifference: -2 + }] + }, { + name: 5, + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }])).to.eventually.be.rejected; + }); + it("should fail to validate multiple documents where some do not match the schema", function () { + return chai.expect(model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: 'related', + ageDifference: -2 + }] + }, { + name: 'Jack', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }])).to.eventually.be.rejected; + }); + it("should fail to validate multiple documents where some do not match the schema", function () { + return model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: 'related', + ageDifference: -2 + }] + }, { + name: 'Jack', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }]).catch(function () { return chai.expect(model.findOne({ 'siblings.related': 'related' })).to.eventually.be.null; }); + }); + }); + describe("instances", function () { + beforeEach(function () { return model.remove().then(function () { return model.insert({ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [] + }); }); }); + it("should validate documents when you attempt to change them", function () { + return chai.expect(model.get({ name: 'Frank' }).then(function (frank) { + frank.siblings.push({ name: 'Francette', related: true, ageDifference: 0 }); + return frank.save(); + })).to.eventually.be.ok; + }); + it("should fail validation if the document does not match the schema", function () { + return chai.expect(model.get({ name: 'Frank' }).then(function (frank) { + frank.siblings.push({ name: 'Francette', related: 'related', ageDifference: 0 }); + return frank.save(); + })).to.eventually.be.rejected; + }); + it("should not change the document in the database if validation fails", function () { + return chai.expect(model.get({ name: 'Frank' }).then(function (frank) { + frank.siblings.push({ name: 'Francette', related: 'related', ageDifference: 0 }); + return frank.save(); + }).catch(function () { return model.get({ name: 'Frank', 'siblings.related': 'related' }); })).to.eventually.be.null; + }); + it("should not reverse the changes made to the instance if validation fails", function () { + var staticFrank; + return chai.expect(model.get({ name: 'Frank' }).then(function (frank) { + staticFrank = frank; + frank.siblings.push({ name: 'Francette', related: 'related', ageDifference: 0 }); + return frank.save(); + }).catch(function () { return chai.expect(staticFrank).to.have.property('siblings').with.length(1); })).to.eventually.be.ok; + }); + }); +}); +//# sourceMappingURL=Validation.js.map \ No newline at end of file diff --git a/test/Validation.js.map b/test/Validation.js.map new file mode 100644 index 0000000..12913ee --- /dev/null +++ b/test/Validation.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Validation.js","sourceRoot":"","sources":["Validation.ts"],"names":["Person","Person.constructor"],"mappings":";;;;;;AACA,AADA,4CAA4C;AAC5C,IAAO,OAAO,WAAW,UAAU,CAAC,CAAC;AAYrC,IAAM,MAAM;IAASA,UAAfA,MAAMA,UAA2CA;IAAvDA,SAAMA,MAAMA;QAASC,8BAAkCA;IAQvDA,CAACA;IAADD,aAACA;AAADA,CAACA,AARD,EAAqB,OAAO,CAAC,QAAQ,EAQpC;AAED,QAAQ,CAAC,YAAY,EAAE;IACnB,IAAI,IAAI,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;IAClD,IAAI,KAAK,GAAG,IAAI,OAAO,CAAC,KAAK,CAAmB,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE;QAClE,IAAI,EAAE,MA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AQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAO,SAAS,EAAE,aAAa,EAAE,CAAC,EAAE,CAAC,CAAC;gBACtF,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;YACxB,CAAC,CAAC,CAAC,KAAK,CAAC,cAAM,OAAA,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAApE,CAAoE,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,CAAC;QAC9G,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/test/Validation.ts b/test/Validation.ts new file mode 100644 index 0000000..82e0496 --- /dev/null +++ b/test/Validation.ts @@ -0,0 +1,197 @@ +/// +import Iridium = require('../index'); + +interface Document { + name: string; + dateOfBirth: Date; + siblings: { + name: string; + related: boolean; + ageDifference: number; + }[]; +} + +class Person extends Iridium.Instance { + name: string; + dateOfBirth: Date; + siblings: { + name: string; + related: boolean; + ageDifference: number; + }[]; +} + +describe("Validation", () => { + var core = new Iridium.Core({ database: 'test' }); + var model = new Iridium.Model(core, Person, 'test', { + name: String, + dateOfBirth: Date, + siblings: [{ + name: String, + related: Boolean, + ageDifference: Number + }] + }); + + before(() => core.connect()); + + after(() => model.remove().then(() => core.close())); + + beforeEach(() => model.remove()); + + describe("inserting", () => { + it("should successfully validate single documents which match the schema", () => { + return chai.expect(model.insert({ + name: 'John', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jane', + related: true, + ageDifference: -2 + }] + })).to.eventually.be.ok; + }); + + it("should fail to validate single documents which do not match the schema", () => { + return chai.expect(model.insert({ + name: 'John', + dateOfBirth: 0, + siblings: [{ + name: 'Jane', + related: true, + ageDifference: -2 + }] + })).to.eventually.be.rejected; + }); + + it("should not insert a document into the database if it fails validation", () => { + return model.insert({ + name: 'John', + dateOfBirth: 0, + siblings: [{ + name: 'Jane', + related: true, + ageDifference: -2 + }] + }).catch(() => chai.expect(model.findOne({ dateOfBirth: 0 })).to.eventually.be.null); + }); + + it("should successfully validate multiple documents which match the schema", () => { + return chai.expect(model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: false, + ageDifference: -2 + }] + }, { + name: 'Jack', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }])).to.eventually.be.ok; + }); + + it("should fail to validate multiple documents which do not match the schema", () => { + return chai.expect(model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: 'related', + ageDifference: -2 + }] + }, { + name: 5, + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }])).to.eventually.be.rejected; + }); + + it("should fail to validate multiple documents where some do not match the schema", () => { + return chai.expect(model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: 'related', + ageDifference: -2 + }] + }, { + name: 'Jack', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }])).to.eventually.be.rejected; + }); + + it("should fail to validate multiple documents where some do not match the schema", () => { + return model.insert([{ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [{ + name: 'Francie', + related: 'related', + ageDifference: -2 + }] + }, { + name: 'Jack', + dateOfBirth: new Date(), + siblings: [{ + name: 'Jill', + related: true, + ageDifference: 2 + }] + }]).catch(() => chai.expect(model.findOne({ 'siblings.related': 'related' })).to.eventually.be.null); + }); + }); + + describe("instances", () => { + beforeEach(() => model.remove().then(() => model.insert({ + name: 'Frank', + dateOfBirth: new Date(), + siblings: [] + }))); + + it("should validate documents when you attempt to change them", () => { + return chai.expect(model.get({ name: 'Frank' }).then((frank) => { + frank.siblings.push({ name: 'Francette', related: true, ageDifference: 0 }); + return frank.save(); + })).to.eventually.be.ok; + }); + + it("should fail validation if the document does not match the schema", () => { + return chai.expect(model.get({ name: 'Frank' }).then((frank) => { + frank.siblings.push({ name: 'Francette', related: 'related', ageDifference: 0 }); + return frank.save(); + })).to.eventually.be.rejected; + }); + + it("should not change the document in the database if validation fails", () => { + return chai.expect(model.get({ name: 'Frank' }).then((frank) => { + frank.siblings.push({ name: 'Francette', related: 'related', ageDifference: 0 }); + return frank.save(); + }).catch(() => model.get({ name: 'Frank', 'siblings.related': 'related' }))).to.eventually.be.null; + }); + + it("should not reverse the changes made to the instance if validation fails", () => { + var staticFrank: Person; + return chai.expect(model.get({ name: 'Frank' }).then((frank) => { + staticFrank = frank; + frank.siblings.push({ name: 'Francette', related: 'related', ageDifference: 0 }); + return frank.save(); + }).catch(() => chai.expect(staticFrank).to.have.property('siblings').with.length(1))).to.eventually.be.ok; + }); + }); +}); \ No newline at end of file