internals.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import type { Transactionable } from '@sequelize/core';
  2. import { setTransactionFromCls } from '@sequelize/core/_non-semver-use-at-your-own-risk_/model-internals.js';
  3. import { expect } from 'chai';
  4. import { beforeAll2, createSequelizeInstance, getTestDialect } from '../../support';
  5. const dialectName = getTestDialect();
  6. describe('setTransactionFromCls', () => {
  7. const vars = beforeAll2(() => {
  8. const sequelize = createSequelizeInstance({
  9. disableClsTransactions: false,
  10. });
  11. return { sequelize };
  12. });
  13. after(async () => {
  14. return vars.sequelize.close();
  15. });
  16. it('sets the transaction & connection if they exists in CLS', async () => {
  17. const { sequelize } = vars;
  18. await sequelize.transaction(transaction => {
  19. const options: Transactionable = {};
  20. setTransactionFromCls(options, sequelize);
  21. expect(options.transaction).to.eq(transaction);
  22. expect(options.connection).to.eq(transaction.getConnection());
  23. });
  24. });
  25. it('does not use CLS if a transaction is already provided', async () => {
  26. // SQLite only has a single connection, we can't open a second transaction
  27. if (dialectName === 'sqlite3') {
  28. return;
  29. }
  30. const { sequelize } = vars;
  31. const manualTransaction = await sequelize.startUnmanagedTransaction();
  32. await sequelize.transaction(async () => {
  33. const options: Transactionable = { transaction: manualTransaction };
  34. setTransactionFromCls(options, sequelize);
  35. expect(options.transaction).to.eq(manualTransaction);
  36. expect(options.connection).to.eq(manualTransaction.getConnection());
  37. });
  38. await manualTransaction.commit();
  39. });
  40. it('does not use CLS if null is explicitly provided', async () => {
  41. const { sequelize } = vars;
  42. await sequelize.transaction(async () => {
  43. const options: Transactionable = { transaction: null };
  44. setTransactionFromCls(options, sequelize);
  45. expect(options.transaction).to.eq(null);
  46. expect(options.connection).to.eq(undefined);
  47. });
  48. });
  49. it('does not set the transaction from CLS if an incompatible connection is provided', async () => {
  50. // SQLite only has a single connection, so it's the same connection
  51. if (dialectName === 'sqlite3') {
  52. return;
  53. }
  54. const { sequelize } = vars;
  55. await sequelize.transaction(async () => {
  56. await sequelize.withConnection(async connection => {
  57. const options: Transactionable = { connection };
  58. setTransactionFromCls(options, sequelize);
  59. expect(options.transaction).to.eq(undefined);
  60. expect(options.connection).to.eq(connection);
  61. });
  62. });
  63. });
  64. it('does not set the transaction from CLS if a compatible connection is provided', async () => {
  65. const { sequelize } = vars;
  66. await sequelize.transaction(async transaction => {
  67. const options: Transactionable = { connection: transaction.getConnection() };
  68. setTransactionFromCls(options, sequelize);
  69. expect(options.transaction).to.eq(transaction);
  70. expect(options.connection).to.eq(transaction.getConnection());
  71. });
  72. });
  73. it('does not allow mismatching connection & transaction', async () => {
  74. // SQLite only has a single connection, so it's the same connection
  75. if (dialectName === 'sqlite3') {
  76. return;
  77. }
  78. const { sequelize } = vars;
  79. await sequelize.transaction(async transaction => {
  80. await sequelize.withConnection(async connection => {
  81. const options: Transactionable = { transaction, connection };
  82. expect(() => setTransactionFromCls(options, sequelize)).to.throw(
  83. `You are using mismatching "transaction" and "connection" options. Please pass either one of them, or make sure they're both using the same connection.`,
  84. );
  85. });
  86. });
  87. });
  88. });