fix cds.ql (#266)

This commit is contained in:
Daniel Hutzel
2021-08-31 13:29:05 +02:00
committed by GitHub
parent f599206bf4
commit efa60550fb

View File

@@ -16,31 +16,6 @@ expect.one = (cqn) => !cqn.SELECT.distinct ? expect(cqn) : skip
describe('cds.ql → cqn', () => { 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']) { for (let each of ['SELECT', 'SELECT one', 'SELECT distinct']) {
let SELECT; beforeEach(()=> SELECT = ( let SELECT; beforeEach(()=> SELECT = (
each === 'SELECT distinct' ? cds.ql.SELECT.distinct : each === 'SELECT distinct' ? cds.ql.SELECT.distinct :
@@ -285,46 +260,46 @@ describe('cds.ql → cqn', () => {
).to.eql({ ).to.eql({
SELECT: { SELECT: {
from: { ref: ['Foo'] }, from: { ref: ['Foo'] },
where: cdr where: cdr ? [
? [ { ref: ['ID'] },
{ ref: ['ID'] }, '=',
'=', { val: ID },
{ val: ID }, 'and',
'and', { ref: ['args'] },
{ ref: ['args'] }, 'in',
'in', { list: args.map(val => ({ val })) },
{ list: args.map(val => ({ val })) }, 'and',
'and', {
{xpr:[ xpr: [
{ ref: ['x'] }, { ref: ['x'] },
'like', 'like',
{ val: '%x%' }, { val: '%x%' },
'or', 'or',
{ ref: ['y'] }, { ref: ['y'] },
'>=', '>=',
{ val: 9 }, { val: 9 },
]},
] ]
: [ },
{ ref: ['ID'] }, ] : [
'=', { ref: ['ID'] },
{ val: ID }, '=',
'and', { val: ID },
{ ref: ['args'] }, 'and',
'in', { ref: ['args'] },
{ list: args.map(val => ({ val })) }, 'in',
'and', { list: args.map(val => ({ val })) },
'(', 'and',
{ ref: ['x'] }, '(',
'like', { ref: ['x'] },
{ val: '%x%' }, 'like',
'or', { val: '%x%' },
{ ref: ['y'] }, 'or',
'>=', { ref: ['y'] },
{ val: 9 }, '>=',
')', { val: 9 },
], ')',
}, ],
}
}) })
// using CQL fragments -> uses cds.parse.expr // 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', () => { test('w/ sub selects', () => {
// in where causes // in where causes
expect(SELECT.from(Foo).where({ x: SELECT('y').from('Bar') })).to.eql( expect(SELECT.from(Foo).where({ x: SELECT('y').from('Bar') })).to.eql(
@@ -418,12 +384,32 @@ describe('cds.ql → cqn', () => {
).to.eql(cqn) ).to.eql(cqn)
}) })
it('w/ plain SQL', () => { test('w/ plain SQL', () => {
expect(SELECT.from(Books) + 'WHERE ...').to.eql( expect(SELECT.from(Books) + 'WHERE ...').to.eql(
'SELECT * FROM capire_bookshop_Books WHERE ...' '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)
})
// //
}) })
} }