configuration.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { Sequelize } from '@sequelize/core';
  2. import { PostgresDialect } from '@sequelize/postgres';
  3. import { expect } from 'chai';
  4. import sinon from 'sinon';
  5. import {
  6. allowDeprecationsInSuite,
  7. createSequelizeInstance,
  8. getTestDialectClass,
  9. sequelize,
  10. } from '../support';
  11. const dialect = getTestDialectClass();
  12. const dialectName = sequelize.dialect.name;
  13. describe('Sequelize constructor', () => {
  14. allowDeprecationsInSuite(['SEQUELIZE0027']);
  15. it('throws when no dialect is supplied', () => {
  16. expect(() => {
  17. // @ts-expect-error -- testing that this throws when the "dialect" option is missing
  18. new Sequelize({});
  19. }).to.throw(Error);
  20. });
  21. it('throws when an invalid dialect is supplied', () => {
  22. expect(() => {
  23. // @ts-expect-error -- testing that this throws
  24. new Sequelize({ dialect: 'some-fancy-dialect' });
  25. }).to.throw(
  26. Error,
  27. 'The dialect some-fancy-dialect is not natively supported. Native dialects: mariadb, mssql, mysql, postgres, sqlite3, ibmi, db2 and snowflake.',
  28. );
  29. });
  30. it('works when dialect is supplied', () => {
  31. expect(() => {
  32. new Sequelize({
  33. dialect,
  34. });
  35. }).not.to.throw();
  36. });
  37. it('throws if pool:false', () => {
  38. expect(() => {
  39. new Sequelize({
  40. dialect,
  41. // @ts-expect-error -- we're testing that this throws an error
  42. pool: false,
  43. });
  44. }).to.throw(
  45. 'Setting the "pool" option to "false" is not supported since Sequelize 4. To disable the pool, set the "pool"."max" option to 1.',
  46. );
  47. });
  48. it('warns if the database version is not supported', () => {
  49. const stub = sinon.stub(process, 'emitWarning');
  50. try {
  51. createSequelizeInstance({ databaseVersion: '0.0.1' });
  52. expect(stub.getCalls()[0].args[0]).to.contain(
  53. 'This database engine version is not supported, please update your database server.',
  54. );
  55. } finally {
  56. stub.restore();
  57. }
  58. });
  59. describe('Network Connections', () => {
  60. // This test suite runs only for postgres but the logic is the same for every dialect
  61. if (dialectName !== 'postgres') {
  62. return;
  63. }
  64. it('should correctly set the host and the port', () => {
  65. const localSequelize = new Sequelize({
  66. dialect: PostgresDialect,
  67. host: '127.0.0.1',
  68. port: 1234,
  69. });
  70. expect(localSequelize.options.replication.write.port).to.equal(1234);
  71. expect(localSequelize.options.replication.write.host).to.equal('127.0.0.1');
  72. });
  73. it('accepts a single URI parameter', () => {
  74. const newSequelize = new Sequelize({
  75. dialect: PostgresDialect,
  76. url: `${dialectName}://user:pass@example.com:9821/dbname`,
  77. });
  78. const replication = newSequelize.options.replication;
  79. expect(replication.write).to.deep.eq({
  80. database: 'dbname',
  81. user: 'user',
  82. password: 'pass',
  83. host: 'example.com',
  84. port: 9821,
  85. });
  86. expect(replication.read).to.deep.eq([]);
  87. });
  88. it('supports not providing username, password, or port in URI, but providing them in the option bag', () => {
  89. const options = {
  90. port: 10,
  91. user: 'root',
  92. password: 'pass',
  93. database: 'dbname',
  94. };
  95. const newSequelize = new Sequelize({
  96. dialect: PostgresDialect,
  97. url: `${dialectName}://example.com/dbname`,
  98. ...options,
  99. });
  100. expect(newSequelize.options.replication.write).to.deep.eq({
  101. host: 'example.com',
  102. ...options,
  103. });
  104. });
  105. it('supports connection strings in replication options', async () => {
  106. const url = `${dialectName}://username:password@host:1234/database`;
  107. const newSequelize = new Sequelize<PostgresDialect>({
  108. dialect: PostgresDialect,
  109. replication: {
  110. write: url,
  111. read: [url],
  112. },
  113. });
  114. const options = {
  115. host: 'host',
  116. database: 'database',
  117. port: 1234,
  118. user: 'username',
  119. password: 'password',
  120. };
  121. expect(newSequelize.options.replication.write).to.deep.eq(options);
  122. expect(newSequelize.options.replication.read).to.deep.eq([options]);
  123. });
  124. it('prioritizes the option bag over the URI', () => {
  125. const newSequelize = new Sequelize<PostgresDialect>({
  126. dialect: PostgresDialect,
  127. url: `${dialectName}://localhost:9821/dbname?ssl=true`,
  128. host: 'localhost2',
  129. user: 'username2',
  130. password: 'password2',
  131. ssl: false,
  132. port: 2000,
  133. database: 'dbname2',
  134. });
  135. const replication = newSequelize.options.replication;
  136. expect(replication.write).to.deep.eq({
  137. database: 'dbname2',
  138. host: 'localhost2',
  139. password: 'password2',
  140. port: 2000,
  141. ssl: false,
  142. user: 'username2',
  143. });
  144. expect(replication.read).to.deep.eq([]);
  145. });
  146. });
  147. });