insert.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { DataTypes, literal } from '@sequelize/core';
  2. import { expect } from 'chai';
  3. import sinon from 'sinon';
  4. import { beforeAll2, expectsql, sequelize } from '../../support';
  5. describe('QueryInterface#insert', () => {
  6. const vars = beforeAll2(() => {
  7. const User = sequelize.define(
  8. 'User',
  9. {
  10. firstName: DataTypes.STRING,
  11. },
  12. { timestamps: false },
  13. );
  14. return { User };
  15. });
  16. afterEach(() => {
  17. sinon.restore();
  18. });
  19. // you'll find more replacement tests in query-generator tests
  20. it('does not parse replacements outside of raw sql', async () => {
  21. const { User } = vars;
  22. const stub = sinon.stub(sequelize, 'queryRaw');
  23. await sequelize.queryInterface.insert(
  24. null,
  25. User.table,
  26. {
  27. firstName: 'Zoe',
  28. },
  29. {
  30. returning: [':data'],
  31. replacements: {
  32. data: 'abc',
  33. },
  34. },
  35. );
  36. expect(stub.callCount).to.eq(1);
  37. const firstCall = stub.getCall(0);
  38. expectsql(firstCall.args[0], {
  39. default: 'INSERT INTO [Users] ([firstName]) VALUES ($sequelize_1);',
  40. sqlite3: 'INSERT INTO `Users` (`firstName`) VALUES ($sequelize_1) RETURNING `:data`;',
  41. postgres: `INSERT INTO "Users" ("firstName") VALUES ($sequelize_1) RETURNING ":data";`,
  42. mssql: `INSERT INTO [Users] ([firstName]) OUTPUT INSERTED.[:data] VALUES ($sequelize_1);`,
  43. db2: `SELECT * FROM FINAL TABLE (INSERT INTO "Users" ("firstName") VALUES ($sequelize_1));`,
  44. ibmi: `SELECT * FROM FINAL TABLE (INSERT INTO "Users" ("firstName") VALUES ($sequelize_1))`,
  45. });
  46. expect(firstCall.args[1]?.bind).to.deep.eq({
  47. sequelize_1: 'Zoe',
  48. });
  49. });
  50. it('throws if a bind parameter name starts with the reserved "sequelize_" prefix', async () => {
  51. const { User } = vars;
  52. sinon.stub(sequelize, 'queryRaw');
  53. await expect(
  54. sequelize.queryInterface.insert(
  55. null,
  56. User.table,
  57. {
  58. firstName: literal('$sequelize_test'),
  59. },
  60. {
  61. bind: {
  62. sequelize_test: 'test',
  63. },
  64. },
  65. ),
  66. ).to.be.rejectedWith(
  67. 'Bind parameters cannot start with "sequelize_", these bind parameters are reserved by Sequelize.',
  68. );
  69. });
  70. it('merges user-provided bind parameters with sequelize-generated bind parameters (object bind)', async () => {
  71. const { User } = vars;
  72. const stub = sinon.stub(sequelize, 'queryRaw');
  73. await sequelize.queryInterface.insert(
  74. null,
  75. User.table,
  76. {
  77. firstName: literal('$firstName'),
  78. lastName: 'Doe',
  79. },
  80. {
  81. bind: {
  82. firstName: 'John',
  83. },
  84. },
  85. );
  86. expect(stub.callCount).to.eq(1);
  87. const firstCall = stub.getCall(0);
  88. expectsql(firstCall.args[0], {
  89. default: 'INSERT INTO [Users] ([firstName],[lastName]) VALUES ($firstName,$sequelize_1);',
  90. db2: `SELECT * FROM FINAL TABLE (INSERT INTO "Users" ("firstName","lastName") VALUES ($firstName,$sequelize_1));`,
  91. ibmi: `SELECT * FROM FINAL TABLE (INSERT INTO "Users" ("firstName","lastName") VALUES ($firstName,$sequelize_1))`,
  92. });
  93. expect(firstCall.args[1]?.bind).to.deep.eq({
  94. firstName: 'John',
  95. sequelize_1: 'Doe',
  96. });
  97. });
  98. it('merges user-provided bind parameters with sequelize-generated bind parameters (array bind)', async () => {
  99. const { User } = vars;
  100. const stub = sinon.stub(sequelize, 'queryRaw');
  101. await sequelize.queryInterface.insert(
  102. null,
  103. User.table,
  104. {
  105. firstName: literal('$1'),
  106. lastName: 'Doe',
  107. },
  108. {
  109. bind: ['John'],
  110. },
  111. );
  112. expect(stub.callCount).to.eq(1);
  113. const firstCall = stub.getCall(0);
  114. expectsql(firstCall.args[0], {
  115. default: 'INSERT INTO [Users] ([firstName],[lastName]) VALUES ($1,$sequelize_1);',
  116. db2: `SELECT * FROM FINAL TABLE (INSERT INTO "Users" ("firstName","lastName") VALUES ($1,$sequelize_1));`,
  117. ibmi: `SELECT * FROM FINAL TABLE (INSERT INTO "Users" ("firstName","lastName") VALUES ($1,$sequelize_1))`,
  118. });
  119. expect(firstCall.args[1]?.bind).to.deep.eq({
  120. 1: 'John',
  121. sequelize_1: 'Doe',
  122. });
  123. });
  124. });