connection-manager.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import type { NormalizedOptions } from '@sequelize/core';
  2. import { DataTypes, QueryTypes } from '@sequelize/core';
  3. import type { PostgresDialect } from '@sequelize/postgres';
  4. import { expect } from 'chai';
  5. import {
  6. createSingleTestSequelizeInstance,
  7. sequelize as defaultSequelize,
  8. getTestDialect,
  9. } from '../../support';
  10. const dialect = getTestDialect();
  11. describe('[POSTGRES] Sequelize', () => {
  12. if (!dialect.startsWith('postgres')) {
  13. return;
  14. }
  15. async function checkTimezoneParsing(baseOptions: NormalizedOptions<PostgresDialect>) {
  16. const options = { ...baseOptions, timezone: 'Asia/Kolkata' };
  17. const tzSequelize = createSingleTestSequelizeInstance(options);
  18. const tzTable = tzSequelize.define('tz_table', { foo: DataTypes.STRING });
  19. await tzTable.sync({ force: true });
  20. const row = await tzTable.create({ foo: 'test' });
  21. expect(row).to.be.not.null;
  22. }
  23. it('should correctly parse the timezone while fetching hstore oids', async () => {
  24. await checkTimezoneParsing(defaultSequelize.options as NormalizedOptions<PostgresDialect>);
  25. });
  26. it('should set client_min_messages to warning by default', async () => {
  27. const result = await defaultSequelize.query<{ client_min_messages: string }>(
  28. 'SHOW client_min_messages',
  29. { type: QueryTypes.SELECT },
  30. );
  31. expect(result[0].client_min_messages).to.equal('warning');
  32. });
  33. it('should allow overriding client_min_messages', async () => {
  34. const sequelize = createSingleTestSequelizeInstance<PostgresDialect>({
  35. clientMinMessages: 'ERROR',
  36. });
  37. const result = await sequelize.query<{ client_min_messages: string }>(
  38. 'SHOW client_min_messages',
  39. { type: QueryTypes.SELECT },
  40. );
  41. expect(result[0].client_min_messages).to.equal('error');
  42. });
  43. it('should not set client_min_messages if clientMinMessages is false', async () => {
  44. const sequelize = createSingleTestSequelizeInstance<PostgresDialect>({
  45. clientMinMessages: false,
  46. });
  47. const result = await sequelize.query<{ client_min_messages: string }>(
  48. 'SHOW client_min_messages',
  49. { type: QueryTypes.SELECT },
  50. );
  51. // `notice` is Postgres's default
  52. expect(result[0].client_min_messages).to.equal('notice');
  53. });
  54. it('should time out the query request when the query runs beyond the configured query_timeout', async () => {
  55. const sequelize = createSingleTestSequelizeInstance<PostgresDialect>({
  56. query_timeout: 100,
  57. });
  58. await expect(sequelize.query('select pg_sleep(2)')).to.be.rejectedWith('Query read timeout');
  59. });
  60. it('should allow overriding session variables through the `options` param', async () => {
  61. const sequelize = createSingleTestSequelizeInstance<PostgresDialect>({
  62. options: '-csearch_path=abc',
  63. });
  64. const result = await sequelize.query<{ search_path: string }>('SHOW search_path', {
  65. type: QueryTypes.SELECT,
  66. });
  67. expect(result[0].search_path).to.equal('abc');
  68. });
  69. });