2016-01-26 19:29:12 -05:00
|
|
|
// Use the base driver as a mock for testing
|
2017-02-28 16:11:34 -05:00
|
|
|
const Helpers = require('../lib/Helpers');
|
2016-09-14 16:50:32 -04:00
|
|
|
const driver = require('../lib/Driver');
|
2016-01-26 19:29:12 -05:00
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
const P = require('../lib/QueryParser');
|
2016-01-26 19:29:12 -05:00
|
|
|
let parser = new P(driver);
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
const State = require('../lib/State');
|
2016-01-26 19:29:12 -05:00
|
|
|
|
|
|
|
// Simulate query builder state
|
|
|
|
let state = new State();
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
let mixedSet = function mixedSet (letName, valType, key, val) {
|
|
|
|
let obj = {};
|
|
|
|
|
2017-02-28 16:11:34 -05:00
|
|
|
if (Helpers.isScalar(key) && !Helpers.isUndefined(val)) {
|
2016-11-18 21:59:22 -05:00
|
|
|
// Convert key/val pair to a simple object
|
|
|
|
obj[key] = val;
|
2017-02-28 16:11:34 -05:00
|
|
|
} else if (Helpers.isScalar(key) && Helpers.isUndefined(val)) {
|
2016-11-18 21:59:22 -05:00
|
|
|
// If just a string for the key, and no value, create a simple object with duplicate key/val
|
|
|
|
obj[key] = key;
|
|
|
|
} else {
|
|
|
|
obj = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(obj).forEach(k => {
|
|
|
|
// If a single value for the return
|
|
|
|
if (['key', 'value'].indexOf(valType) !== -1) {
|
|
|
|
let pushVal = (valType === 'key') ? k : obj[k];
|
|
|
|
state[letName].push(pushVal);
|
|
|
|
} else {
|
|
|
|
state[letName][k] = obj[k];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return state[letName];
|
2016-01-26 19:29:12 -05:00
|
|
|
};
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
let whereMock = function (key, val) {
|
2016-01-26 19:29:12 -05:00
|
|
|
state.whereMap = [];
|
|
|
|
state.whereValues = [];
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
mixedSet('rawWhereValues', 'value', key, val);
|
|
|
|
mixedSet('whereMap', 'both', key, val);
|
2016-01-26 19:29:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// ! Start Tests
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2017-02-28 15:47:29 -05:00
|
|
|
describe('Query Parser Tests', () => {
|
|
|
|
describe('Has operator tests', () => {
|
|
|
|
it('Has operator', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let matches = parser.hasOperator('foo <> 2');
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(matches).toEqual(['<>']);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
it('Has no operator', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let matches = parser.hasOperator('foo');
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(matches).toBe(null);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
describe('Where parser tests', () => {
|
|
|
|
beforeAll(() => {
|
2016-01-26 19:29:12 -05:00
|
|
|
state = new State();
|
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
it('Has function full string', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
whereMock('time < SUM(FOO(BAR()))');
|
|
|
|
parser.parseWhere(driver, state);
|
|
|
|
expect(state.whereMap)
|
2018-02-02 11:50:29 -05:00
|
|
|
.toEqual(['"time" < SUM(FOO(BAR()))']);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
it('Has function key/val', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
whereMock('time <', 'SUM(FOO(BAR()))');
|
|
|
|
parser.parseWhere(driver, state);
|
|
|
|
expect(state.whereMap)
|
2018-02-02 11:50:29 -05:00
|
|
|
.toEqual(['"time" < SUM(FOO(BAR()))']);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
it('Has function key/val object', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
whereMock({
|
2016-09-14 16:50:32 -04:00
|
|
|
'time <': 'SUM(FOO(BAR(\'x\')))'
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
parser.parseWhere(driver, state);
|
|
|
|
expect(state.whereMap)
|
2018-02-02 11:50:29 -05:00
|
|
|
.toEqual(['"time" < SUM(FOO(BAR(\'x\')))']);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
it('Has literal value', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
whereMock({
|
2016-09-14 16:50:32 -04:00
|
|
|
foo: 3
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
parser.parseWhere(driver, state);
|
|
|
|
expect(state.whereMap)
|
2018-02-02 11:50:29 -05:00
|
|
|
.toEqual(['"foo" = ?']);
|
2016-01-26 19:29:12 -05:00
|
|
|
expect(state.whereValues)
|
2018-02-02 11:50:29 -05:00
|
|
|
.toEqual(['3']);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
it('Has multiple literal values', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
whereMock({
|
|
|
|
foo: 3,
|
2016-09-14 16:50:32 -04:00
|
|
|
bar: 5
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
parser.parseWhere(driver, state);
|
|
|
|
expect(state.whereMap)
|
2018-02-02 11:50:29 -05:00
|
|
|
.toEqual(['"foo" = ?', '"bar" = ?']);
|
2016-01-26 19:29:12 -05:00
|
|
|
expect(state.whereValues)
|
2018-02-02 11:50:29 -05:00
|
|
|
.toEqual(['3', '5']);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
describe('Parse join tests', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let data = [
|
|
|
|
{
|
|
|
|
desc: 'Simple equals condition',
|
|
|
|
join: 'table1.field1=table2.field2',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: ['table1.field1', '=', 'table2.field2']
|
2016-01-26 19:29:12 -05:00
|
|
|
}, {
|
|
|
|
desc: 'Db.table.field condition',
|
|
|
|
join: 'db1.table1.field1!=db2.table2.field2',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: ['db1.table1.field1', '!=', 'db2.table2.field2']
|
2016-01-26 19:29:12 -05:00
|
|
|
}, {
|
|
|
|
desc: 'Underscore in identifier',
|
|
|
|
join: 'table_1.field1 = tab_le2.field_2',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: ['table_1.field1', '=', 'tab_le2.field_2']
|
2016-01-26 19:29:12 -05:00
|
|
|
}, {
|
|
|
|
desc: 'Function in condition',
|
|
|
|
join: 'table1.field1 > SUM(3+6)',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: ['table1.field1', '>', 'SUM(3+6)']
|
|
|
|
}
|
2016-01-26 19:29:12 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
data.forEach(datum => {
|
2017-02-28 15:47:29 -05:00
|
|
|
it(datum.desc, () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let matches = parser.parseJoin(datum.join);
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(matches.combined).toEqual(datum.expected);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-02-28 15:47:29 -05:00
|
|
|
describe('Compile join tests', () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let data = [
|
|
|
|
{
|
|
|
|
desc: 'Simple equals condition',
|
|
|
|
clause: 'table1.field1=table2.field2',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: '"table1"."field1" = "table2"."field2"'
|
2016-01-26 19:29:12 -05:00
|
|
|
}, {
|
|
|
|
desc: 'Db.table.field condition',
|
|
|
|
clause: 'db1.table1.field1!=db2.table2.field2',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: '"db1"."table1"."field1" != "db2"."table2"."field2"'
|
2016-01-26 19:29:12 -05:00
|
|
|
}, {
|
|
|
|
desc: 'Underscore in identifier',
|
|
|
|
clause: 'table_1.field1 = tab_le2.field_2',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: '"table_1"."field1" = "tab_le2"."field_2"'
|
2016-01-26 19:29:12 -05:00
|
|
|
}, {
|
|
|
|
desc: 'Function in condition',
|
|
|
|
clause: 'table1.field1 > SUM(3+6)',
|
2016-09-14 16:50:32 -04:00
|
|
|
expected: '"table1"."field1" > SUM(3+6)'
|
|
|
|
}
|
2016-01-26 19:29:12 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
data.forEach(datum => {
|
2017-02-28 15:47:29 -05:00
|
|
|
it(datum.desc, () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let join = parser.compileJoin(datum.clause);
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(join).toEqual(datum.expected);
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-09-14 16:50:32 -04:00
|
|
|
});
|