sequelizer.joins.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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('JOIN statements', function() {
  6. it('should generate a query when a JOIN statement is added', function() {
  7. var tree = analyze({
  8. select: ['users.id', 'contacts.phone'],
  9. from: 'users',
  10. join: [
  11. {
  12. from: 'contacts',
  13. on: {
  14. users: 'id',
  15. contacts: 'user_id'
  16. }
  17. }
  18. ]
  19. });
  20. var result = Sequelizer(tree);
  21. assert.equal(result.sql, 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"');
  22. });
  23. it('should generate a query when a multiple JOIN statements are added', function() {
  24. var tree = analyze({
  25. select: ['users.id', 'contacts.phone', 'carriers.name'],
  26. from: 'users',
  27. join: [
  28. {
  29. from: 'contacts',
  30. on: {
  31. users: 'id',
  32. contacts: 'user_id'
  33. }
  34. },
  35. {
  36. from: 'carriers',
  37. on: {
  38. users: 'id',
  39. carriers: 'user_id'
  40. }
  41. }
  42. ]
  43. });
  44. var result = Sequelizer(tree);
  45. assert.equal(result.sql, 'select "users"."id", "contacts"."phone", "carriers"."name" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id" inner join "carriers" on "users"."id" = "carriers"."user_id"');
  46. });
  47. it('should generate a query when an INNERJOIN statement is added', function() {
  48. var tree = analyze({
  49. select: ['users.id', 'contacts.phone'],
  50. from: 'users',
  51. innerJoin: [
  52. {
  53. from: 'contacts',
  54. on: {
  55. users: 'id',
  56. contacts: 'user_id'
  57. }
  58. }
  59. ]
  60. });
  61. var result = Sequelizer(tree);
  62. assert.equal(result.sql, 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"');
  63. });
  64. it('should generate a query when an OUTERJOIN statement is added', function() {
  65. var tree = analyze({
  66. select: ['users.id', 'contacts.phone'],
  67. from: 'users',
  68. outerJoin: [
  69. {
  70. from: 'contacts',
  71. on: {
  72. users: 'id',
  73. contacts: 'user_id'
  74. }
  75. }
  76. ]
  77. });
  78. var result = Sequelizer(tree);
  79. assert.equal(result.sql, 'select "users"."id", "contacts"."phone" from "users" outer join "contacts" on "users"."id" = "contacts"."user_id"');
  80. });
  81. it('should generate a query when grouped OR joins are added', function() {
  82. var tree = analyze({
  83. select: ['*'],
  84. from: 'users',
  85. join: [
  86. {
  87. from: 'accounts',
  88. on: {
  89. or: [
  90. {
  91. accounts: 'id',
  92. users: 'account_id'
  93. },
  94. {
  95. accounts: 'owner_id',
  96. users: 'id'
  97. }
  98. ]
  99. }
  100. }
  101. ]
  102. });
  103. var result = Sequelizer(tree);
  104. assert.equal(result.sql, 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"');
  105. });
  106. it('should generate a query when grouped AND joins are added', function() {
  107. var tree = analyze({
  108. select: ['*'],
  109. from: 'users',
  110. join: [
  111. {
  112. from: 'accounts',
  113. on: [
  114. {
  115. accounts: 'id',
  116. users: 'account_id'
  117. },
  118. {
  119. accounts: 'owner_id',
  120. users: 'id'
  121. }
  122. ]
  123. }
  124. ]
  125. });
  126. var result = Sequelizer(tree);
  127. assert.equal(result.sql, 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" and "accounts"."owner_id" = "users"."id"');
  128. });
  129. it('should generate a query when multiple grouped OR joins are added', function() {
  130. var tree = analyze({
  131. select: ['*'],
  132. from: 'users',
  133. join: [
  134. {
  135. from: 'accounts',
  136. on: {
  137. or: [
  138. {
  139. accounts: 'id',
  140. users: 'account_id'
  141. },
  142. {
  143. accounts: 'owner_id',
  144. users: 'id'
  145. }
  146. ]
  147. }
  148. },
  149. {
  150. from: 'carriers',
  151. on: {
  152. or: [
  153. {
  154. carriers: 'id',
  155. users: 'account_id'
  156. },
  157. {
  158. carriers: 'owner_id',
  159. users: 'id'
  160. }
  161. ]
  162. }
  163. }
  164. ]
  165. });
  166. var result = Sequelizer(tree);
  167. assert.equal(result.sql, 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id" inner join "carriers" on "carriers"."id" = "users"."account_id" or "carriers"."owner_id" = "users"."id"');
  168. });
  169. });
  170. });