data-schema-registry.spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {expect} from 'chai';
  2. import {DataType} from './data-type.js';
  3. import {DataSchemaRegistry} from './data-schema-registry.js';
  4. describe('DataSchemaRegistry', function () {
  5. describe('defineSchema', function () {
  6. it('should register a given definition', function () {
  7. const S = new DataSchemaRegistry();
  8. const schemaDef = {name: 'mySchema', schema: {}};
  9. S.defineSchema(schemaDef);
  10. const res = S.getDefinition(schemaDef.name);
  11. expect(res).to.be.eql(schemaDef);
  12. });
  13. it('should override a registered definition', function () {
  14. const S = new DataSchemaRegistry();
  15. const def1 = {name: 'mySchema', schema: {type: DataType.STRING}};
  16. const def2 = {name: 'mySchema', schema: {type: DataType.NUMBER}};
  17. S.defineSchema(def1);
  18. const res1 = S.getDefinition(def1.name);
  19. expect(res1).to.be.eql(def1);
  20. S.defineSchema(def2);
  21. const res2 = S.getDefinition(def2.name);
  22. expect(res2).to.be.eql(def2);
  23. });
  24. it('should return the current instance', function () {
  25. const S = new DataSchemaRegistry();
  26. const res = S.defineSchema({name: 'mySchema', schema: {}});
  27. expect(res).to.be.eq(S);
  28. });
  29. });
  30. describe('hasSchema', function () {
  31. it('should return true if a given name is registered', function () {
  32. const S = new DataSchemaRegistry();
  33. const schemaDef = {name: 'mySchema', schema: {}};
  34. expect(S.hasSchema(schemaDef.name)).to.be.false;
  35. S.defineSchema(schemaDef);
  36. expect(S.hasSchema(schemaDef.name)).to.be.true;
  37. });
  38. });
  39. describe('getSchema', function () {
  40. it('should throw an error if a given name is not registered', function () {
  41. const S = new DataSchemaRegistry();
  42. const throwable = () => S.getSchema('mySchema');
  43. expect(throwable).to.throw('Data schema "mySchema" is not found.');
  44. });
  45. it('should return the schema property of a registered definition', function () {
  46. const S = new DataSchemaRegistry();
  47. const schemaDef = {name: 'mySchema', schema: {}};
  48. S.defineSchema(schemaDef);
  49. const res = S.getSchema(schemaDef.name);
  50. expect(res).to.be.eql(schemaDef.schema);
  51. });
  52. });
  53. describe('getDefinition', function () {
  54. it('should throw an error if a given name is not registered', function () {
  55. const S = new DataSchemaRegistry();
  56. const throwable = () => S.getDefinition('mySchema');
  57. expect(throwable).to.throw('Schema definition "mySchema" is not found.');
  58. });
  59. it('should return the registered definition for a given name', function () {
  60. const S = new DataSchemaRegistry();
  61. const schemaDef = {name: 'mySchema', schema: {}};
  62. S.defineSchema(schemaDef);
  63. const res = S.getDefinition(schemaDef.name);
  64. expect(res).to.be.eql(schemaDef);
  65. });
  66. });
  67. });