Skip to content

Commit ceff5f6

Browse files
committed
better test structure
1 parent 797b6e1 commit ceff5f6

File tree

3 files changed

+115
-72
lines changed

3 files changed

+115
-72
lines changed

db-service/test/cqn4sql/DELETE.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,63 @@ describe('DELETE', () => {
244244
}
245245
expect(query.DELETE).to.deep.equal(expected.DELETE)
246246
})
247+
248+
describe('with path expressions', () => {
249+
let forNodeModel
250+
beforeAll(() => {
251+
// subqueries reference flat author_ID, which is not part of client csn
252+
forNodeModel = cds.compile.for.nodejs(JSON.parse(JSON.stringify(cds.model)))
253+
})
254+
255+
it('inner joins for the path expression at the leaf of scoped queries', () => {
256+
let query = DELETE.from('bookshop.Authors:books[genre.name = null]')
257+
const transformed = cqn4sql(query, forNodeModel)
258+
259+
const subquery = cds.ql`
260+
SELECT books.ID from bookshop.Books as books
261+
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
262+
WHERE EXISTS (
263+
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
264+
) and genre.name = null`
265+
const expected = DELETE.from('bookshop.Books').alias('books2')
266+
expected.DELETE.where = [{ list: [{ ref: ['books2', 'ID'] }] }, 'in', subquery]
267+
268+
expect(transformed).to.deep.equal(expected)
269+
})
270+
271+
it('inner joins for the path expression at the leaf of scoped queries, two assocs', () => {
272+
let query = DELETE.from('bookshop.Authors:books[genre.parent.name = null]')
273+
const transformed = cqn4sql(query, forNodeModel)
274+
275+
const subquery = cds.ql`
276+
SELECT books.ID from bookshop.Books as books
277+
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
278+
inner join bookshop.Genres as parent on parent.ID = genre.parent_ID
279+
WHERE EXISTS (
280+
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
281+
) and parent.name = null`
282+
const expected = DELETE.from('bookshop.Books').alias('books2')
283+
expected.DELETE.where = [{ list: [{ ref: ['books2', 'ID'] }] }, 'in', subquery]
284+
285+
expect(transformed).to.deep.equal(expected)
286+
})
287+
it('inner joins for the path expression NOT at the leaf of scoped queries, two assocs', () => {
288+
let query = DELETE.from(`bookshop.Authors[books.title = 'bar']:books[genre.parent.name = null]`).alias('MyBook')
289+
290+
const transformed = cqn4sql(query, forNodeModel)
291+
const subquery = cds.ql`
292+
SELECT MyBook.ID from bookshop.Books as MyBook
293+
inner join bookshop.Genres as genre on genre.ID = MyBook.genre_ID
294+
inner join bookshop.Genres as parent on parent.ID = genre.parent_ID
295+
WHERE EXISTS (
296+
SELECT 1 from bookshop.Authors as Authors
297+
inner join bookshop.Books as books on books.author_ID = Authors.ID
298+
where Authors.ID = MyBook.author_ID and books.title = 'bar'
299+
) and parent.name = null`
300+
const expected = DELETE.from('bookshop.Books').alias('MyBook2')
301+
expected.DELETE.where = [{ list: [{ ref: ['MyBook2', 'ID'] }] }, 'in', subquery]
302+
303+
expect(transformed).to.deep.equal(expected)
304+
})
305+
})
247306
})

db-service/test/cqn4sql/UPDATE.test.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,14 @@ describe('UPDATE', () => {
170170
})
171171
})
172172
describe('UPDATE with path expression', () => {
173-
let model
173+
let forNodeModel
174174
beforeAll(async () => {
175-
model = cds.model = await cds.load(__dirname + '/model/update').then(cds.linked)
176-
model = cds.compile.for.nodejs(model)
175+
cds.model = await cds.load(__dirname + '/../bookshop/srv/cat-service').then(cds.linked)
176+
forNodeModel = cds.compile.for.nodejs(JSON.parse(JSON.stringify(cds.model)))
177177
})
178178

179-
it('with path expressions with draft enabled entity', () => {
179+
it('with path expressions with draft enabled entity', async () => {
180+
const draftModel = await cds.load(__dirname + '/model/update').then(cds.linked)
180181
const { UPDATE } = cds.ql
181182
let u = UPDATE.entity({ ref: ['bookshop.CatalogService.Books'] }).where(`author.name LIKE '%Bron%'`)
182183

@@ -197,7 +198,57 @@ describe('UPDATE with path expression', () => {
197198
as: 'Books2',
198199
ref: ['bookshop.CatalogService.Books'],
199200
}
200-
let res = cqn4sql(u, model)
201+
let res = cqn4sql(u, draftModel)
201202
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(JSON.parse(JSON.stringify(expected)))
202203
})
204+
205+
it('inner joins for the path expression at the leaf of scoped queries', () => {
206+
let query = UPDATE.entity('bookshop.Authors:books[genre.name = null]')
207+
208+
const transformed = cqn4sql(query, forNodeModel)
209+
const subquery = cds.ql`
210+
SELECT books.ID from bookshop.Books as books
211+
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
212+
WHERE EXISTS (
213+
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
214+
) and genre.name = null`
215+
const expected = UPDATE.entity('bookshop.Books').alias('books2')
216+
expected.UPDATE.where = [{ list: [{ ref: ['books2', 'ID'] }] }, 'in', subquery]
217+
218+
expect(transformed).to.deep.equal(expected)
219+
})
220+
it('inner joins for the path expression at the leaf of scoped queries, two assocs (UPDATE)', () => {
221+
let query = UPDATE.entity('bookshop.Authors:books[genre.parent.name = null]')
222+
223+
const transformed = cqn4sql(query, forNodeModel)
224+
const subquery = cds.ql`
225+
SELECT books.ID from bookshop.Books as books
226+
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
227+
inner join bookshop.Genres as parent on parent.ID = genre.parent_ID
228+
WHERE EXISTS (
229+
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
230+
) and parent.name = null`
231+
const expected = UPDATE.entity('bookshop.Books').alias('books2')
232+
expected.UPDATE.where = [{ list: [{ ref: ['books2', 'ID'] }] }, 'in', subquery]
233+
234+
expect(transformed).to.deep.equal(expected)
235+
})
236+
it('inner joins for the path expression NOT at the leaf of scoped queries, two assocs (UPDATE)', () => {
237+
let query = UPDATE.entity(`bookshop.Authors[books.title = 'bar']:books[genre.parent.name = null]`).alias('MyBook')
238+
239+
const transformed = cqn4sql(query, forNodeModel)
240+
const subquery = cds.ql`
241+
SELECT MyBook.ID from bookshop.Books as MyBook
242+
inner join bookshop.Genres as genre on genre.ID = MyBook.genre_ID
243+
inner join bookshop.Genres as parent on parent.ID = genre.parent_ID
244+
WHERE EXISTS (
245+
SELECT 1 from bookshop.Authors as Authors
246+
inner join bookshop.Books as books on books.author_ID = Authors.ID
247+
where Authors.ID = MyBook.author_ID and books.title = 'bar'
248+
) and parent.name = null`
249+
const expected = UPDATE.entity('bookshop.Books').alias('MyBook2')
250+
expected.UPDATE.where = [{ list: [{ ref: ['MyBook2', 'ID'] }] }, 'in', subquery]
251+
252+
expect(transformed).to.deep.equal(expected)
253+
})
203254
})

db-service/test/cqn4sql/where-exists.test.js

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,71 +1720,4 @@ describe('path expression within infix filter following exists predicate', () =>
17201720
)`,
17211721
)
17221722
})
1723-
1724-
it('renders inner joins for the path expression at the leaf of scoped queries (DELETE)', () => {
1725-
// subquery has flat author_ID, which is not part of client csn
1726-
const forNodeModel = cds.compile.for.nodejs(JSON.parse(JSON.stringify(cds.model)))
1727-
let query = DELETE.from('bookshop.Authors:books[genre.name = null]')
1728-
1729-
const transformed = cqn4sql(query, forNodeModel)
1730-
const subquery = cds.ql`
1731-
SELECT books.ID from bookshop.Books as books
1732-
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
1733-
WHERE EXISTS (
1734-
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
1735-
) and genre.name = null`
1736-
const expected = DELETE.from('bookshop.Books').alias('books2')
1737-
expected.DELETE.where = [ { list: [ {ref: ['books2', 'ID'] }] }, 'in', subquery ]
1738-
expect(transformed).to.deep.equal( expected )
1739-
})
1740-
it('renders inner joins for the path expression at the leaf of scoped queries, two assocs (DELETE)', () => {
1741-
// subquery has flat author_ID, which is not part of client csn
1742-
const forNodeModel = cds.compile.for.nodejs(JSON.parse(JSON.stringify(cds.model)))
1743-
let query = DELETE.from('bookshop.Authors:books[genre.parent.name = null]')
1744-
1745-
const transformed = cqn4sql(query, forNodeModel)
1746-
const subquery = cds.ql`
1747-
SELECT books.ID from bookshop.Books as books
1748-
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
1749-
inner join bookshop.Genres as parent on parent.ID = genre.parent_ID
1750-
WHERE EXISTS (
1751-
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
1752-
) and parent.name = null`
1753-
const expected = DELETE.from('bookshop.Books').alias('books2')
1754-
expected.DELETE.where = [ { list: [ {ref: ['books2', 'ID'] }] }, 'in', subquery ]
1755-
expect(transformed).to.deep.equal( expected )
1756-
})
1757-
it('renders inner joins for the path expression at the leaf of scoped queries (UPDATE)', () => {
1758-
// subquery has flat author_ID, which is not part of client csn
1759-
const forNodeModel = cds.compile.for.nodejs(JSON.parse(JSON.stringify(cds.model)))
1760-
let query = UPDATE.entity('bookshop.Authors:books[genre.name = null]')
1761-
1762-
const transformed = cqn4sql(query, forNodeModel)
1763-
const subquery = cds.ql`
1764-
SELECT books.ID from bookshop.Books as books
1765-
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
1766-
WHERE EXISTS (
1767-
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
1768-
) and genre.name = null`
1769-
const expected = UPDATE.entity('bookshop.Books').alias('books2')
1770-
expected.UPDATE.where = [ { list: [ {ref: ['books2', 'ID'] }] }, 'in', subquery ]
1771-
expect(transformed).to.deep.equal( expected )
1772-
})
1773-
it('renders inner joins for the path expression at the leaf of scoped queries, two assocs (DELETE)', () => {
1774-
// subquery has flat author_ID, which is not part of client csn
1775-
const forNodeModel = cds.compile.for.nodejs(JSON.parse(JSON.stringify(cds.model)))
1776-
let query = UPDATE.entity('bookshop.Authors:books[genre.parent.name = null]')
1777-
1778-
const transformed = cqn4sql(query, forNodeModel)
1779-
const subquery = cds.ql`
1780-
SELECT books.ID from bookshop.Books as books
1781-
inner join bookshop.Genres as genre on genre.ID = books.genre_ID
1782-
inner join bookshop.Genres as parent on parent.ID = genre.parent_ID
1783-
WHERE EXISTS (
1784-
SELECT 1 from bookshop.Authors as Authors where Authors.ID = books.author_ID
1785-
) and parent.name = null`
1786-
const expected = UPDATE.entity('bookshop.Books').alias('books2')
1787-
expected.UPDATE.where = [ { list: [ {ref: ['books2', 'ID'] }] }, 'in', subquery ]
1788-
expect(transformed).to.deep.equal( expected )
1789-
})
17901723
})

0 commit comments

Comments
 (0)