sequelize.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Sequelize, sql } from '@sequelize/core';
  2. import { expect } from 'chai';
  3. import type { SinonStub } from 'sinon';
  4. import sinon from 'sinon';
  5. import { beforeEach2, createSequelizeInstance, sequelize } from '../support';
  6. describe('Sequelize', () => {
  7. describe('version', () => {
  8. it('should be a string', () => {
  9. expect(typeof Sequelize.version).to.eq('string');
  10. });
  11. });
  12. describe('query', () => {
  13. let stubs: Array<SinonStub<any>> = [];
  14. afterEach(() => {
  15. for (const stub of stubs) {
  16. stub.restore();
  17. }
  18. stubs = [];
  19. });
  20. it('supports sql expressions', async () => {
  21. // mock sequelize.queryRaw using sinon
  22. stubs.push(sinon.stub(sequelize, 'queryRaw').resolves([[], 0]));
  23. await sequelize.query(sql`SELECT * FROM "users" WHERE id = ${1} AND id2 = :id2`, {
  24. replacements: {
  25. id2: 2,
  26. },
  27. });
  28. expect(sequelize.queryRaw).to.have.been.calledWith(
  29. 'SELECT * FROM "users" WHERE id = 1 AND id2 = 2',
  30. );
  31. });
  32. });
  33. describe('close', () => {
  34. it('clears the pool & closes Sequelize', async () => {
  35. const options = {
  36. replication: null,
  37. };
  38. const sequelize2 = createSequelizeInstance(options);
  39. const poolClearSpy = sinon.spy(sequelize2.pool, 'destroyAllNow');
  40. await sequelize2.close();
  41. expect(poolClearSpy.calledOnce).to.be.true;
  42. expect(sequelize2.isClosed()).to.be.true;
  43. });
  44. });
  45. describe('init', () => {
  46. afterEach(async () => {
  47. Sequelize.hooks.removeAllListeners();
  48. });
  49. it('beforeInit hook can alter options', () => {
  50. Sequelize.hooks.addListener('beforeInit', options => {
  51. options.databaseVersion = sequelize.dialect.minimumDatabaseVersion;
  52. });
  53. const seq = createSequelizeInstance();
  54. expect(seq.getDatabaseVersion()).to.equal(sequelize.dialect.minimumDatabaseVersion);
  55. });
  56. it('afterInit hook cannot alter options', () => {
  57. Sequelize.hooks.addListener('afterInit', sequelize2 => {
  58. // @ts-expect-error -- only exists in some dialects but the principle remains identical
  59. sequelize2.options.protocol = 'udp';
  60. });
  61. expect(() => createSequelizeInstance()).to.throw();
  62. });
  63. });
  64. describe('log', () => {
  65. it('is disabled by default', () => {
  66. expect(sequelize.options.logging).to.equal(false);
  67. });
  68. describe('with a custom function for logging', () => {
  69. const vars = beforeEach2(() => {
  70. const spy = sinon.spy();
  71. return { spy, sequelize: createSequelizeInstance({ logging: spy }) };
  72. });
  73. it('calls the custom logger method', () => {
  74. vars.sequelize.log('om nom');
  75. expect(vars.spy.calledOnce).to.be.true;
  76. });
  77. it('calls the custom logger method with options', () => {
  78. const message = 'om nom';
  79. const timeTaken = 5;
  80. const options = { correlationId: 'ABC001' };
  81. vars.sequelize.log(message, timeTaken, options);
  82. expect(vars.spy.withArgs(message, timeTaken, options).calledOnce).to.be.true;
  83. });
  84. });
  85. });
  86. });