operators.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. const { DataTypes, Op } = require('@sequelize/core');
  6. const dialect = Support.getTestDialect();
  7. describe(Support.getTestDialectTeaser('Operators'), () => {
  8. describe('REGEXP', () => {
  9. beforeEach(async function () {
  10. this.User = this.sequelize.define(
  11. 'user',
  12. {
  13. id: {
  14. type: DataTypes.INTEGER,
  15. allowNull: false,
  16. primaryKey: true,
  17. autoIncrement: true,
  18. field: 'userId',
  19. },
  20. name: {
  21. type: DataTypes.STRING,
  22. field: 'full_name',
  23. },
  24. },
  25. {
  26. tableName: 'users',
  27. timestamps: false,
  28. },
  29. );
  30. await this.sequelize.queryInterface.createTable('users', {
  31. userId: {
  32. type: DataTypes.INTEGER,
  33. allowNull: false,
  34. primaryKey: true,
  35. autoIncrement: true,
  36. },
  37. full_name: {
  38. type: DataTypes.STRING,
  39. },
  40. });
  41. });
  42. if (['mysql', 'postgres'].includes(dialect)) {
  43. describe('case sensitive', () => {
  44. it('should work with a regexp where', async function () {
  45. await this.User.create({ name: 'Foobar' });
  46. const user = await this.User.findOne({
  47. where: {
  48. name: { [Op.regexp]: '^Foo' },
  49. },
  50. });
  51. expect(user).to.be.ok;
  52. });
  53. it('should work with a not regexp where', async function () {
  54. await this.User.create({ name: 'Foobar' });
  55. const user = await this.User.findOne({
  56. where: {
  57. name: { [Op.notRegexp]: '^Foo' },
  58. },
  59. });
  60. expect(user).to.not.be.ok;
  61. });
  62. it('should properly escape regular expressions', async function () {
  63. await this.User.bulkCreate([{ name: 'John' }, { name: 'Bob' }]);
  64. await this.User.findAll({
  65. where: {
  66. name: { [Op.notRegexp]: "Bob'; drop table users --" },
  67. },
  68. });
  69. await this.User.findAll({
  70. where: {
  71. name: { [Op.regexp]: "Bob'; drop table users --" },
  72. },
  73. });
  74. expect(await this.User.findAll()).to.have.length(2);
  75. });
  76. });
  77. }
  78. if (dialect === 'postgres') {
  79. describe('case insensitive', () => {
  80. it('should work with a case-insensitive regexp where', async function () {
  81. await this.User.create({ name: 'Foobar' });
  82. const user = await this.User.findOne({
  83. where: {
  84. name: { [Op.iRegexp]: '^foo' },
  85. },
  86. });
  87. expect(user).to.be.ok;
  88. });
  89. it('should work with a case-insensitive not regexp where', async function () {
  90. await this.User.create({ name: 'Foobar' });
  91. const user = await this.User.findOne({
  92. where: {
  93. name: { [Op.notIRegexp]: '^foo' },
  94. },
  95. });
  96. expect(user).to.not.be.ok;
  97. });
  98. it('should properly escape regular expressions', async function () {
  99. await this.User.bulkCreate([{ name: 'John' }, { name: 'Bob' }]);
  100. await this.User.findAll({
  101. where: {
  102. name: { [Op.iRegexp]: "Bob'; drop table users --" },
  103. },
  104. });
  105. await this.User.findAll({
  106. where: {
  107. name: { [Op.notIRegexp]: "Bob'; drop table users --" },
  108. },
  109. });
  110. expect(await this.User.findAll()).to.have.length(2);
  111. });
  112. });
  113. }
  114. });
  115. });