database-schema.spec.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {expect} from 'chai';
  2. import {Repository} from './repository/index.js';
  3. import {DatabaseSchema} from './database-schema.js';
  4. import {DefinitionRegistry} from './definition/index.js';
  5. describe('DatabaseSchema', function () {
  6. describe('defineDatasource', function () {
  7. it('returns this', function () {
  8. const dbs = new DatabaseSchema();
  9. const res = dbs.defineDatasource({
  10. name: 'datasource',
  11. adapter: 'memory',
  12. });
  13. expect(res).to.be.eq(dbs);
  14. });
  15. it('sets the datasource definition', function () {
  16. const dbs = new DatabaseSchema();
  17. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  18. const res = dbs
  19. .getService(DefinitionRegistry)
  20. .getDatasource('datasource');
  21. expect(res).to.be.eql({name: 'datasource', adapter: 'memory'});
  22. });
  23. it('throws an error if the datasource name already defined', function () {
  24. const dbs = new DatabaseSchema();
  25. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  26. const throwable = () =>
  27. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  28. expect(throwable).to.throw(
  29. 'The datasource "datasource" is already defined.',
  30. );
  31. });
  32. });
  33. describe('defineModel', function () {
  34. it('returns this', function () {
  35. const dbs = new DatabaseSchema();
  36. const res = dbs.defineModel({name: 'model'});
  37. expect(res).to.be.eq(dbs);
  38. });
  39. it('sets the model definition', function () {
  40. const dbs = new DatabaseSchema();
  41. dbs.defineModel({name: 'model'});
  42. const res = dbs.getService(DefinitionRegistry).getModel('model');
  43. expect(res).to.be.eql({name: 'model'});
  44. });
  45. it('throws an error if the model name already defined', function () {
  46. const dbs = new DatabaseSchema();
  47. dbs.defineModel({name: 'model'});
  48. const throwable = () => dbs.defineModel({name: 'model'});
  49. expect(throwable).to.throw('The model "model" is already defined.');
  50. });
  51. });
  52. describe('getRepository', function () {
  53. it('returns a repository by the model name', function () {
  54. const dbs = new DatabaseSchema();
  55. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  56. dbs.defineModel({name: 'model', datasource: 'datasource'});
  57. const res = dbs.getRepository('model');
  58. expect(res).to.be.instanceof(Repository);
  59. });
  60. it('throws an error if the model is not defined', function () {
  61. const dbs = new DatabaseSchema();
  62. const throwable = () => dbs.getRepository('model');
  63. expect(throwable).to.throw('The model "model" is not defined.');
  64. });
  65. });
  66. });