sequelizer.operators.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var Sequelizer = require('../../../index')({ dialect: 'postgres' }).sequelizer;
  2. var analyze = require('../../support/analyze');
  3. var assert = require('assert');
  4. describe('Sequelizer ::', function() {
  5. describe('Various Operators', function() {
  6. it('should generate a query for LIKE operators', function() {
  7. var tree = analyze({
  8. select: ['*'],
  9. from: 'users',
  10. where: {
  11. or: [
  12. {
  13. name: {
  14. like: '%Test%'
  15. }
  16. },
  17. {
  18. id: {
  19. nin: [1, 2, 3]
  20. }
  21. }
  22. ]
  23. }
  24. });
  25. var result = Sequelizer(tree);
  26. assert.equal(result.sql, 'select * from "users" where "name" like $1 or "id" not in ($2, $3, $4)');
  27. assert.deepEqual(result.bindings, ['%Test%', '1', '2', '3']);
  28. });
  29. it('should generate a query for != operators', function() {
  30. var tree = analyze({
  31. select: ['id'],
  32. from: 'users',
  33. where: {
  34. and: [
  35. {
  36. firstName: {
  37. '!=': 'Test'
  38. }
  39. },
  40. {
  41. lastName: {
  42. '!=': 'User'
  43. }
  44. }
  45. ]
  46. }
  47. });
  48. var result = Sequelizer(tree);
  49. assert.equal(result.sql, 'select "id" from "users" where "firstName" != $1 and "lastName" != $2');
  50. assert.deepEqual(result.bindings, ['Test', 'User']);
  51. });
  52. it('should generate a query with nested WHERE NOT EQUAL statements', function() {
  53. var tree = analyze({
  54. select: ['*'],
  55. from: 'users',
  56. where: {
  57. or: [
  58. {
  59. or: [
  60. {
  61. id: {
  62. '!=': 1
  63. }
  64. },
  65. {
  66. id: {
  67. '<': 10
  68. }
  69. }
  70. ]
  71. },
  72. {
  73. name: {
  74. '!=': 'Tester'
  75. }
  76. }
  77. ]
  78. }
  79. });
  80. var result = Sequelizer(tree);
  81. assert.equal(result.sql, 'select * from "users" where ("id" != $1 or "id" < $2) or "name" != $3');
  82. assert.deepEqual(result.bindings, ['1', '10', 'Tester']);
  83. });
  84. it('should generate a query for != null operators', function() {
  85. var tree = analyze({
  86. select: ['id'],
  87. from: 'users',
  88. where: {
  89. and: [
  90. {
  91. firstName: {
  92. '!=': null
  93. }
  94. },
  95. {
  96. lastName: {
  97. '!=': 'User'
  98. }
  99. }
  100. ]
  101. }
  102. });
  103. var result = Sequelizer(tree);
  104. assert.equal(result.sql, 'select "id" from "users" where "firstName" is not null and "lastName" != $1');
  105. assert.deepEqual(result.bindings, ['User']);
  106. });
  107. it('should generate a query when multiple operators are used', function() {
  108. var tree = analyze({
  109. select: ['*'],
  110. from: 'users',
  111. where: {
  112. or: [
  113. {
  114. name: 'John'
  115. },
  116. {
  117. votes: {
  118. '>': 100
  119. },
  120. title: {
  121. '!=': 'Admin'
  122. }
  123. }
  124. ]
  125. }
  126. });
  127. var result = Sequelizer(tree);
  128. assert.equal(result.sql, 'select * from "users" where "name" = $1 or ("votes" > $2 and "title" != $3)');
  129. assert.deepEqual(result.bindings, ['John', '100', 'Admin']);
  130. });
  131. });
  132. });