From efa60550fb2341e02126cad44f49fc72d1c3ecf1 Mon Sep 17 00:00:00 2001 From: Daniel Hutzel Date: Tue, 31 Aug 2021 13:29:05 +0200 Subject: [PATCH] fix cds.ql (#266) --- test/cds.ql.test.js | 134 ++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 74 deletions(-) diff --git a/test/cds.ql.test.js b/test/cds.ql.test.js index 85b80c96..f9cda7f3 100644 --- a/test/cds.ql.test.js +++ b/test/cds.ql.test.js @@ -16,31 +16,6 @@ expect.one = (cqn) => !cqn.SELECT.distinct ? expect(cqn) : skip describe('cds.ql → cqn', () => { // - describe(`SELECT...`, () => { - - it('should consistently handle *', () => { - if (!cdr) return - expect({ - SELECT: { from: { ref: ['Foo'] }, columns: ['*'] }, - }) - .to.eql(CQL`SELECT * from Foo`) - .to.eql(CQL`SELECT from Foo{*}`) - .to.eql(SELECT('*').from(Foo)) - .to.eql(SELECT.from(Foo,['*'])) - }) - - - it('should consistently handle lists', () => { - const ID = 11, args = [{ref:['foo']}, 'bar', 3] - const cqn = CQL`SELECT from Foo where ID=11 and x in (foo,'bar',3)` - expect(SELECT.from`Foo`.where `ID=${ID} and x in ${args}`).to.eql(cqn) - expect(SELECT.from(Foo).where(`ID=`, ID, `and x in`, args)).to.eql(cqn) - expect(SELECT.from(Foo).where({ ID, x:args })).to.eql(cqn) - }) - - }) - - for (let each of ['SELECT', 'SELECT one', 'SELECT distinct']) { let SELECT; beforeEach(()=> SELECT = ( each === 'SELECT distinct' ? cds.ql.SELECT.distinct : @@ -285,46 +260,46 @@ describe('cds.ql → cqn', () => { ).to.eql({ SELECT: { from: { ref: ['Foo'] }, - where: cdr - ? [ - { ref: ['ID'] }, - '=', - { val: ID }, - 'and', - { ref: ['args'] }, - 'in', - { list: args.map(val => ({ val })) }, - 'and', - {xpr:[ - { ref: ['x'] }, - 'like', - { val: '%x%' }, - 'or', - { ref: ['y'] }, - '>=', - { val: 9 }, - ]}, + where: cdr ? [ + { ref: ['ID'] }, + '=', + { val: ID }, + 'and', + { ref: ['args'] }, + 'in', + { list: args.map(val => ({ val })) }, + 'and', + { + xpr: [ + { ref: ['x'] }, + 'like', + { val: '%x%' }, + 'or', + { ref: ['y'] }, + '>=', + { val: 9 }, ] - : [ - { ref: ['ID'] }, - '=', - { val: ID }, - 'and', - { ref: ['args'] }, - 'in', - { list: args.map(val => ({ val })) }, - 'and', - '(', - { ref: ['x'] }, - 'like', - { val: '%x%' }, - 'or', - { ref: ['y'] }, - '>=', - { val: 9 }, - ')', - ], - }, + }, + ] : [ + { ref: ['ID'] }, + '=', + { val: ID }, + 'and', + { ref: ['args'] }, + 'in', + { list: args.map(val => ({ val })) }, + 'and', + '(', + { ref: ['x'] }, + 'like', + { val: '%x%' }, + 'or', + { ref: ['y'] }, + '>=', + { val: 9 }, + ')', + ], + } }) // using CQL fragments -> uses cds.parse.expr @@ -377,15 +352,6 @@ describe('cds.ql → cqn', () => { ) }) - it('should consistently handle lists', () => { - if (!cdr) return - const ID = 11, args = [{ref:['foo']}, "bar", 3] - const cqn = CQL`SELECT from Foo where ID=11 and x in (foo,'bar',3)` - expect(SELECT.from(Foo).where`ID=${ID} and x in ${args}`).to.eql(cqn) - expect(SELECT.from(Foo).where(`ID=`, ID, `and x in`, args)).to.eql(cqn) - expect(SELECT.from(Foo).where({ ID, x:args })).to.eql(cqn) - }) - test('w/ sub selects', () => { // in where causes expect(SELECT.from(Foo).where({ x: SELECT('y').from('Bar') })).to.eql( @@ -418,12 +384,32 @@ describe('cds.ql → cqn', () => { ).to.eql(cqn) }) - it('w/ plain SQL', () => { + test('w/ plain SQL', () => { expect(SELECT.from(Books) + 'WHERE ...').to.eql( 'SELECT * FROM capire_bookshop_Books WHERE ...' ) }) + it('should consistently handle *', () => { + if (!cdr) return + expect({ + SELECT: { from: { ref: ['Foo'] }, columns: ['*'] }, + }) + .to.eql(CQL`SELECT * from Foo`) + .to.eql(CQL`SELECT from Foo{*}`) + .to.eql(SELECT('*').from(Foo)) + .to.eql(SELECT.from(Foo,['*'])) + }) + + it('should consistently handle lists', () => { + if (!cdr) return + const ID = 11, args = [{ref:['foo']}, "bar", 3] + const cqn = CQL`SELECT from Foo where ID=11 and x in (foo,'bar',3)` + expect(SELECT.from(Foo).where`ID=${ID} and x in ${args}`).to.eql(cqn) + expect(SELECT.from(Foo).where(`ID=`, ID, `and x in`, args)).to.eql(cqn) + expect(SELECT.from(Foo).where({ ID, x:args })).to.eql(cqn) + }) + // }) }