connection-manager.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import type { Options } from '@sequelize/core';
  2. import { ConnectionError, Sequelize } from '@sequelize/core';
  3. import { MsSqlDialect } from '@sequelize/mssql';
  4. import { assert, expect } from 'chai';
  5. import sinon from 'sinon';
  6. import { Connection as TediousConnection } from 'tedious';
  7. import { getTestDialect } from '../../../support';
  8. const dialect = getTestDialect();
  9. type TestConnection = Omit<TediousConnection, 'once' | 'removeListener' | 'on'> & {
  10. once(event: string, cb: () => void): void;
  11. removeListener(): void;
  12. on(): void;
  13. };
  14. describe('[MSSQL Specific] Connection Manager', () => {
  15. if (dialect !== 'mssql') {
  16. return;
  17. }
  18. let config: Options<MsSqlDialect>;
  19. let instance: Sequelize<MsSqlDialect>;
  20. let Connection: Partial<TestConnection>;
  21. beforeEach(() => {
  22. Connection = {};
  23. const tediousModule = {
  24. Connection: function fakeConnection() {
  25. return Connection;
  26. },
  27. } as any;
  28. config = {
  29. dialect: MsSqlDialect,
  30. server: 'localhost',
  31. authentication: {
  32. type: 'default',
  33. options: {
  34. domain: 'TEST.COM',
  35. userName: 'none',
  36. password: 'none',
  37. },
  38. },
  39. pool: {},
  40. port: 2433,
  41. tediousModule,
  42. };
  43. instance = new Sequelize<MsSqlDialect>(config);
  44. });
  45. it('connectionManager.connect() should reject if end was called and connect was not', async () => {
  46. Connection = {
  47. STATE: TediousConnection.prototype.STATE,
  48. state: undefined,
  49. once(event, cb) {
  50. if (event === 'end') {
  51. setTimeout(() => {
  52. cb();
  53. }, 500);
  54. }
  55. },
  56. removeListener: () => {},
  57. on: () => {},
  58. };
  59. const error = await expect(instance.dialect.connectionManager.connect(config)).to.be.rejected;
  60. assert(error instanceof ConnectionError);
  61. expect(error.name).to.equal('SequelizeConnectionError');
  62. assert(error.cause instanceof Error);
  63. expect(error.cause.message).to.equal('Connection was closed by remote server');
  64. });
  65. it('connectionManager.connect() should call connect if state is initialized', async () => {
  66. const connectStub = sinon.stub();
  67. Connection = {
  68. STATE: TediousConnection.prototype.STATE,
  69. state: TediousConnection.prototype.STATE.INITIALIZED,
  70. connect: connectStub,
  71. once(event, cb) {
  72. if (event === 'connect') {
  73. setTimeout(() => {
  74. cb();
  75. }, 500);
  76. }
  77. },
  78. removeListener: () => {},
  79. on: () => {},
  80. };
  81. await instance.dialect.connectionManager.connect(config);
  82. expect(connectStub.called).to.equal(true);
  83. });
  84. it('connectionManager.connect() should not fail with an instanceName but no port specified in config', async () => {
  85. const connectStub = sinon.stub();
  86. Connection = {
  87. STATE: TediousConnection.prototype.STATE,
  88. state: TediousConnection.prototype.STATE.INITIALIZED,
  89. connect: connectStub,
  90. once(event, cb) {
  91. if (event === 'connect') {
  92. setTimeout(() => {
  93. cb();
  94. }, 500);
  95. }
  96. },
  97. removeListener: () => {},
  98. on: () => {},
  99. };
  100. config.instanceName = 'INSTANCENAME';
  101. await instance.dialect.connectionManager.connect(config);
  102. expect(connectStub.called).to.equal(true);
  103. });
  104. });