model-definition-validator.spec.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import chai from 'chai';
  2. import {format} from 'util';
  3. import {expect} from 'chai';
  4. import {RelationsDefinitionValidator} from './relations/index.js';
  5. import {PropertiesDefinitionValidator} from './properties/index.js';
  6. import {ModelDefinitionValidator} from './model-definition-validator.js';
  7. const S = new ModelDefinitionValidator();
  8. const sandbox = chai.spy.sandbox();
  9. describe('ModelDefinitionValidator', function () {
  10. afterEach(function () {
  11. sandbox.restore();
  12. });
  13. describe('validate', function () {
  14. it('requires the given definition to be an object', function () {
  15. const validate = value => () => S.validate(value);
  16. const error = value =>
  17. format(
  18. 'The model definition should be an Object, but %s given.',
  19. value,
  20. );
  21. expect(validate('str')).to.throw(error('"str"'));
  22. expect(validate(10)).to.throw(error('10'));
  23. expect(validate(true)).to.throw(error('true'));
  24. expect(validate(false)).to.throw(error('false'));
  25. expect(validate([])).to.throw(error('Array'));
  26. expect(validate(undefined)).to.throw(error('undefined'));
  27. expect(validate(null)).to.throw(error('null'));
  28. validate({name: 'model'})();
  29. });
  30. it('requires the option "name" as a non-empty string', function () {
  31. const validate = name => () => S.validate({name});
  32. const error = value =>
  33. format(
  34. 'The model definition requires the option "name" ' +
  35. 'as a non-empty String, but %s given.',
  36. value,
  37. );
  38. expect(validate('')).to.throw(error('""'));
  39. expect(validate(10)).to.throw(error('10'));
  40. expect(validate(true)).to.throw(error('true'));
  41. expect(validate(false)).to.throw(error('false'));
  42. expect(validate([])).to.throw(error('Array'));
  43. expect(validate({})).to.throw(error('Object'));
  44. expect(validate(undefined)).to.throw(error('undefined'));
  45. expect(validate(null)).to.throw(error('null'));
  46. validate('model')();
  47. });
  48. it('expects the provided option "datasource" to be a string', function () {
  49. const validate = datasource => () =>
  50. S.validate({name: 'model', datasource});
  51. const error = value =>
  52. format(
  53. 'The provided option "datasource" of the model "model" ' +
  54. 'should be a String, but %s given.',
  55. value,
  56. );
  57. expect(validate(10)).to.throw(error('10'));
  58. expect(validate(true)).to.throw(error('true'));
  59. expect(validate([])).to.throw(error('Array'));
  60. expect(validate({})).to.throw(error('Object'));
  61. validate('datasource')();
  62. });
  63. it('expects the provided option "base" to be a string', function () {
  64. const validate = base => () => S.validate({name: 'model', base});
  65. const error = value =>
  66. format(
  67. 'The provided option "base" of the model "model" ' +
  68. 'should be a String, but %s given.',
  69. value,
  70. );
  71. expect(validate(10)).to.throw(error('10'));
  72. expect(validate(true)).to.throw(error('true'));
  73. expect(validate([])).to.throw(error('Array'));
  74. expect(validate({})).to.throw(error('Object'));
  75. validate('base')();
  76. });
  77. it('expects the provided option "tableName" to be a string', function () {
  78. const validate = tableName => () =>
  79. S.validate({name: 'model', tableName});
  80. const error = value =>
  81. format(
  82. 'The provided option "tableName" of the model "model" ' +
  83. 'should be a String, but %s given.',
  84. value,
  85. );
  86. expect(validate(10)).to.throw(error('10'));
  87. expect(validate(true)).to.throw(error('true'));
  88. expect(validate([])).to.throw(error('Array'));
  89. expect(validate({})).to.throw(error('Object'));
  90. validate('tableName')();
  91. });
  92. it('expects the provided option "properties" to be an object', function () {
  93. const validate = properties => () =>
  94. S.validate({name: 'model', properties});
  95. const error = value =>
  96. format(
  97. 'The provided option "properties" of the model "model" ' +
  98. 'should be an Object, but %s given.',
  99. value,
  100. );
  101. expect(validate('str')).to.throw(error('"str"'));
  102. expect(validate(10)).to.throw(error('10'));
  103. expect(validate(true)).to.throw(error('true'));
  104. expect(validate([])).to.throw(error('Array'));
  105. validate({})();
  106. });
  107. it('expects the provided option "relations" to be an object', function () {
  108. const validate = relations => () =>
  109. S.validate({name: 'model', relations});
  110. const error = value =>
  111. format(
  112. 'The provided option "relations" of the model "model" ' +
  113. 'should be an Object, but %s given.',
  114. value,
  115. );
  116. expect(validate('str')).to.throw(error('"str"'));
  117. expect(validate(10)).to.throw(error('10'));
  118. expect(validate(true)).to.throw(error('true'));
  119. expect(validate([])).to.throw(error('Array'));
  120. validate({})();
  121. });
  122. it('uses PropertiesDefinitionValidator service to validate model properties', function () {
  123. const V = S.get(PropertiesDefinitionValidator);
  124. sandbox.on(V, 'validate');
  125. const properties = {};
  126. S.validate({name: 'model', properties});
  127. expect(V.validate).to.have.been.called.once;
  128. expect(V.validate).to.have.been.called.with.exactly('model', properties);
  129. });
  130. it('uses RelationsDefinitionValidator service to validate model relations', function () {
  131. const V = S.get(RelationsDefinitionValidator);
  132. sandbox.on(V, 'validate');
  133. const relations = {};
  134. S.validate({name: 'model', relations});
  135. expect(V.validate).to.have.been.called.once;
  136. expect(V.validate).to.have.been.called.with.exactly('model', relations);
  137. });
  138. });
  139. });