select.test.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { DataTypes } from '@sequelize/core';
  2. import { expect } from 'chai';
  3. import sinon from 'sinon';
  4. import { beforeAll2, expectsql, sequelize } from '../../support';
  5. describe('QueryInterface#select', () => {
  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 user-provided data as replacements', async () => {
  21. const { User } = vars;
  22. const stub = sinon.stub(sequelize, 'queryRaw');
  23. await sequelize.queryInterface.select(User, User.table, {
  24. // @ts-expect-error -- we'll fix the typings when we migrate query-generator to TypeScript
  25. attributes: ['id'],
  26. where: {
  27. username: 'some :data',
  28. },
  29. replacements: {
  30. data: "OR ' = ",
  31. },
  32. });
  33. expect(stub.callCount).to.eq(1);
  34. const firstCall = stub.getCall(0);
  35. expectsql(firstCall.args[0], {
  36. default: `SELECT [id] FROM [Users] AS [User] WHERE [User].[username] = 'some :data';`,
  37. mssql: `SELECT [id] FROM [Users] AS [User] WHERE [User].[username] = N'some :data';`,
  38. });
  39. });
  40. });