| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import {expect} from 'chai';
- import {Repository} from './repository/index.js';
- import {DatabaseSchema} from './database-schema.js';
- import {DefinitionRegistry} from './definition/index.js';
- describe('DatabaseSchema', function () {
- describe('defineDatasource', function () {
- it('returns this', function () {
- const dbs = new DatabaseSchema();
- const res = dbs.defineDatasource({
- name: 'datasource',
- adapter: 'memory',
- });
- expect(res).to.be.eq(dbs);
- });
- it('sets the datasource definition', function () {
- const dbs = new DatabaseSchema();
- dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
- const res = dbs
- .getService(DefinitionRegistry)
- .getDatasource('datasource');
- expect(res).to.be.eql({name: 'datasource', adapter: 'memory'});
- });
- it('throws an error if the datasource name already defined', function () {
- const dbs = new DatabaseSchema();
- dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
- const throwable = () =>
- dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
- expect(throwable).to.throw(
- 'The datasource "datasource" is already defined.',
- );
- });
- });
- describe('defineModel', function () {
- it('returns this', function () {
- const dbs = new DatabaseSchema();
- const res = dbs.defineModel({name: 'model'});
- expect(res).to.be.eq(dbs);
- });
- it('sets the model definition', function () {
- const dbs = new DatabaseSchema();
- dbs.defineModel({name: 'model'});
- const res = dbs.getService(DefinitionRegistry).getModel('model');
- expect(res).to.be.eql({name: 'model'});
- });
- it('throws an error if the model name already defined', function () {
- const dbs = new DatabaseSchema();
- dbs.defineModel({name: 'model'});
- const throwable = () => dbs.defineModel({name: 'model'});
- expect(throwable).to.throw('The model "model" is already defined.');
- });
- });
- describe('getRepository', function () {
- it('returns a repository by the model name', function () {
- const dbs = new DatabaseSchema();
- dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
- dbs.defineModel({name: 'model', datasource: 'datasource'});
- const res = dbs.getRepository('model');
- expect(res).to.be.instanceof(Repository);
- });
- it('throws an error if the model is not defined', function () {
- const dbs = new DatabaseSchema();
- const throwable = () => dbs.getRepository('model');
- expect(throwable).to.throw('The model "model" is not defined.');
- });
- });
- });
|