join.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var Test = require('../../support/test-runner');
  2. describe('Query Generation ::', function() {
  3. describe('JOINS ::', function() {
  4. it('should generate a basic join query', function(done) {
  5. Test({
  6. query: {
  7. select: ['users.id', 'contacts.phone'],
  8. from: 'users',
  9. join: [
  10. {
  11. from: 'contacts',
  12. on: {
  13. users: 'id',
  14. contacts: 'user_id'
  15. }
  16. }
  17. ]
  18. },
  19. outcomes: [
  20. {
  21. dialect: 'postgresql',
  22. sql: 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"',
  23. bindings: []
  24. },
  25. {
  26. dialect: 'mysql',
  27. sql: 'select `users`.`id`, `contacts`.`phone` from `users` inner join `contacts` on `users`.`id` = `contacts`.`user_id`',
  28. bindings: []
  29. },
  30. {
  31. dialect: 'sqlite3',
  32. sql: 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"',
  33. bindings: []
  34. },
  35. {
  36. dialect: 'oracle',
  37. sql: 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"',
  38. bindings: []
  39. },
  40. {
  41. dialect: 'mariadb',
  42. sql: 'select `users`.`id`, `contacts`.`phone` from `users` inner join `contacts` on `users`.`id` = `contacts`.`user_id`',
  43. bindings: []
  44. }
  45. ]
  46. }, done);
  47. });
  48. it('should be able to contain multiple joins', function(done) {
  49. Test({
  50. query: {
  51. select: ['users.id', 'contacts.phone', 'carriers.name'],
  52. from: 'users',
  53. join: [
  54. {
  55. from: 'contacts',
  56. on: {
  57. users: 'id',
  58. contacts: 'user_id'
  59. }
  60. },
  61. {
  62. from: 'carriers',
  63. on: {
  64. users: 'id',
  65. carriers: 'user_id'
  66. }
  67. }
  68. ]
  69. },
  70. outcomes: [
  71. {
  72. dialect: 'postgresql',
  73. 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"',
  74. bindings: []
  75. },
  76. {
  77. dialect: 'mysql',
  78. 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`',
  79. bindings: []
  80. },
  81. {
  82. dialect: 'sqlite3',
  83. 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"',
  84. bindings: []
  85. },
  86. {
  87. dialect: 'oracle',
  88. 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"',
  89. bindings: []
  90. },
  91. {
  92. dialect: 'mariadb',
  93. 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`',
  94. bindings: []
  95. }
  96. ]
  97. }, done);
  98. });
  99. it('should be able to group joins', function(done) {
  100. Test({
  101. query: {
  102. select: ['*'],
  103. from: 'users',
  104. join: [
  105. {
  106. from: 'accounts',
  107. on: {
  108. or: [
  109. {
  110. accounts: 'id',
  111. users: 'account_id'
  112. },
  113. {
  114. accounts: 'owner_id',
  115. users: 'id'
  116. }
  117. ]
  118. }
  119. }
  120. ]
  121. },
  122. outcomes: [
  123. {
  124. dialect: 'postgresql',
  125. sql: 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"',
  126. bindings: []
  127. },
  128. {
  129. dialect: 'mysql',
  130. sql: 'select * from `users` inner join `accounts` on `accounts`.`id` = `users`.`account_id` or `accounts`.`owner_id` = `users`.`id`',
  131. bindings: []
  132. },
  133. {
  134. dialect: 'sqlite3',
  135. sql: 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"',
  136. bindings: []
  137. },
  138. {
  139. dialect: 'oracle',
  140. sql: 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"',
  141. bindings: []
  142. },
  143. {
  144. dialect: 'mariadb',
  145. sql: 'select * from `users` inner join `accounts` on `accounts`.`id` = `users`.`account_id` or `accounts`.`owner_id` = `users`.`id`',
  146. bindings: []
  147. }
  148. ]
  149. }, done);
  150. });
  151. });
  152. });