where.simple.test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. var Test = require('../../support/test-runner');
  2. describe('Query Generation ::', function() {
  3. describe('Simple WHERE statements', function() {
  4. it('should generate a query with a simple WHERE statement', function(done) {
  5. Test({
  6. query: {
  7. select: ['id'],
  8. where: {
  9. and: [
  10. {
  11. firstName: 'Test'
  12. },
  13. {
  14. lastName: 'User'
  15. }
  16. ]
  17. },
  18. from: 'users'
  19. },
  20. outcomes: [
  21. {
  22. dialect: 'postgresql',
  23. sql: 'select "id" from "users" where "firstName" = $1 and "lastName" = $2',
  24. bindings: ['Test', 'User']
  25. },
  26. {
  27. dialect: 'mysql',
  28. sql: 'select `id` from `users` where `firstName` = ? and `lastName` = ?',
  29. bindings: ['Test', 'User']
  30. },
  31. {
  32. dialect: 'sqlite3',
  33. sql: 'select "id" from "users" where "firstName" = ? and "lastName" = ?',
  34. bindings: ['Test', 'User']
  35. },
  36. {
  37. dialect: 'oracle',
  38. sql: 'select "id" from "users" where "firstName" = :1 and "lastName" = :2',
  39. bindings: ['Test', 'User']
  40. },
  41. {
  42. dialect: 'mariadb',
  43. sql: 'select `id` from `users` where `firstName` = ? and `lastName` = ?',
  44. bindings: ['Test', 'User']
  45. }
  46. ]
  47. }, done);
  48. });
  49. it('should generate a query when operators are used', function(done) {
  50. Test({
  51. query: {
  52. select: ['*'],
  53. where: {
  54. and: [
  55. {
  56. votes: {
  57. '>': 100
  58. }
  59. }
  60. ]
  61. },
  62. from: 'users'
  63. },
  64. outcomes: [
  65. {
  66. dialect: 'postgresql',
  67. sql: 'select * from "users" where "votes" > $1',
  68. bindings: ['100']
  69. },
  70. {
  71. dialect: 'mysql',
  72. sql: 'select * from `users` where `votes` > ?',
  73. bindings: ['100']
  74. },
  75. {
  76. dialect: 'sqlite3',
  77. sql: 'select * from "users" where "votes" > ?',
  78. bindings: ['100']
  79. },
  80. {
  81. dialect: 'oracle',
  82. sql: 'select * from "users" where "votes" > :1',
  83. bindings: ['100']
  84. },
  85. {
  86. dialect: 'mariadb',
  87. sql: 'select * from `users` where `votes` > ?',
  88. bindings: ['100']
  89. }
  90. ]
  91. }, done);
  92. });
  93. it('should generate a query when multiple operators are used', function(done) {
  94. Test({
  95. query: {
  96. select: ['*'],
  97. where: {
  98. and: [
  99. {
  100. votes: {
  101. '>': 100
  102. }
  103. },
  104. {
  105. votes: {
  106. '<': 200
  107. }
  108. }
  109. ]
  110. },
  111. from: 'users'
  112. },
  113. outcomes: [
  114. {
  115. dialect: 'postgresql',
  116. sql: 'select * from "users" where "votes" > $1 and "votes" < $2',
  117. bindings: ['100', '200']
  118. },
  119. {
  120. dialect: 'mysql',
  121. sql: 'select * from `users` where `votes` > ? and `votes` < ?',
  122. bindings: ['100', '200']
  123. },
  124. {
  125. dialect: 'sqlite3',
  126. sql: 'select * from "users" where "votes" > ? and "votes" < ?',
  127. bindings: ['100', '200']
  128. },
  129. {
  130. dialect: 'oracle',
  131. sql: 'select * from "users" where "votes" > :1 and "votes" < :2',
  132. bindings: ['100', '200']
  133. },
  134. {
  135. dialect: 'mariadb',
  136. sql: 'select * from `users` where `votes` > ? and `votes` < ?',
  137. bindings: ['100', '200']
  138. }
  139. ]
  140. }, done);
  141. });
  142. it('should generate a query when multiple columns and operators are used', function(done) {
  143. Test({
  144. query: {
  145. select: ['*'],
  146. where: {
  147. and: [
  148. {
  149. votes: {
  150. '>': 100
  151. }
  152. },
  153. {
  154. age: {
  155. '<': 50
  156. }
  157. }
  158. ]
  159. },
  160. from: 'users'
  161. },
  162. outcomes: [
  163. {
  164. dialect: 'postgresql',
  165. sql: 'select * from "users" where "votes" > $1 and "age" < $2',
  166. bindings: ['100', '50']
  167. },
  168. {
  169. dialect: 'mysql',
  170. sql: 'select * from `users` where `votes` > ? and `age` < ?',
  171. bindings: ['100', '50']
  172. },
  173. {
  174. dialect: 'sqlite3',
  175. sql: 'select * from "users" where "votes" > ? and "age" < ?',
  176. bindings: ['100', '50']
  177. },
  178. {
  179. dialect: 'oracle',
  180. sql: 'select * from "users" where "votes" > :1 and "age" < :2',
  181. bindings: ['100', '50']
  182. },
  183. {
  184. dialect: 'mariadb',
  185. sql: 'select * from `users` where `votes` > ? and `age` < ?',
  186. bindings: ['100', '50']
  187. }
  188. ]
  189. }, done);
  190. });
  191. });
  192. });