arithmetic-query.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { DataTypes, literal } from '@sequelize/core';
  2. import { beforeAll2, expectsql, sequelize } from '../../support';
  3. describe('QueryGenerator#arithmeticQuery', () => {
  4. const queryGenerator = sequelize.queryGenerator;
  5. const vars = beforeAll2(() => {
  6. const User = sequelize.define(
  7. 'User',
  8. {
  9. firstName: DataTypes.STRING,
  10. },
  11. { timestamps: false },
  12. );
  13. return { User };
  14. });
  15. it('uses the specified operator', async () => {
  16. const sqlPlus = queryGenerator.arithmeticQuery('+', 'myTable', {}, { foo: 3 }, {}, {});
  17. const sqlMinus = queryGenerator.arithmeticQuery('-', 'myTable', {}, { foo: 3 }, {}, {});
  18. expectsql(sqlPlus, {
  19. default: `UPDATE [myTable] SET [foo]=[foo]+ 3`,
  20. mssql: `UPDATE [myTable] SET [foo]=[foo]+ 3 OUTPUT INSERTED.*`,
  21. sqlite3: 'UPDATE `myTable` SET `foo`=`foo`+ 3 RETURNING *',
  22. postgres: `UPDATE "myTable" SET "foo"="foo"+ 3 RETURNING *`,
  23. });
  24. expectsql(sqlMinus, {
  25. default: `UPDATE [myTable] SET [foo]=[foo]- 3`,
  26. mssql: `UPDATE [myTable] SET [foo]=[foo]- 3 OUTPUT INSERTED.*`,
  27. sqlite3: 'UPDATE `myTable` SET `foo`=`foo`- 3 RETURNING *',
  28. postgres: `UPDATE "myTable" SET "foo"="foo"- 3 RETURNING *`,
  29. });
  30. });
  31. it('uses the specified operator with literal', async () => {
  32. const sql = queryGenerator.arithmeticQuery('+', 'myTable', {}, { foo: literal('bar') }, {}, {});
  33. expectsql(sql, {
  34. default: `UPDATE [myTable] SET [foo]=[foo]+ bar`,
  35. mssql: `UPDATE [myTable] SET [foo]=[foo]+ bar OUTPUT INSERTED.*`,
  36. sqlite3: 'UPDATE `myTable` SET `foo`=`foo`+ bar RETURNING *',
  37. postgres: `UPDATE "myTable" SET "foo"="foo"+ bar RETURNING *`,
  38. });
  39. });
  40. it('supports specifying a WHERE clause', async () => {
  41. const sql = queryGenerator.arithmeticQuery('+', 'myTable', { bar: 'biz' }, { foo: 3 }, {}, {});
  42. expectsql(sql, {
  43. default: `UPDATE [myTable] SET [foo]=[foo]+ 3 WHERE [bar] = 'biz'`,
  44. mssql: `UPDATE [myTable] SET [foo]=[foo]+ 3 OUTPUT INSERTED.* WHERE [bar] = N'biz'`,
  45. sqlite3: "UPDATE `myTable` SET `foo`=`foo`+ 3 WHERE `bar` = 'biz' RETURNING *",
  46. postgres: `UPDATE "myTable" SET "foo"="foo"+ 3 WHERE "bar" = 'biz' RETURNING *`,
  47. });
  48. });
  49. it('supports omitting the RETURNING clause', async () => {
  50. const sql = queryGenerator.arithmeticQuery(
  51. '+',
  52. 'myTable',
  53. {},
  54. { foo: 3 },
  55. {},
  56. { returning: false },
  57. );
  58. expectsql(sql, {
  59. default: `UPDATE [myTable] SET [foo]=[foo]+ 3`,
  60. });
  61. });
  62. it('does not cause a syntax error when the minus operator is used with a negative value', async () => {
  63. const sql = queryGenerator.arithmeticQuery('-', 'myTable', {}, { foo: -1 }, {}, {});
  64. expectsql(sql, {
  65. default: `UPDATE [myTable] SET [foo]=[foo]- -1`,
  66. mssql: `UPDATE [myTable] SET [foo]=[foo]- -1 OUTPUT INSERTED.*`,
  67. sqlite3: 'UPDATE `myTable` SET `foo`=`foo`- -1 RETURNING *',
  68. postgres: `UPDATE "myTable" SET "foo"="foo"- -1 RETURNING *`,
  69. });
  70. });
  71. // you'll find more replacement tests in query-generator tests
  72. it('parses named replacements in literals', async () => {
  73. const { User } = vars;
  74. const sql = queryGenerator.arithmeticQuery(
  75. '+',
  76. User.table,
  77. // where
  78. literal('id = :id'),
  79. // increment by field
  80. {
  81. age: literal(':age'),
  82. },
  83. // extraAttributesToBeUpdated
  84. {
  85. name: literal(':name'),
  86. },
  87. {
  88. replacements: {
  89. id: 47,
  90. age: 2,
  91. name: 'John',
  92. },
  93. },
  94. );
  95. expectsql(sql, {
  96. default: `UPDATE [Users] SET [age]=[age]+ 2,[name]='John' WHERE id = 47`,
  97. mssql: `UPDATE [Users] SET [age]=[age]+ 2,[name]=N'John' OUTPUT INSERTED.* WHERE id = 47`,
  98. sqlite3: "UPDATE `Users` SET `age`=`age`+ 2,`name`='John' WHERE id = 47 RETURNING *",
  99. postgres: `UPDATE "Users" SET "age"="age"+ 2,"name"='John' WHERE id = 47 RETURNING *`,
  100. });
  101. });
  102. });