sequelizer.or.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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('Grouping statements with OR', function() {
  6. it('should generate a query when an OR statement is used', function() {
  7. var tree = analyze({
  8. select: ['*'],
  9. where: {
  10. or: [
  11. {
  12. id: { '>': 10 }
  13. },
  14. {
  15. name: 'Tester'
  16. }
  17. ]
  18. },
  19. from: 'users'
  20. });
  21. var result = Sequelizer(tree);
  22. assert.equal(result.sql, 'select * from "users" where "id" > $1 or "name" = $2');
  23. assert.deepEqual(result.bindings, ['10', 'Tester']);
  24. });
  25. it('should generate a query when nested OR statements are used', function() {
  26. var tree = analyze({
  27. select: ['*'],
  28. where: {
  29. or: [
  30. {
  31. or: [
  32. { id: 1 },
  33. { id: { '>': 10 } }
  34. ]
  35. },
  36. {
  37. name: 'Tester'
  38. }
  39. ]
  40. },
  41. from: 'users'
  42. });
  43. var result = Sequelizer(tree);
  44. assert.equal(result.sql, 'select * from "users" where ("id" = $1 or "id" > $2) or "name" = $3');
  45. assert.deepEqual(result.bindings, ['1', '10', 'Tester']);
  46. });
  47. it('should generate a query when complex OR statements are used', function() {
  48. var tree = analyze({
  49. select: ['*'],
  50. where: {
  51. or: [
  52. {
  53. and: [
  54. {
  55. firstName: {
  56. like: '%user0%'
  57. }
  58. },
  59. {
  60. type: 'or test'
  61. }
  62. ]
  63. },
  64. {
  65. and: [
  66. {
  67. firstName: {
  68. like: '%user1'
  69. }
  70. },
  71. {
  72. age: {
  73. '>': 0
  74. }
  75. },
  76. {
  77. type: 'or test'
  78. }
  79. ]
  80. }
  81. ]
  82. },
  83. from: 'users'
  84. });
  85. var result = Sequelizer(tree);
  86. assert.equal(result.sql, 'select * from "users" where ("firstName" like $1 and "type" = $2) or ("firstName" like $3 and "age" > $4 and "type" = $5)');
  87. assert.deepEqual(result.bindings, ['%user0%', 'or test', '%user1', '0', 'or test']);
  88. });
  89. it('should generate a query when complex OR statements are used with IN', function() {
  90. var tree = analyze({
  91. select: ['*'],
  92. where: {
  93. and: [{
  94. inviteStatus: 'pending'
  95. }, {
  96. or: [{
  97. entityId: 1,
  98. inviteType: 'globalAdmin'
  99. }, {
  100. entityId: { in : [1] },
  101. inviteType: 'localAdmin'
  102. }]
  103. }]
  104. },
  105. from: 'users'
  106. });
  107. var result = Sequelizer(tree);
  108. assert.equal(result.sql, 'select * from "users" where "inviteStatus" = $1 and (("entityId" = $2 and "inviteType" = $3) or ("entityId" in ($4) and "inviteType" = $5))');
  109. assert.deepEqual(result.bindings, ['pending', '1', 'globalAdmin', '1', 'localAdmin']);
  110. });
  111. it('should generate a query when complex OR statements are used with NOT IN', function() {
  112. var tree = analyze({
  113. select: ['*'],
  114. where: {
  115. and: [{
  116. inviteStatus: 'pending'
  117. }, {
  118. or: [{
  119. entityId: 1,
  120. inviteType: 'globalAdmin'
  121. }, {
  122. entityId: { nin : [1] },
  123. inviteType: 'localAdmin'
  124. }]
  125. }]
  126. },
  127. from: 'users'
  128. });
  129. var result = Sequelizer(tree);
  130. assert.equal(result.sql, 'select * from "users" where "inviteStatus" = $1 and (("entityId" = $2 and "inviteType" = $3) or ("entityId" not in ($4) and "inviteType" = $5))');
  131. assert.deepEqual(result.bindings, ['pending', '1', 'globalAdmin', '1', 'localAdmin']);
  132. });
  133. it('should generate a query when complex multi-level nesting OR statements are used', function() {
  134. var tree = analyze({
  135. select: ['*'],
  136. where: {
  137. or: [
  138. {
  139. and: [
  140. {
  141. lastName: 'smith'
  142. },
  143. {
  144. or: [
  145. {
  146. age: {
  147. '<=': 7
  148. }
  149. },
  150. {
  151. type: 'even'
  152. }
  153. ]
  154. }
  155. ]
  156. },
  157. {
  158. and: [
  159. {
  160. lastName: 'jones'
  161. },
  162. {
  163. or: [
  164. {
  165. type: 'odd'
  166. },
  167. {
  168. firstName: {
  169. like: '%6%'
  170. }
  171. }
  172. ]
  173. }
  174. ]
  175. }
  176. ]
  177. },
  178. from: 'users'
  179. });
  180. var result = Sequelizer(tree);
  181. assert.equal(result.sql, 'select * from "users" where ("lastName" = $1 and ("age" <= $2 or "type" = $3)) or ("lastName" = $4 and ("type" = $5 or "firstName" like $6))');
  182. assert.deepEqual(result.bindings, ['smith', 7, 'even', 'jones', 'odd', '%6%']);
  183. });
  184. it('should generate a query when complex even more multi-level nesting OR statements are used', function() {
  185. var tree = analyze({
  186. select: ['*'],
  187. where: {
  188. or: [
  189. {
  190. and: [
  191. {
  192. lastName: 'smith'
  193. },
  194. {
  195. or: [
  196. { age: { '<=': 7 } },
  197. { type: 'even' }
  198. ]
  199. }
  200. ]
  201. },
  202. {
  203. and: [
  204. {
  205. lastName: 'jones'
  206. },
  207. {
  208. or: [
  209. {
  210. type: 'odd'
  211. },
  212. {
  213. and: [
  214. {
  215. firstName: { like: '%6%' }
  216. },
  217. {
  218. firstName: { like: '%nested' }
  219. }
  220. ]
  221. }
  222. ]
  223. }
  224. ]
  225. }
  226. ]
  227. },
  228. from: 'users'
  229. });
  230. var result = Sequelizer(tree);
  231. assert.equal(result.sql, 'select * from "users" where ("lastName" = $1 and ("age" <= $2 or "type" = $3)) or ("lastName" = $4 and ("type" = $5 or ("firstName" like $6 and "firstName" like $7)))');
  232. assert.deepEqual(result.bindings, ['smith', 7, 'even', 'jones', 'odd', '%6%', '%nested']);
  233. });
  234. it('should generate a query when complex even more super duper multi-level nesting OR statements are used', function() {
  235. var tree = analyze({
  236. select: ['*'],
  237. where: {
  238. or: [
  239. {
  240. and: [
  241. {
  242. lastName: 'smith'
  243. },
  244. {
  245. or: [
  246. { age: { '<=': 7 } },
  247. { type: 'even' }
  248. ]
  249. }
  250. ]
  251. },
  252. {
  253. and: [
  254. {
  255. lastName: 'jones'
  256. },
  257. {
  258. or: [
  259. {
  260. type: 'odd'
  261. },
  262. {
  263. and: [
  264. {
  265. firstName: {
  266. like: '%6%'
  267. }
  268. },
  269. {
  270. or: [
  271. { age: 1 },
  272. { age: { '<': 2 } }
  273. ]
  274. }
  275. ]
  276. }
  277. ]
  278. }
  279. ]
  280. }
  281. ]
  282. },
  283. from: 'users'
  284. });
  285. var result = Sequelizer(tree);
  286. assert.equal(result.sql, 'select * from "users" where ("lastName" = $1 and ("age" <= $2 or "type" = $3)) or ("lastName" = $4 and ("type" = $5 or ("firstName" like $6 and ("age" = $7 or "age" < $8))))');
  287. assert.deepEqual(result.bindings, ['smith', 7, 'even', 'jones', 'odd', '%6%', 1, 2]);
  288. });
  289. });
  290. });