| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import chai from 'chai';
- import {format} from 'util';
- import {expect} from 'chai';
- import {RelationsDefinitionValidator} from './relations/index.js';
- import {PropertiesDefinitionValidator} from './properties/index.js';
- import {ModelDefinitionValidator} from './model-definition-validator.js';
- const S = new ModelDefinitionValidator();
- const sandbox = chai.spy.sandbox();
- describe('ModelDefinitionValidator', function () {
- afterEach(function () {
- sandbox.restore();
- });
- describe('validate', function () {
- it('requires the given definition to be an object', function () {
- const validate = value => () => S.validate(value);
- const error = value =>
- format(
- 'The model definition should be an Object, but %s given.',
- value,
- );
- expect(validate('str')).to.throw(error('"str"'));
- expect(validate(10)).to.throw(error('10'));
- expect(validate(true)).to.throw(error('true'));
- expect(validate(false)).to.throw(error('false'));
- expect(validate([])).to.throw(error('Array'));
- expect(validate(undefined)).to.throw(error('undefined'));
- expect(validate(null)).to.throw(error('null'));
- validate({name: 'model'})();
- });
- it('requires the option "name" as a non-empty string', function () {
- const validate = name => () => S.validate({name});
- const error = value =>
- format(
- 'The model definition requires the option "name" ' +
- 'as a non-empty String, but %s given.',
- value,
- );
- expect(validate('')).to.throw(error('""'));
- expect(validate(10)).to.throw(error('10'));
- expect(validate(true)).to.throw(error('true'));
- expect(validate(false)).to.throw(error('false'));
- expect(validate([])).to.throw(error('Array'));
- expect(validate({})).to.throw(error('Object'));
- expect(validate(undefined)).to.throw(error('undefined'));
- expect(validate(null)).to.throw(error('null'));
- validate('model')();
- });
- it('expects the provided option "datasource" to be a string', function () {
- const validate = datasource => () =>
- S.validate({name: 'model', datasource});
- const error = value =>
- format(
- 'The provided option "datasource" of the model "model" ' +
- 'should be a String, but %s given.',
- value,
- );
- expect(validate(10)).to.throw(error('10'));
- expect(validate(true)).to.throw(error('true'));
- expect(validate([])).to.throw(error('Array'));
- expect(validate({})).to.throw(error('Object'));
- validate('datasource')();
- });
- it('expects the provided option "base" to be a string', function () {
- const validate = base => () => S.validate({name: 'model', base});
- const error = value =>
- format(
- 'The provided option "base" of the model "model" ' +
- 'should be a String, but %s given.',
- value,
- );
- expect(validate(10)).to.throw(error('10'));
- expect(validate(true)).to.throw(error('true'));
- expect(validate([])).to.throw(error('Array'));
- expect(validate({})).to.throw(error('Object'));
- validate('base')();
- });
- it('expects the provided option "tableName" to be a string', function () {
- const validate = tableName => () =>
- S.validate({name: 'model', tableName});
- const error = value =>
- format(
- 'The provided option "tableName" of the model "model" ' +
- 'should be a String, but %s given.',
- value,
- );
- expect(validate(10)).to.throw(error('10'));
- expect(validate(true)).to.throw(error('true'));
- expect(validate([])).to.throw(error('Array'));
- expect(validate({})).to.throw(error('Object'));
- validate('tableName')();
- });
- it('expects the provided option "properties" to be an object', function () {
- const validate = properties => () =>
- S.validate({name: 'model', properties});
- const error = value =>
- format(
- 'The provided option "properties" of the model "model" ' +
- 'should be an Object, but %s given.',
- value,
- );
- expect(validate('str')).to.throw(error('"str"'));
- expect(validate(10)).to.throw(error('10'));
- expect(validate(true)).to.throw(error('true'));
- expect(validate([])).to.throw(error('Array'));
- validate({})();
- });
- it('expects the provided option "relations" to be an object', function () {
- const validate = relations => () =>
- S.validate({name: 'model', relations});
- const error = value =>
- format(
- 'The provided option "relations" of the model "model" ' +
- 'should be an Object, but %s given.',
- value,
- );
- expect(validate('str')).to.throw(error('"str"'));
- expect(validate(10)).to.throw(error('10'));
- expect(validate(true)).to.throw(error('true'));
- expect(validate([])).to.throw(error('Array'));
- validate({})();
- });
- it('uses PropertiesDefinitionValidator service to validate model properties', function () {
- const V = S.get(PropertiesDefinitionValidator);
- sandbox.on(V, 'validate');
- const properties = {};
- S.validate({name: 'model', properties});
- expect(V.validate).to.have.been.called.once;
- expect(V.validate).to.have.been.called.with.exactly('model', properties);
- });
- it('uses RelationsDefinitionValidator service to validate model relations', function () {
- const V = S.get(RelationsDefinitionValidator);
- sandbox.on(V, 'validate');
- const relations = {};
- S.validate({name: 'model', relations});
- expect(V.validate).to.have.been.called.once;
- expect(V.validate).to.have.been.called.with.exactly('model', relations);
- });
- });
- });
|