tokenizer.join.test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var Tokenizer = require('../../../index').query.tokenizer;
  2. var assert = require('assert');
  3. describe('Tokenizer ::', function() {
  4. describe('JOINS ::', function() {
  5. it('should generate a valid token array when a JOIN operation is used', function() {
  6. var result = Tokenizer({
  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. assert.deepEqual(result, [
  20. { type: 'IDENTIFIER', value: 'SELECT' },
  21. { type: 'VALUE', value: 'users.id' },
  22. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  23. { type: 'IDENTIFIER', value: 'SELECT' },
  24. { type: 'VALUE', value: 'contacts.phone' },
  25. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  26. { type: 'IDENTIFIER', value: 'FROM' },
  27. { type: 'VALUE', value: 'users' },
  28. { type: 'ENDIDENTIFIER', value: 'FROM' },
  29. { type: 'IDENTIFIER', value: 'JOIN' },
  30. { type: 'KEY', value: 'TABLE' },
  31. { type: 'VALUE', value: 'contacts' },
  32. { type: 'KEY', value: 'TABLE_KEY' },
  33. { type: 'VALUE', value: 'users' },
  34. { type: 'KEY', value: 'COLUMN_KEY' },
  35. { type: 'VALUE', value: 'id' },
  36. { type: 'KEY', value: 'TABLE_KEY' },
  37. { type: 'VALUE', value: 'contacts' },
  38. { type: 'KEY', value: 'COLUMN_KEY' },
  39. { type: 'VALUE', value: 'user_id' },
  40. { type: 'ENDIDENTIFIER', value: 'JOIN' }
  41. ]);
  42. });
  43. it('should generate a valid token array when multiple JOIN operations is used', function() {
  44. var result = Tokenizer({
  45. select: ['users.id', 'contacts.phone'],
  46. from: 'users',
  47. join: [
  48. {
  49. from: 'contacts',
  50. on: {
  51. users: 'id',
  52. contacts: 'user_id'
  53. }
  54. },
  55. {
  56. from: 'carriers',
  57. on: {
  58. users: 'id',
  59. carriers: 'user_id'
  60. }
  61. }
  62. ]
  63. });
  64. assert.deepEqual(result, [
  65. { type: 'IDENTIFIER', value: 'SELECT' },
  66. { type: 'VALUE', value: 'users.id' },
  67. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  68. { type: 'IDENTIFIER', value: 'SELECT' },
  69. { type: 'VALUE', value: 'contacts.phone' },
  70. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  71. { type: 'IDENTIFIER', value: 'FROM' },
  72. { type: 'VALUE', value: 'users' },
  73. { type: 'ENDIDENTIFIER', value: 'FROM' },
  74. { type: 'IDENTIFIER', value: 'JOIN' },
  75. { type: 'KEY', value: 'TABLE' },
  76. { type: 'VALUE', value: 'contacts' },
  77. { type: 'KEY', value: 'TABLE_KEY' },
  78. { type: 'VALUE', value: 'users' },
  79. { type: 'KEY', value: 'COLUMN_KEY' },
  80. { type: 'VALUE', value: 'id' },
  81. { type: 'KEY', value: 'TABLE_KEY' },
  82. { type: 'VALUE', value: 'contacts' },
  83. { type: 'KEY', value: 'COLUMN_KEY' },
  84. { type: 'VALUE', value: 'user_id' },
  85. { type: 'ENDIDENTIFIER', value: 'JOIN' },
  86. { type: 'IDENTIFIER', value: 'JOIN' },
  87. { type: 'KEY', value: 'TABLE' },
  88. { type: 'VALUE', value: 'carriers' },
  89. { type: 'KEY', value: 'TABLE_KEY' },
  90. { type: 'VALUE', value: 'users' },
  91. { type: 'KEY', value: 'COLUMN_KEY' },
  92. { type: 'VALUE', value: 'id' },
  93. { type: 'KEY', value: 'TABLE_KEY' },
  94. { type: 'VALUE', value: 'carriers' },
  95. { type: 'KEY', value: 'COLUMN_KEY' },
  96. { type: 'VALUE', value: 'user_id' },
  97. { type: 'ENDIDENTIFIER', value: 'JOIN' }
  98. ]);
  99. });
  100. it('should generate a valid token array when an INNERJOIN operation is used', function() {
  101. var result = Tokenizer({
  102. select: ['users.id', 'contacts.phone'],
  103. from: 'users',
  104. innerJoin: [
  105. {
  106. from: 'contacts',
  107. on: {
  108. users: 'id',
  109. contacts: 'user_id'
  110. }
  111. }
  112. ]
  113. });
  114. assert.deepEqual(result, [
  115. { type: 'IDENTIFIER', value: 'SELECT' },
  116. { type: 'VALUE', value: 'users.id' },
  117. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  118. { type: 'IDENTIFIER', value: 'SELECT' },
  119. { type: 'VALUE', value: 'contacts.phone' },
  120. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  121. { type: 'IDENTIFIER', value: 'FROM' },
  122. { type: 'VALUE', value: 'users' },
  123. { type: 'ENDIDENTIFIER', value: 'FROM' },
  124. { type: 'IDENTIFIER', value: 'INNERJOIN' },
  125. { type: 'KEY', value: 'TABLE' },
  126. { type: 'VALUE', value: 'contacts' },
  127. { type: 'KEY', value: 'TABLE_KEY' },
  128. { type: 'VALUE', value: 'users' },
  129. { type: 'KEY', value: 'COLUMN_KEY' },
  130. { type: 'VALUE', value: 'id' },
  131. { type: 'KEY', value: 'TABLE_KEY' },
  132. { type: 'VALUE', value: 'contacts' },
  133. { type: 'KEY', value: 'COLUMN_KEY' },
  134. { type: 'VALUE', value: 'user_id' },
  135. { type: 'ENDIDENTIFIER', value: 'INNERJOIN' }
  136. ]);
  137. });
  138. it('should generate a valid token array when a grouped OR JOIN operation is used', function() {
  139. var result = Tokenizer({
  140. select: ['*'],
  141. from: 'users',
  142. join: [
  143. {
  144. from: 'accounts',
  145. on: {
  146. or: [
  147. {
  148. accounts: 'id',
  149. users: 'account_id'
  150. },
  151. {
  152. accounts: 'owner_id',
  153. users: 'id'
  154. }
  155. ]
  156. }
  157. }
  158. ]
  159. });
  160. assert.deepEqual(result, [
  161. { type: 'IDENTIFIER', value: 'SELECT' },
  162. { type: 'VALUE', value: '*' },
  163. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  164. { type: 'IDENTIFIER', value: 'FROM' },
  165. { type: 'VALUE', value: 'users' },
  166. { type: 'ENDIDENTIFIER', value: 'FROM' },
  167. { type: 'IDENTIFIER', value: 'JOIN' },
  168. { type: 'KEY', value: 'TABLE' },
  169. { type: 'VALUE', value: 'accounts' },
  170. { type: 'COMBINATOR', value: 'OR' },
  171. { type: 'KEY', value: 'TABLE_KEY' },
  172. { type: 'VALUE', value: 'accounts' },
  173. { type: 'KEY', value: 'COLUMN_KEY' },
  174. { type: 'VALUE', value: 'id' },
  175. { type: 'KEY', value: 'TABLE_KEY' },
  176. { type: 'VALUE', value: 'users' },
  177. { type: 'KEY', value: 'COLUMN_KEY' },
  178. { type: 'VALUE', value: 'account_id' },
  179. { type: 'COMBINATOR', value: 'OR' },
  180. { type: 'KEY', value: 'TABLE_KEY' },
  181. { type: 'VALUE', value: 'accounts' },
  182. { type: 'KEY', value: 'COLUMN_KEY' },
  183. { type: 'VALUE', value: 'owner_id' },
  184. { type: 'KEY', value: 'TABLE_KEY' },
  185. { type: 'VALUE', value: 'users' },
  186. { type: 'KEY', value: 'COLUMN_KEY' },
  187. { type: 'VALUE', value: 'id' },
  188. { type: 'ENDIDENTIFIER', value: 'JOIN' }
  189. ]);
  190. });
  191. it('should generate a valid token array when a grouped AND JOIN operation is used', function() {
  192. var result = Tokenizer({
  193. select: ['*'],
  194. from: 'users',
  195. join: [
  196. {
  197. from: 'accounts',
  198. on: [
  199. {
  200. accounts: 'id',
  201. users: 'account_id'
  202. },
  203. {
  204. accounts: 'owner_id',
  205. users: 'id'
  206. }
  207. ]
  208. }
  209. ]
  210. });
  211. assert.deepEqual(result, [
  212. { type: 'IDENTIFIER', value: 'SELECT' },
  213. { type: 'VALUE', value: '*' },
  214. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  215. { type: 'IDENTIFIER', value: 'FROM' },
  216. { type: 'VALUE', value: 'users' },
  217. { type: 'ENDIDENTIFIER', value: 'FROM' },
  218. { type: 'IDENTIFIER', value: 'JOIN' },
  219. { type: 'KEY', value: 'TABLE' },
  220. { type: 'VALUE', value: 'accounts' },
  221. { type: 'COMBINATOR', value: 'AND' },
  222. { type: 'KEY', value: 'TABLE_KEY' },
  223. { type: 'VALUE', value: 'accounts' },
  224. { type: 'KEY', value: 'COLUMN_KEY' },
  225. { type: 'VALUE', value: 'id' },
  226. { type: 'KEY', value: 'TABLE_KEY' },
  227. { type: 'VALUE', value: 'users' },
  228. { type: 'KEY', value: 'COLUMN_KEY' },
  229. { type: 'VALUE', value: 'account_id' },
  230. { type: 'COMBINATOR', value: 'AND' },
  231. { type: 'KEY', value: 'TABLE_KEY' },
  232. { type: 'VALUE', value: 'accounts' },
  233. { type: 'KEY', value: 'COLUMN_KEY' },
  234. { type: 'VALUE', value: 'owner_id' },
  235. { type: 'KEY', value: 'TABLE_KEY' },
  236. { type: 'VALUE', value: 'users' },
  237. { type: 'KEY', value: 'COLUMN_KEY' },
  238. { type: 'VALUE', value: 'id' },
  239. { type: 'ENDIDENTIFIER', value: 'JOIN' }
  240. ]);
  241. });
  242. });
  243. });