raw-select.test.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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#rawSelect', () => {
  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.rawSelect(
  24. User.table,
  25. {
  26. // @ts-expect-error -- we'll fix the typings when we migrate query-generator to TypeScript
  27. attributes: ['id'],
  28. where: {
  29. username: 'some :data',
  30. },
  31. replacements: {
  32. data: "OR ' = ",
  33. },
  34. },
  35. 'id',
  36. User,
  37. );
  38. expect(stub.callCount).to.eq(1);
  39. const firstCall = stub.getCall(0);
  40. expectsql(firstCall.args[0], {
  41. default: `SELECT [id] FROM [Users] AS [User] WHERE [User].[username] = 'some :data';`,
  42. mssql: `SELECT [id] FROM [Users] AS [User] WHERE [User].[username] = N'some :data';`,
  43. });
  44. });
  45. });