Skip to content

Commit

Permalink
Merge f35676c into 5e9d53e
Browse files Browse the repository at this point in the history
  • Loading branch information
segayuu committed Jul 3, 2019
2 parents 5e9d53e + f35676c commit e8365a5
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 206 deletions.
8 changes: 4 additions & 4 deletions test/scripts/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('Model', () => {
age: 20
}).then(data => {
User.findById(data._id).should.exist;
User.length.should.eql(1);
User.should.to.have.length(1);
listener.calledOnce.should.be.true;
return data;
}).then(data => User.removeById(data._id));
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('Model', () => {
const user = User.findById(id);

user.age.should.eql(2);
User.length.should.eql(1);
User.should.to.have.length(1);

return User.removeById(id);
});
Expand Down Expand Up @@ -471,7 +471,7 @@ describe('Model', () => {
});

it('count()', () => {
Post.length.should.eql(Post.count());
Post.should.to.have.length(Post.count());
});

it('forEach()', () => {
Expand Down Expand Up @@ -1218,7 +1218,7 @@ describe('Model', () => {
{_id: 'B', bool: 0}
]);

Test.length.should.eql(2);
Test.should.to.have.length(2);

Test.toArray().should.eql([
Test.new({_id: 'A', bool: true}),
Expand Down
50 changes: 9 additions & 41 deletions test/scripts/schema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();

describe('Schema', () => {
const Database = require('../..');
Expand Down Expand Up @@ -98,18 +98,10 @@ describe('Schema', () => {
schema.hooks.pre.remove.should.have.length(1);

// incompatible type
try {
schema.pre('wtf', () => {});
} catch (err) {
err.should.eql(new TypeError('Hook type must be `save` or `remove`!'));
}
(() => schema.pre('wtf', () => {})).should.to.throw(TypeError, 'Hook type must be `save` or `remove`!');

// hook is not a function
try {
schema.pre('save');
} catch (err) {
err.should.eql(new TypeError('Hook must be a function!'));
}
(() => schema.pre('save')).should.to.throw(TypeError, 'Hook must be a function!');
});

it('post()', () => {
Expand All @@ -126,18 +118,10 @@ describe('Schema', () => {
schema.hooks.post.remove.should.have.length(1);

// incompatible type
try {
schema.post('wtf', () => {});
} catch (err) {
err.should.eql(new TypeError('Hook type must be `save` or `remove`!'));
}
(() => schema.post('wtf', () => {})).should.throw(TypeError, 'Hook type must be `save` or `remove`!');

// hook is not a function
try {
schema.post('save');
} catch (err) {
err.should.eql(new TypeError('Hook must be a function!'));
}
(() => schema.post('save')).should.to.throw(TypeError, 'Hook must be a function!');
});

it('method()', () => {
Expand All @@ -148,18 +132,10 @@ describe('Schema', () => {
schema.methods.test.should.exist;

// without name
try {
schema.method();
} catch (err) {
err.should.eql(new TypeError('Method name is required!'));
}
schema.method.should.to.throw(TypeError, 'Method name is required!');

// without function
try {
schema.method('wtf');
} catch (err) {
err.should.eql(new TypeError('Instance method must be a function!'));
}
(() => schema.method('wtf')).should.to.throw(TypeError, 'Instance method must be a function!');
});

it('static()', () => {
Expand All @@ -170,17 +146,9 @@ describe('Schema', () => {
schema.statics.test.should.exist;

// without name
try {
schema.static();
} catch (err) {
err.should.eql(new TypeError('Method name is required!'));
}
schema.static.should.to.throw(TypeError, 'Method name is required!');

// without function
try {
schema.sttic('wtf');
} catch (err) {
err.should.eql(new TypeError('Static method must be a function!'));
}
(() => schema.static('wtf')).should.to.throw(TypeError, 'Static method must be a function!');
});
});
8 changes: 1 addition & 7 deletions test/scripts/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ describe('SchemaType', () => {
it('validate() - required', () => {
const type = new SchemaType('test', {required: true});

try {
type.validate();
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`test` is required!');
}
type.validate.should.to.throw(ValidationError, '`test` is required!');
});

it('compare()', () => {
Expand Down
24 changes: 3 additions & 21 deletions test/scripts/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ describe('SchemaTypeArray', () => {
});

function shouldThrowError(value) {
try {
type.validate(value);
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', `\`${value}\` is not an array!`);
}
(() => type.validate(value)).should.to.throw(ValidationError, `\`${value}\` is not an array!`);
}

it('validate()', () => {
Expand All @@ -51,25 +45,13 @@ describe('SchemaTypeArray', () => {
it('validate() - required', () => {
const type = new SchemaTypeArray('test', {required: true});

try {
type.validate();
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`test` is required!');
}
type.validate.should.to.throw(ValidationError, '`test` is required!');
});

it('validate() - child', () => {
const type = new SchemaTypeArray('test', {child: new SchemaTypeString()});

try {
type.validate([1, 2, 3]);
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`1` is not a string!');
}
(() => type.validate([1, 2, 3])).should.to.throw(ValidationError, '`1` is not a string!');
});

it('compare()', () => {
Expand Down
18 changes: 3 additions & 15 deletions test/scripts/types/boolean.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();
const ValidationError = require('../../../lib/error/validation');

describe('SchemaTypeBoolean', () => {
Expand Down Expand Up @@ -28,13 +28,7 @@ describe('SchemaTypeBoolean', () => {
});

function shouldThrowError(value) {
try {
type.validate(value);
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', `\`${value}\` is not a boolean!`);
}
(() => type.validate(value)).should.to.throw(ValidationError, `\`${value}\` is not a boolean!`);
}

it('validate()', () => {
Expand All @@ -51,13 +45,7 @@ describe('SchemaTypeBoolean', () => {
it('validate() - required', () => {
const type = new SchemaTypeBoolean('test', {required: true});

try {
type.validate();
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`test` is required!');
}
type.validate.should.to.throw(ValidationError, '`test` is required!');
});

it('parse()', () => {
Expand Down
18 changes: 3 additions & 15 deletions test/scripts/types/buffer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
const should = require('chai').should();
const ValidationError = require('../../../lib/error/validation');

describe('SchemaTypeBuffer', () => {
Expand Down Expand Up @@ -30,13 +30,7 @@ describe('SchemaTypeBuffer', () => {
});

function shouldThrowError(value) {
try {
type.validate(value);
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', `\`${value}\` is not a valid buffer!`);
}
(() => type.validate(value)).should.to.throw(ValidationError, `\`${value}\` is not a valid buffer!`);
}

it('validate()', () => {
Expand All @@ -51,13 +45,7 @@ describe('SchemaTypeBuffer', () => {
it('validate() - required', () => {
const type = new SchemaTypeBuffer('test', {required: true});

try {
type.validate();
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`test` is required!');
}
type.validate.should.to.throw(ValidationError, '`test` is required!');
});

it('match()', () => {
Expand Down
8 changes: 1 addition & 7 deletions test/scripts/types/cuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ describe('SchemaTypeCUID', () => {
it('validate()', () => {
type.validate('ch72gsb320000udocl363eofy').should.eql('ch72gsb320000udocl363eofy');

try {
type.validate('foo');
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`foo` is not a valid CUID');
}
(() => type.validate('foo')).should.to.throw(ValidationError, '`foo` is not a valid CUID');
});
});
16 changes: 2 additions & 14 deletions test/scripts/types/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ describe('SchemaTypeDate', () => {
});

function shouldThrowError(value) {
try {
type.validate(value);
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', `\`${value}\` is not a valid date!`);
}
(() => type.validate(value)).should.to.throw(ValidationError, `\`${value}\` is not a valid date!`);
}

it('validate()', () => {
Expand All @@ -43,13 +37,7 @@ describe('SchemaTypeDate', () => {
it('validate() - required', () => {
const type = new SchemaTypeDate('test', {required: true});

try {
type.validate();
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`test` is required!');
}
type.validate.should.to.throw(ValidationError, '`test` is required!');
});

it('match()', () => {
Expand Down
18 changes: 3 additions & 15 deletions test/scripts/types/enum.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();
const ValidationError = require('../../../lib/error/validation');

describe('SchemaTypeEnum', () => {
Expand All @@ -11,24 +11,12 @@ describe('SchemaTypeEnum', () => {

type.validate('foo').should.eql('foo');

try {
type.validate('wat');
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', 'The value must be one of foo, bar, baz');
}
(() => type.validate('wat')).should.to.throw(ValidationError, 'The value must be one of foo, bar, baz');
});

it('validate() - required', () => {
const type = new SchemaTypeEnum('test', {required: true});

try {
type.validate();
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`test` is required!');
}
type.validate.should.to.throw(ValidationError, '`test` is required!');
});
});
26 changes: 4 additions & 22 deletions test/scripts/types/integer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
require('chai').should();
const ValidationError = require('../../../lib/error/validation');

describe('SchemaTypeInteger', () => {
Expand All @@ -23,13 +23,7 @@ describe('SchemaTypeInteger', () => {
});

function shouldThrowError(value) {
try {
type.validate(value);
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', `\`${value}\` is not a number!`);
}
(() => type.validate(value)).should.to.throw(ValidationError, `\`${value}\` is not a number!`);
}

it('validate()', () => {
Expand All @@ -41,24 +35,12 @@ describe('SchemaTypeInteger', () => {
shouldThrowError(false);
shouldThrowError({});

try {
type.validate(3.14);
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`3.14` is not an integer!');
}
(() => type.validate(3.14)).should.to.throw(ValidationError, '`3.14` is not an integer!');
});

it('validate() - required', () => {
const type = new SchemaTypeInteger('test', {required: true});

try {
type.validate();
} catch (err) {
err.should.be
.instanceOf(ValidationError)
.property('message', '`test` is required!');
}
type.validate.should.throw(ValidationError, '`test` is required!');
});
});
Loading

0 comments on commit e8365a5

Please sign in to comment.