|
| 1 | +import type { Options } from '@sequelize/core'; |
| 2 | +import { ConnectionError, Sequelize } from '@sequelize/core'; |
| 3 | +import type { MsSqlDialect } from '@sequelize/mssql'; |
| 4 | +import type { RequiredBy } from '@sequelize/utils'; |
| 5 | +import { assert, expect } from 'chai'; |
| 6 | +import sinon from 'sinon'; |
| 7 | +import { Connection as TediousConnection } from 'tedious'; |
| 8 | +import { getTestDialect } from '../../../support'; |
| 9 | + |
| 10 | +const dialect = getTestDialect(); |
| 11 | + |
| 12 | +type TestConnection = Omit<TediousConnection, 'once' | 'removeListener' | 'on'> & { |
| 13 | + once(event: string, cb: () => void): void; |
| 14 | + removeListener(): void; |
| 15 | + on(): void; |
| 16 | +}; |
| 17 | + |
| 18 | +describe('[MSSQL Specific] Connection Manager', () => { |
| 19 | + if (dialect !== 'mssql') { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + let config: RequiredBy<Options<MsSqlDialect>, 'dialectOptions'>; |
| 24 | + let instance: Sequelize<MsSqlDialect>; |
| 25 | + let Connection: Partial<TestConnection>; |
| 26 | + let connectionStub: sinon.SinonStub; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + config = { |
| 30 | + dialect: 'mssql', |
| 31 | + database: 'none', |
| 32 | + username: 'none', |
| 33 | + password: 'none', |
| 34 | + host: 'localhost', |
| 35 | + port: 2433, |
| 36 | + pool: {}, |
| 37 | + dialectOptions: { |
| 38 | + domain: 'TEST.COM', |
| 39 | + }, |
| 40 | + }; |
| 41 | + instance = new Sequelize(config); |
| 42 | + Connection = {}; |
| 43 | + // @ts-expect-error -- lib is private |
| 44 | + connectionStub = sinon.stub(instance.connectionManager, 'lib').value({ |
| 45 | + Connection: function fakeConnection() { |
| 46 | + return Connection; |
| 47 | + }, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + connectionStub.restore(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('connectionManager._connect() does not delete `domain` from config.dialectOptions', async () => { |
| 56 | + Connection = { |
| 57 | + STATE: TediousConnection.prototype.STATE, |
| 58 | + state: undefined, |
| 59 | + once: (event, cb) => { |
| 60 | + if (event === 'connect') { |
| 61 | + setTimeout(() => { |
| 62 | + cb(); |
| 63 | + }, 500); |
| 64 | + } |
| 65 | + }, |
| 66 | + removeListener: () => {}, |
| 67 | + on: () => {}, |
| 68 | + }; |
| 69 | + |
| 70 | + expect(config.dialectOptions.domain).to.equal('TEST.COM'); |
| 71 | + // @ts-expect-error -- protected method |
| 72 | + await instance.dialect.connectionManager._connect(config); |
| 73 | + expect(config.dialectOptions.domain).to.equal('TEST.COM'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('connectionManager._connect() should reject if end was called and connect was not', async () => { |
| 77 | + Connection = { |
| 78 | + STATE: TediousConnection.prototype.STATE, |
| 79 | + state: undefined, |
| 80 | + once(event, cb) { |
| 81 | + if (event === 'end') { |
| 82 | + setTimeout(() => { |
| 83 | + cb(); |
| 84 | + }, 500); |
| 85 | + } |
| 86 | + }, |
| 87 | + removeListener: () => {}, |
| 88 | + on: () => {}, |
| 89 | + }; |
| 90 | + |
| 91 | + try { |
| 92 | + // @ts-expect-error -- protected method |
| 93 | + await instance.dialect.connectionManager._connect(config); |
| 94 | + assert.fail('Expected an error to be thrown'); |
| 95 | + } catch (error) { |
| 96 | + assert(error instanceof ConnectionError); |
| 97 | + expect(error.name).to.equal('SequelizeConnectionError'); |
| 98 | + assert(error.cause instanceof Error); |
| 99 | + expect(error.cause.message).to.equal('Connection was closed by remote server'); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it('connectionManager._connect() should call connect if state is initialized', async () => { |
| 104 | + const connectStub = sinon.stub(); |
| 105 | + Connection = { |
| 106 | + STATE: TediousConnection.prototype.STATE, |
| 107 | + state: TediousConnection.prototype.STATE.INITIALIZED, |
| 108 | + connect: connectStub, |
| 109 | + once(event, cb) { |
| 110 | + if (event === 'connect') { |
| 111 | + setTimeout(() => { |
| 112 | + cb(); |
| 113 | + }, 500); |
| 114 | + } |
| 115 | + }, |
| 116 | + removeListener: () => {}, |
| 117 | + on: () => {}, |
| 118 | + }; |
| 119 | + |
| 120 | + // @ts-expect-error -- protected method |
| 121 | + await instance.dialect.connectionManager._connect(config); |
| 122 | + expect(connectStub.called).to.equal(true); |
| 123 | + }); |
| 124 | +}); |
0 commit comments