subquery.predicate.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var Test = require('../../support/test-runner');
  2. describe('Query Generation ::', function() {
  3. describe('Subqueries', function() {
  4. describe('used as a predicate', function() {
  5. it('should generate a valid query for an IN subquery', function(done) {
  6. Test({
  7. query: {
  8. select: ['*'],
  9. where: {
  10. and: [
  11. {
  12. id: {
  13. in: {
  14. select: ['id'],
  15. from: 'users',
  16. where: {
  17. or: [
  18. { status: 'active' },
  19. { name: 'John' }
  20. ]
  21. }
  22. }
  23. }
  24. }
  25. ]
  26. },
  27. from: 'accounts'
  28. },
  29. outcomes: [
  30. {
  31. dialect: 'postgresql',
  32. sql: 'select * from "accounts" where "id" in (select "id" from "users" where "status" = $1 or "name" = $2)',
  33. bindings: ['active', 'John']
  34. },
  35. {
  36. dialect: 'mysql',
  37. sql: 'select * from `accounts` where `id` in (select `id` from `users` where `status` = ? or `name` = ?)',
  38. bindings: ['active', 'John']
  39. },
  40. {
  41. dialect: 'sqlite3',
  42. sql: 'select * from "accounts" where "id" in (select "id" from "users" where "status" = ? or "name" = ?)',
  43. bindings: ['active', 'John']
  44. },
  45. {
  46. dialect: 'oracle',
  47. sql: 'select * from "accounts" where "id" in (select "id" from "users" where "status" = :1 or "name" = :2)',
  48. bindings: ['active', 'John']
  49. },
  50. {
  51. dialect: 'mariadb',
  52. sql: 'select * from `accounts` where `id` in (select `id` from `users` where `status` = ? or `name` = ?)',
  53. bindings: ['active', 'John']
  54. }
  55. ]
  56. }, done);
  57. });
  58. it('should generate a valid query for a NOT IN subquery', function(done) {
  59. Test({
  60. query: {
  61. select: ['*'],
  62. from: 'accounts',
  63. where: {
  64. and: [
  65. {
  66. id: {
  67. nin: {
  68. select: ['id'],
  69. from: 'users',
  70. where: {
  71. or: [
  72. { status: 'active' },
  73. { name: 'John' }
  74. ]
  75. }
  76. }
  77. }
  78. }
  79. ]
  80. }
  81. },
  82. outcomes: [
  83. {
  84. dialect: 'postgresql',
  85. sql: 'select * from "accounts" where "id" not in (select "id" from "users" where "status" = $1 or "name" = $2)',
  86. bindings: ['active', 'John']
  87. },
  88. {
  89. dialect: 'mysql',
  90. sql: 'select * from `accounts` where `id` not in (select `id` from `users` where `status` = ? or `name` = ?)',
  91. bindings: ['active', 'John']
  92. },
  93. {
  94. dialect: 'sqlite3',
  95. sql: 'select * from "accounts" where "id" not in (select "id" from "users" where "status" = ? or "name" = ?)',
  96. bindings: ['active', 'John']
  97. },
  98. {
  99. dialect: 'oracle',
  100. sql: 'select * from "accounts" where "id" not in (select "id" from "users" where "status" = :1 or "name" = :2)',
  101. bindings: ['active', 'John']
  102. },
  103. {
  104. dialect: 'mariadb',
  105. sql: 'select * from `accounts` where `id` not in (select `id` from `users` where `status` = ? or `name` = ?)',
  106. bindings: ['active', 'John']
  107. }
  108. ]
  109. }, done);
  110. });
  111. });
  112. });
  113. });