schema.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { literal } from '@sequelize/core';
  2. import { expect } from 'chai';
  3. import assert from 'node:assert';
  4. import {
  5. allowDeprecationsInSuite,
  6. beforeAll2,
  7. getTestDialectTeaser,
  8. sequelize,
  9. } from '../../support';
  10. describe(`${getTestDialectTeaser('Model')}Schemas`, () => {
  11. allowDeprecationsInSuite(['SEQUELIZE0009']);
  12. if (!sequelize.dialect.supports.schemas) {
  13. return;
  14. }
  15. const vars = beforeAll2(() => {
  16. const Project = sequelize.define('project');
  17. const Company = sequelize.define(
  18. 'company',
  19. {},
  20. {
  21. schema: 'default',
  22. schemaDelimiter: '&',
  23. },
  24. );
  25. Project.addScope('scope1', { where: literal('') });
  26. return { Project, Company };
  27. });
  28. describe('schema', () => {
  29. it('should work with no default schema', () => {
  30. const { Project } = vars;
  31. expect(Project.table.schema).to.equal(sequelize.dialect.getDefaultSchema());
  32. });
  33. it('should apply default schema from define', () => {
  34. const { Company } = vars;
  35. expect(Company.table.schema).to.equal('default');
  36. });
  37. it('returns the same model if the schema is equal', () => {
  38. const { Project } = vars;
  39. assert(
  40. // eslint-disable-next-line no-self-compare -- value could be different.
  41. Project.withSchema('newSchema') === Project.withSchema('newSchema'),
  42. 'withSchema should have returned the same model if the schema is equal',
  43. );
  44. });
  45. it('returns a new model if the schema is equal, but scope is different', () => {
  46. const { Project } = vars;
  47. expect(Project.withScope('scope1').withSchema('newSchema')).not.to.equal(
  48. Project.withSchema('newSchema'),
  49. );
  50. });
  51. it('returns the current model if the schema is identical', () => {
  52. const { Project } = vars;
  53. assert(Project.withSchema('') === Project);
  54. assert(Project.withSchema('test').withSchema('test') === Project.withSchema('test'));
  55. });
  56. it('should be able to override the default schema', () => {
  57. const { Company } = vars;
  58. expect(Company.withSchema('newSchema').table.schema).to.equal('newSchema');
  59. });
  60. it('should be able to override the default schema using deprecated schema', () => {
  61. const { Company } = vars;
  62. expect(Company.schema('newSchema').table.schema).to.equal('newSchema');
  63. });
  64. it('should be able nullify schema', () => {
  65. const { Company } = vars;
  66. expect(Company.withSchema(null).table.schema).to.equal(sequelize.dialect.getDefaultSchema());
  67. });
  68. it('should support multiple, coexistent schema models', () => {
  69. const { Company } = vars;
  70. const schema1 = Company.withSchema('schema1');
  71. const schema2 = Company.withSchema('schema1');
  72. expect(schema1.table.schema).to.equal('schema1');
  73. expect(schema2.table.schema).to.equal('schema1');
  74. });
  75. });
  76. describe('schema delimiter', () => {
  77. it('should work with no default schema delimiter', () => {
  78. const { Project } = vars;
  79. expect(Project.table.delimiter).to.equal('.');
  80. });
  81. it('should apply default schema delimiter from define', () => {
  82. const { Company } = vars;
  83. expect(Company.table.delimiter).to.equal('&');
  84. });
  85. it('should be able to override the default schema delimiter', () => {
  86. const { Company } = vars;
  87. expect(
  88. Company.withSchema({ schema: Company.table.schema!, schemaDelimiter: '^' }).table.delimiter,
  89. ).to.equal('^');
  90. });
  91. it('should be able to override the default schema delimiter using deprecated schema', () => {
  92. const { Company } = vars;
  93. expect(Company.schema(Company.table.schema, '^').table.delimiter).to.equal('^');
  94. });
  95. it('should be able to override the default schema delimiter using deprecated schema with schema object', () => {
  96. const { Company } = vars;
  97. expect(
  98. Company.schema(Company.table.schema, { schemaDelimiter: '^' }).table.delimiter,
  99. ).to.equal('^');
  100. });
  101. it('should support multiple, coexistent schema delimiter models', () => {
  102. const { Company } = vars;
  103. const schema1 = Company.withSchema({ schema: Company.table.schema!, schemaDelimiter: '$' });
  104. const schema2 = Company.withSchema({ schema: Company.table.schema!, schemaDelimiter: '#' });
  105. expect(schema1.table.delimiter).to.equal('$');
  106. expect(schema2.table.delimiter).to.equal('#');
  107. });
  108. });
  109. });