123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- var Tokenizer = require('../../../index').query.tokenizer;
- var assert = require('assert');
- describe('Tokenizer ::', function() {
- describe('Simple WHERE statements', function() {
- it('should generate a valid token array', function() {
- var result = Tokenizer({
- select: ['id'],
- where: {
- and: [
- {
- firstName: 'Test'
- },
- {
- lastName: 'User'
- }
- ]
- },
- from: 'users'
- });
- assert.deepEqual(result, [
- { type: 'IDENTIFIER', value: 'SELECT' },
- { type: 'VALUE', value: 'id' },
- { type: 'ENDIDENTIFIER', value: 'SELECT' },
- { type: 'IDENTIFIER', value: 'WHERE' },
- { type: 'CONDITION', value: 'AND' },
- { type: 'GROUP', value: 0 },
- { type: 'KEY', value: 'firstName' },
- { type: 'VALUE', value: 'Test' },
- { type: 'ENDGROUP', value: 0 },
- { type: 'GROUP', value: 1 },
- { type: 'KEY', value: 'lastName' },
- { type: 'VALUE', value: 'User' },
- { type: 'ENDGROUP', value: 1 },
- { type: 'ENDCONDITION', value: 'AND' },
- { type: 'ENDIDENTIFIER', value: 'WHERE' },
- { type: 'IDENTIFIER', value: 'FROM' },
- { type: 'VALUE', value: 'users' },
- { type: 'ENDIDENTIFIER', value: 'FROM' }
- ]);
- });
- it('should generate a valid token array when used with operators', function() {
- var result = Tokenizer({
- select: ['*'],
- where: {
- and: [
- {
- votes: {
- '>': 100
- }
- }
- ]
- },
- from: 'users'
- });
- assert.deepEqual(result, [
- { type: 'IDENTIFIER', value: 'SELECT' },
- { type: 'VALUE', value: '*' },
- { type: 'ENDIDENTIFIER', value: 'SELECT' },
- { type: 'IDENTIFIER', value: 'WHERE' },
- { type: 'CONDITION', value: 'AND' },
- { type: 'GROUP', value: 0 },
- { type: 'KEY', value: 'votes' },
- { type: 'OPERATOR', value: '>' },
- { type: 'VALUE', value: 100 },
- { type: 'ENDOPERATOR', value: '>' },
- { type: 'ENDGROUP', value: 0 },
- { type: 'ENDCONDITION', value: 'AND' },
- { type: 'ENDIDENTIFIER', value: 'WHERE' },
- { type: 'IDENTIFIER', value: 'FROM' },
- { type: 'VALUE', value: 'users' },
- { type: 'ENDIDENTIFIER', value: 'FROM' }
- ]);
- });
- it('should generate a valid token array when used with multiple operators', function() {
- var result = Tokenizer({
- select: ['*'],
- where: {
- and: [
- {
- votes: {
- '>': 100
- }
- },
- {
- votes: {
- '<': 200
- }
- }
- ]
- },
- from: 'users'
- });
- assert.deepEqual(result, [
- { type: 'IDENTIFIER', value: 'SELECT' },
- { type: 'VALUE', value: '*' },
- { type: 'ENDIDENTIFIER', value: 'SELECT' },
- { type: 'IDENTIFIER', value: 'WHERE' },
- { type: 'CONDITION', value: 'AND' },
- { type: 'GROUP', value: 0 },
- { type: 'KEY', value: 'votes' },
- { type: 'OPERATOR', value: '>' },
- { type: 'VALUE', value: 100 },
- { type: 'ENDOPERATOR', value: '>' },
- { type: 'ENDGROUP', value: 0 },
- { type: 'GROUP', value: 1 },
- { type: 'KEY', value: 'votes' },
- { type: 'OPERATOR', value: '<' },
- { type: 'VALUE', value: 200 },
- { type: 'ENDOPERATOR', value: '<' },
- { type: 'ENDGROUP', value: 1 },
- { type: 'ENDCONDITION', value: 'AND' },
- { type: 'ENDIDENTIFIER', value: 'WHERE' },
- { type: 'IDENTIFIER', value: 'FROM' },
- { type: 'VALUE', value: 'users' },
- { type: 'ENDIDENTIFIER', value: 'FROM' }
- ]);
- });
- it('should generate a valid token array when used with multiple columns and operators', function() {
- var result = Tokenizer({
- select: ['*'],
- where: {
- and: [
- {
- votes: {
- '>': 100
- }
- },
- {
- age: {
- '<': 50
- }
- }
- ]
- },
- from: 'users'
- });
- assert.deepEqual(result, [
- { type: 'IDENTIFIER', value: 'SELECT' },
- { type: 'VALUE', value: '*' },
- { type: 'ENDIDENTIFIER', value: 'SELECT' },
- { type: 'IDENTIFIER', value: 'WHERE' },
- { type: 'CONDITION', value: 'AND' },
- { type: 'GROUP', value: 0 },
- { type: 'KEY', value: 'votes' },
- { type: 'OPERATOR', value: '>' },
- { type: 'VALUE', value: 100 },
- { type: 'ENDOPERATOR', value: '>' },
- { type: 'ENDGROUP', value: 0 },
- { type: 'GROUP', value: 1 },
- { type: 'KEY', value: 'age' },
- { type: 'OPERATOR', value: '<' },
- { type: 'VALUE', value: 50 },
- { type: 'ENDOPERATOR', value: '<' },
- { type: 'ENDGROUP', value: 1 },
- { type: 'ENDCONDITION', value: 'AND' },
- { type: 'ENDIDENTIFIER', value: 'WHERE' },
- { type: 'IDENTIFIER', value: 'FROM' },
- { type: 'VALUE', value: 'users' },
- { type: 'ENDIDENTIFIER', value: 'FROM' }
- ]);
- });
- });
- });
|