UNPKG

depresto

Version:

Why am I doing this to myself

379 lines (346 loc) 9.31 kB
const { depresto, fn, col } = require('../index'); test('select all query', () => { const AST = { from: 'tablename', } const SQL = `SELECT * FROM tablename;`; expect(depresto(AST)).toBe(SQL); }); test('select specific columns', () => { const AST = { from: 'tablename', select: ['a', 'b', 'c'], } const SQL = `SELECT a, b, c FROM tablename;` expect(depresto(AST)).toBe(SQL); }); test('simple where x = y clause', () => { const AST = { from: 'tablename', where: { x: 'y', } } const SQL = `SELECT * FROM tablename WHERE x = 'y';`; expect(depresto(AST)).toBe(SQL); }); test('simple where AND clause', () => { const AST = { from: 'tablename', where: { x: 'y', a: 'b', } } const SQL = `SELECT * FROM tablename WHERE x = 'y' AND a = 'b';`; expect(depresto(AST)).toBe(SQL); }); test('test OR statement', () => { const AST = { from: 'tablename', where: { $or: [ { a: 10 }, { b: 20 }, ], } } const SQL = `SELECT * FROM tablename WHERE (a = 10 OR b = 20);` expect(depresto(AST)).toBe(SQL); }); test('test combination of AND and OR', () => { const AST = { from: 'tablename', where: { a: 10, $or: [ { b: 'hello' }, { c: 'world!' }, ], } } const SQL = `SELECT * FROM tablename WHERE a = 10 AND (b = 'hello' OR c = 'world!');` expect(depresto(AST)).toBe(SQL); }); test('test $in condition', () => { const AST = { from: 'tablename', where: { $in: { a: [1, 2, 3] } } } const SQL = `SELECT * FROM tablename WHERE a IN (1, 2, 3);`; expect(depresto(AST)).toBe(SQL); }); test('test $in condition with strings', () => { const AST = { from: 'tablename', where: { $in: { a: ['1', '2', '3'] } } } const SQL = `SELECT * FROM tablename WHERE a IN ('1', '2', '3');`; expect(depresto(AST)).toBe(SQL); }); test('test $in with AND', () => { const AST = { from: 'tablename', where: { a: 1, $in: { b: [1, 2, 3] }, } } const SQL = `SELECT * FROM tablename WHERE a = 1 AND b IN (1, 2, 3);`; expect(depresto(AST)).toBe(SQL); }); test('test $in with AND and OR', () => { const AST = { from: 'tablename', where: { a: 10, $or: [ { c: 20 }, { d: 30 } ], $in: { e: [1, 2, 3] } } } const SQL = `SELECT * FROM tablename WHERE a = 10 AND (c = 20 OR d = 30) AND e IN (1, 2, 3);` expect(depresto(AST)).toBe(SQL); }); test(`test OR statements with nested AND's`, () => { const AST = { from: 'tablename', where: { $or: [ { a: 10, b: 20 }, { a: 20, b: 10 }, ] } } const SQL = `SELECT * FROM tablename WHERE ((a = 10 AND b = 20) OR (a = 20 AND b = 10));` expect(depresto(AST)).toBe(SQL); }); test('test nested OR statement', () => { const AST = { from: 'tablename', where: { $or: [ { a: 10 }, { $or: [{ b: 20 }, { c: 30 }] }, ] } } const SQL = `SELECT * FROM tablename WHERE (a = 10 OR (b = 20 OR c = 30));` expect(depresto(AST)).toBe(SQL); }); test('test not in query', () => { const AST = { from: 'tablename', where: { $notIn: { b: [10, 20, 30] }, } } const SQL = `SELECT * FROM tablename WHERE b NOT IN (10, 20, 30);` expect(depresto(AST)).toBe(SQL); }); test('test not in query with additional AND clause', () => { const AST = { from: 'tablename', where: { $notIn: { b: [10, 20, 30] }, c: 50, } } const SQL = `SELECT * FROM tablename WHERE b NOT IN (10, 20, 30) AND c = 50;` expect(depresto(AST)).toBe(SQL); }); test('test not in wrapped in OR', () => { const AST = { from: 'tablename', where: { $or: [ { $notIn: { a: [10, 20, 30] } }, { $notIn: { b: [40, 50, 60] } } ], } } const SQL = `SELECT * FROM tablename WHERE (a NOT IN (10, 20, 30) OR b NOT IN (40, 50, 60));` expect(depresto(AST)).toBe(SQL); }); test('test greater than operation', () => { const AST = { from: 'tablename', where: { $gt: { a: 10 }, } } const SQL = `SELECT * FROM tablename WHERE a > 10;` expect(depresto(AST)).toBe(SQL); }); test('test greater than or equal to operation', () => { const AST = { from: 'tablename', where: { $gte: { a: 10 }, } } const SQL = `SELECT * FROM tablename WHERE a >= 10;` expect(depresto(AST)).toBe(SQL); }); test('test less than operation', () => { const AST = { from: 'tablename', where: { $lt: { a: 10 }, } } const SQL = `SELECT * FROM tablename WHERE a < 10;` expect(depresto(AST)).toBe(SQL); }); test('test less than or equal to operation', () => { const AST = { from: 'tablename', where: { $lte: { a: 10 }, } } const SQL = `SELECT * FROM tablename WHERE a <= 10;` expect(depresto(AST)).toBe(SQL); }); test('test is operation', () => { const AST = { from: 'tablename', where: { $is: { a: null }, } } const SQL = `SELECT * FROM tablename WHERE a IS NULL;` expect(depresto(AST)).toBe(SQL); }); test('test is not operation', () => { const AST = { from: 'tablename', where: { $isNot: { a: null }, } } const SQL = `SELECT * FROM tablename WHERE a IS NOT NULL;` expect(depresto(AST)).toBe(SQL); }); test('test like operation', () => { const AST = { from: 'tablename', where: { $like: { a: '%llo wor%' }, } } const SQL = `SELECT * FROM tablename WHERE a LIKE '%llo wor%';` expect(depresto(AST)).toBe(SQL); }); test('test not like operation', () => { const AST = { from: 'tablename', where: { $notLike: { a: '%llo wor%' }, } } const SQL = `SELECT * FROM tablename WHERE a NOT LIKE '%llo wor%';` expect(depresto(AST)).toBe(SQL); }); test('test date parse operation', () => { const AST = { from: 'tablename', where: { [fn('date_parse', col('a'), 'dd-mm-yyyy')]: '2022-10-01', } } const SQL = `SELECT * FROM tablename WHERE date_parse(a, 'dd-mm-yyyy') = '2022-10-01';`; expect(depresto(AST)).toBe(SQL); }); test('test date parse is greater than', () => { const AST = { from: 'tablename', where: { $gt: { [fn('date_parse', col('a'), 'dd-mm-yyyy')]: '2022-10-01' }, } } const SQL = `SELECT * FROM tablename WHERE date_parse(a, 'dd-mm-yyyy') > '2022-10-01';`; expect(depresto(AST)).toBe(SQL); }) test('test strings are escaped properly', () => { const AST = { from: 'tablename', where: { a: "'test", } } const SQL = `SELECT * FROM tablename WHERE a = '\'\'test';` expect(depresto(AST)).toBe(SQL); }); test('test malicious query doesn\'t work', () => { const AST = { from: 'tablename', where: { a: "test'; DROP TABLE tablename --", } } const SQL = `SELECT * FROM tablename WHERE a = 'test''; DROP TABLE tablename --';` expect(depresto(AST)).toBe(SQL); }); test('test query limit', () => { const AST = { from: 'tablename', limit: 10, } const SQL = `SELECT * FROM tablename LIMIT 10;`; expect(depresto(AST)).toBe(SQL); }); test('test query grouping', () => { const AST = { from: 'tablename', groupBy: 'a', } const SQL = `SELECT * FROM tablename GROUP BY a;`; expect(depresto(AST)).toBe(SQL); }); test('test count query', () => { const AST = { from: 'tablename', select: fn('COUNT', col('*')), } const SQL = `SELECT COUNT(*) FROM tablename;`; expect(depresto(AST)).toBe(SQL); }); test('test $in subquery', () => { const AST = { from: 'tablename', select: ['a', 'b'], where: { $in: { a: { from: 'tablename2', where: { c: 'a' } } } } } const SQL = `SELECT a, b FROM tablename WHERE a IN (SELECT * FROM tablename2 WHERE c = 'a');`; expect(depresto(AST)).toBe(SQL); }); test('test $notIn subquery', () => { const AST = { from: 'tablename', select: ['a', 'b'], where: { $notIn: { a: { from: 'tablename2', where: { c: 'a' } } } } } const SQL = `SELECT a, b FROM tablename WHERE a NOT IN (SELECT * FROM tablename2 WHERE c = 'a');`; expect(depresto(AST)).toBe(SQL); });