bulk-create.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. const { expect } = require('chai');
  3. const sinon = require('sinon');
  4. const { beforeAll2, sequelize } = require('../../support');
  5. const { DataTypes } = require('@sequelize/core');
  6. describe('Model#bulkCreate', () => {
  7. const vars = beforeAll2(() => {
  8. const TestModel = sequelize.define(
  9. 'TestModel',
  10. {
  11. accountId: {
  12. type: DataTypes.INTEGER(11).UNSIGNED,
  13. allowNull: false,
  14. field: 'account_id',
  15. },
  16. purchaseCount: {
  17. type: DataTypes.INTEGER(11).UNSIGNED,
  18. allowNull: false,
  19. underscored: true,
  20. },
  21. },
  22. { timestamps: false },
  23. );
  24. const stub = sinon.stub(sequelize.queryInterface, 'bulkInsert').resolves([]);
  25. return { TestModel, stub };
  26. });
  27. afterEach(() => {
  28. vars.stub.resetHistory();
  29. });
  30. after(() => {
  31. vars.stub.restore();
  32. });
  33. describe('validations', () => {
  34. it('should not fail for renamed fields', async () => {
  35. const { stub, TestModel } = vars;
  36. await TestModel.bulkCreate([{ accountId: 42, purchaseCount: 4 }], {
  37. validate: true,
  38. });
  39. expect(stub.getCall(0).args[1]).to.deep.equal([
  40. { account_id: 42, purchaseCount: 4, id: null },
  41. ]);
  42. });
  43. if (sequelize.dialect.supports.inserts.updateOnDuplicate) {
  44. it('should map conflictAttributes to column names', async () => {
  45. const { stub, TestModel } = vars;
  46. // Note that the model also has an id key as its primary key.
  47. await TestModel.bulkCreate([{ accountId: 42, purchaseCount: 3 }], {
  48. conflictAttributes: ['accountId'],
  49. updateOnDuplicate: ['purchaseCount'],
  50. });
  51. expect(
  52. // Not worth checking that the reference of the array matches - just the contents.
  53. stub.getCall(0).args[2].upsertKeys,
  54. ).to.deep.equal(['account_id']);
  55. });
  56. }
  57. });
  58. });