projection-schema-registry.spec.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {ProjectionSchemaRegistry} from './projection-schema-registry.js';
  4. const SCHEMA = {foo: true, bar: false};
  5. const SCHEMA_NAME = 'mySchema';
  6. const SCHEMA_DEF = {name: SCHEMA_NAME, schema: SCHEMA};
  7. describe('ProjectionSchemaRegistry', function () {
  8. describe('defineSchema', function () {
  9. it('should add the given definition to registry', function () {
  10. const S = new ProjectionSchemaRegistry();
  11. S.defineSchema(SCHEMA_DEF);
  12. const res = S.getDefinition(SCHEMA_NAME);
  13. expect(res).to.be.eq(SCHEMA_DEF);
  14. });
  15. it('should throw an error if the given name is already registered', function () {
  16. const S = new ProjectionSchemaRegistry();
  17. S.defineSchema(SCHEMA_DEF);
  18. const throwable = () => S.defineSchema(SCHEMA_DEF);
  19. const error = format(
  20. 'Projection schema %v is already registered.',
  21. SCHEMA_NAME,
  22. );
  23. expect(throwable).to.throw(error);
  24. });
  25. });
  26. describe('hasSchema', function () {
  27. it('should return true if the given name is registered', function () {
  28. const S = new ProjectionSchemaRegistry();
  29. const res1 = S.hasSchema(SCHEMA_NAME);
  30. expect(res1).to.be.false;
  31. S.defineSchema(SCHEMA_DEF);
  32. const res2 = S.hasSchema(SCHEMA_NAME);
  33. expect(res2).to.be.true;
  34. });
  35. });
  36. describe('getSchema', function () {
  37. it('should throw an error if the given name is not registered', function () {
  38. const S = new ProjectionSchemaRegistry();
  39. const throwable = () => S.getSchema(SCHEMA_NAME);
  40. const error = format('Projection schema %v is not found.', SCHEMA_NAME);
  41. expect(throwable).to.throw(error);
  42. });
  43. it('should return registered schema', function () {
  44. const S = new ProjectionSchemaRegistry();
  45. S.defineSchema(SCHEMA_DEF);
  46. const res = S.getSchema(SCHEMA_NAME);
  47. expect(res).to.be.eq(SCHEMA);
  48. });
  49. });
  50. describe('getDefinition', function () {
  51. it('should throw an error if the given name is not registered', function () {
  52. const S = new ProjectionSchemaRegistry();
  53. const throwable = () => S.getDefinition(SCHEMA_NAME);
  54. const error = format(
  55. 'Projection definition %v is not found.',
  56. SCHEMA_NAME,
  57. );
  58. expect(throwable).to.throw(error);
  59. });
  60. it('should return registered schema', function () {
  61. const S = new ProjectionSchemaRegistry();
  62. S.defineSchema(SCHEMA_DEF);
  63. const res = S.getDefinition(SCHEMA_NAME);
  64. expect(res).to.be.eq(SCHEMA_DEF);
  65. });
  66. });
  67. });