tokenizer.where.in.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var Tokenizer = require('../../../index').query.tokenizer;
  2. var assert = require('assert');
  3. describe('Tokenizer ::', function() {
  4. describe('WHERE IN statements', function() {
  5. it('should generate a valid token array', function() {
  6. var result = Tokenizer({
  7. select: ['name'],
  8. from: 'users',
  9. where: {
  10. and: [
  11. {
  12. id: {
  13. in: [1, 2, 3]
  14. }
  15. }
  16. ]
  17. }
  18. });
  19. assert.deepEqual(result, [
  20. { type: 'IDENTIFIER', value: 'SELECT' },
  21. { type: 'VALUE', value: 'name' },
  22. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  23. { type: 'IDENTIFIER', value: 'FROM' },
  24. { type: 'VALUE', value: 'users' },
  25. { type: 'ENDIDENTIFIER', value: 'FROM' },
  26. { type: 'IDENTIFIER', value: 'WHERE' },
  27. { type: 'CONDITION', value: 'AND' },
  28. { type: 'GROUP', value: 0 },
  29. { type: 'KEY', value: 'id' },
  30. { type: 'CONDITION', value: 'IN' },
  31. { type: 'VALUE', value: [1, 2, 3] },
  32. { type: 'ENDCONDITION', value: 'IN' },
  33. { type: 'ENDGROUP', value: 0 },
  34. { type: 'ENDCONDITION', value: 'AND' },
  35. { type: 'ENDIDENTIFIER', value: 'WHERE' }
  36. ]);
  37. });
  38. it('should generate a valid token array when in an OR statement', function() {
  39. var result = Tokenizer({
  40. select: ['name'],
  41. from: 'users',
  42. where: {
  43. or: [
  44. {
  45. id: {
  46. in: [1, 2, 3]
  47. }
  48. },
  49. {
  50. id: {
  51. in: [4, 5, 6]
  52. }
  53. }
  54. ]
  55. }
  56. });
  57. assert.deepEqual(result, [
  58. { type: 'IDENTIFIER', value: 'SELECT' },
  59. { type: 'VALUE', value: 'name' },
  60. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  61. { type: 'IDENTIFIER', value: 'FROM' },
  62. { type: 'VALUE', value: 'users' },
  63. { type: 'ENDIDENTIFIER', value: 'FROM' },
  64. { type: 'IDENTIFIER', value: 'WHERE' },
  65. { type: 'CONDITION', value: 'OR' },
  66. { type: 'GROUP', value: 0 },
  67. { type: 'KEY', value: 'id' },
  68. { type: 'CONDITION', value: 'IN' },
  69. { type: 'VALUE', value: [1, 2, 3] },
  70. { type: 'ENDCONDITION', value: 'IN' },
  71. { type: 'ENDGROUP', value: 0 },
  72. { type: 'GROUP', value: 1 },
  73. { type: 'KEY', value: 'id' },
  74. { type: 'CONDITION', value: 'IN' },
  75. { type: 'VALUE', value: [4, 5, 6] },
  76. { type: 'ENDCONDITION', value: 'IN' },
  77. { type: 'ENDGROUP', value: 1 },
  78. { type: 'ENDCONDITION', value: 'OR' },
  79. { type: 'ENDIDENTIFIER', value: 'WHERE' }
  80. ]);
  81. });
  82. });
  83. });