and.test.js 3.4 KB

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