sequelizer.subquery.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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('Subqueries', function() {
  6. describe('used as a predicate', function() {
  7. it('should generate a valid query for an IN subquery', function() {
  8. var tree = analyze({
  9. select: ['*'],
  10. where: {
  11. and: [
  12. {
  13. id: {
  14. in: {
  15. select: ['id'],
  16. from: 'users',
  17. where: {
  18. or: [
  19. { status: 'active' },
  20. { name: 'John' }
  21. ]
  22. }
  23. }
  24. }
  25. }
  26. ]
  27. },
  28. from: 'accounts'
  29. });
  30. var result = Sequelizer(tree);
  31. assert.equal(result.sql, 'select * from "accounts" where "id" in (select "id" from "users" where "status" = $1 or "name" = $2)');
  32. assert.deepEqual(result.bindings, ['active', 'John']);
  33. });
  34. it('should generate a valid query for a NOT IN subquery', function() {
  35. var tree = analyze({
  36. select: ['*'],
  37. from: 'accounts',
  38. where: {
  39. and: [
  40. {
  41. id: {
  42. nin: {
  43. select: ['id'],
  44. from: 'users',
  45. where: {
  46. or: [
  47. { status: 'active' },
  48. { name: 'John' }
  49. ]
  50. }
  51. }
  52. }
  53. }
  54. ]
  55. }
  56. });
  57. var result = Sequelizer(tree);
  58. assert.equal(result.sql, 'select * from "accounts" where "id" not in (select "id" from "users" where "status" = $1 or "name" = $2)');
  59. assert.deepEqual(result.bindings, ['active', 'John']);
  60. });
  61. });
  62. describe('used as scalar values', function() {
  63. it('should generate a valid query when used inside a SELECT', function() {
  64. var tree = analyze({
  65. select: ['name', {
  66. select: ['username'],
  67. from: 'users',
  68. where: {
  69. or: [
  70. { status: 'active' },
  71. { name: 'John' }
  72. ]
  73. },
  74. as: 'username'
  75. }, 'age'],
  76. from: 'accounts'
  77. });
  78. var result = Sequelizer(tree);
  79. assert.equal(result.sql, 'select "name", (select "username" from "users" where "status" = $1 or "name" = $2) as "username", "age" from "accounts"');
  80. assert.deepEqual(result.bindings, ['active', 'John']);
  81. });
  82. it('should generate a valid query when used as a value in a WHERE', function() {
  83. var tree = analyze({
  84. select: ['name', 'age'],
  85. from: 'accounts',
  86. where: {
  87. and: [
  88. {
  89. username: {
  90. select: ['username'],
  91. from: 'users',
  92. where: {
  93. color: 'accounts.color'
  94. }
  95. }
  96. }
  97. ]
  98. }
  99. });
  100. var result = Sequelizer(tree);
  101. assert.equal(result.sql, 'select "name", "age" from "accounts" where "username" = (select "username" from "users" where "color" = $1)');
  102. assert.deepEqual(result.bindings, ['accounts.color']);
  103. });
  104. });
  105. describe('used as table sub query', function() {
  106. it('should generate a valid query when used as a value in a FROM with an AS alias', function() {
  107. var tree = analyze({
  108. select: ['name', 'age'],
  109. from: {
  110. select: ['age'],
  111. from: 'users',
  112. where: {
  113. and: [
  114. {
  115. age: 21
  116. }
  117. ]
  118. },
  119. as: 'userage'
  120. }
  121. });
  122. var result = Sequelizer(tree);
  123. assert.equal(result.sql, 'select "name", "age" from (select "age" from "users" where "age" = $1) as "userage"');
  124. assert.deepEqual(result.bindings, [21]);
  125. });
  126. });
  127. });
  128. });