| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import {expect} from 'chai';
- import {DataType} from './data-type.js';
- import {DataSchemaRegistry} from './data-schema-registry.js';
- describe('DataSchemaRegistry', function () {
- describe('defineSchema', function () {
- it('should register a given definition', function () {
- const S = new DataSchemaRegistry();
- const schemaDef = {name: 'mySchema', schema: {}};
- S.defineSchema(schemaDef);
- const res = S.getDefinition(schemaDef.name);
- expect(res).to.be.eql(schemaDef);
- });
- it('should override a registered definition', function () {
- const S = new DataSchemaRegistry();
- const def1 = {name: 'mySchema', schema: {type: DataType.STRING}};
- const def2 = {name: 'mySchema', schema: {type: DataType.NUMBER}};
- S.defineSchema(def1);
- const res1 = S.getDefinition(def1.name);
- expect(res1).to.be.eql(def1);
- S.defineSchema(def2);
- const res2 = S.getDefinition(def2.name);
- expect(res2).to.be.eql(def2);
- });
- it('should return the current instance', function () {
- const S = new DataSchemaRegistry();
- const res = S.defineSchema({name: 'mySchema', schema: {}});
- expect(res).to.be.eq(S);
- });
- });
- describe('hasSchema', function () {
- it('should return true if a given name is registered', function () {
- const S = new DataSchemaRegistry();
- const schemaDef = {name: 'mySchema', schema: {}};
- expect(S.hasSchema(schemaDef.name)).to.be.false;
- S.defineSchema(schemaDef);
- expect(S.hasSchema(schemaDef.name)).to.be.true;
- });
- });
- describe('getSchema', function () {
- it('should throw an error if a given name is not registered', function () {
- const S = new DataSchemaRegistry();
- const throwable = () => S.getSchema('mySchema');
- expect(throwable).to.throw('Data schema "mySchema" is not found.');
- });
- it('should return the schema property of a registered definition', function () {
- const S = new DataSchemaRegistry();
- const schemaDef = {name: 'mySchema', schema: {}};
- S.defineSchema(schemaDef);
- const res = S.getSchema(schemaDef.name);
- expect(res).to.be.eql(schemaDef.schema);
- });
- });
- describe('getDefinition', function () {
- it('should throw an error if a given name is not registered', function () {
- const S = new DataSchemaRegistry();
- const throwable = () => S.getDefinition('mySchema');
- expect(throwable).to.throw('Schema definition "mySchema" is not found.');
- });
- it('should return the registered definition for a given name', function () {
- const S = new DataSchemaRegistry();
- const schemaDef = {name: 'mySchema', schema: {}};
- S.defineSchema(schemaDef);
- const res = S.getDefinition(schemaDef.name);
- expect(res).to.be.eql(schemaDef);
- });
- });
- });
|