From aaa94e2d5f92bed5ddeda568436327636862c649 Mon Sep 17 00:00:00 2001 From: Daniel Hutzel Date: Tue, 14 May 2024 14:37:37 +0200 Subject: [PATCH] Improve tests to also work with node --test --- hello/test/hello-world-test.js | 5 +- test/cds.ql.test.js | 5 +- test/hierarchical-data.test.js | 56 ++++++++---------- test/odata.test.js | 105 +++++++++++++++++---------------- 4 files changed, 84 insertions(+), 87 deletions(-) diff --git a/hello/test/hello-world-test.js b/hello/test/hello-world-test.js index b2ea3ef1..76149534 100644 --- a/hello/test/hello-world-test.js +++ b/hello/test/hello-world-test.js @@ -1,13 +1,10 @@ const cds = require ('@sap/cds') describe('Hello world!', () => { - - beforeAll (()=> process.env.CDS_TYPESCRIPT = true) - afterAll (()=> delete process.env.CDS_TYPESCRIPT) const {GET} = cds.test.in(__dirname,'../srv').run('serve', 'world.cds') it('should say hello with class impl', async () => { const {data} = await GET`/say/hello(to='world')` - expect(data.value).toMatch(/Hello world.*typescript.*/i) + expect(data.value).to.match(/Hello world.*typescript.*/i) }) }) diff --git a/test/cds.ql.test.js b/test/cds.ql.test.js index c42347ac..09f05447 100644 --- a/test/cds.ql.test.js +++ b/test/cds.ql.test.js @@ -1,7 +1,8 @@ +const cds = require('@sap/cds/lib') +const { expect } = cds.test + describe('cds.ql → cqn', () => { - const cds = require('@sap/cds/lib') - const { expect } = cds.test const Foo = { name: 'Foo' } const Books = { name: 'capire.bookshop.Books' } diff --git a/test/hierarchical-data.test.js b/test/hierarchical-data.test.js index 81c0955a..73ae0acd 100644 --- a/test/hierarchical-data.test.js +++ b/test/hierarchical-data.test.js @@ -77,49 +77,45 @@ describe('cap/samples - Hierarchical Data', ()=>{ ) }) - it ('supports nested reads', async()=>{ - expect (await - SELECT.one.from (Cats, c=>{ - c.ID, c.name.as('parent'), c.children (c=>{ - c.name.as('child') - }) - }) .where ({name:'Cat'}) - ) .to.eql ( - { ID:101, parent:'Cat', children:[ - { child:'Kitty' }, - { child:'Catwoman' }, - ]} - ) - }) + it ('supports nested reads', ()=> expect ( + SELECT.one.from (Cats, c=>{ + c.ID, c.name.as('parent'), c.children (c=>{ + c.name.as('child') + }) + }) .where ({name:'Cat'}) + ) .to.eventually.eql ( + { ID:101, parent:'Cat', children:[ + { child:'Kitty' }, + { child:'Catwoman' }, + ]} + )) - it ('supports deeply nested reads', async()=>{ - expect (await SELECT.one.from (Cats, c=>{ + it ('supports deeply nested reads', ()=> expect ( + SELECT.one.from (Cats, c=>{ c.ID, c.name, c.children ( c => { c.name }, {levels:3} ) }) .where ({name:'Cat'}) - ) .to.eql ( - { ID:101, name:'Cat', children:[ - { name:'Kitty', children:[ - { name:'Kitty Cat', children:[ - { name:'Aristocat' }, ]}, // level 3 - { name:'Kitty Bat', children:[] }, ]}, - { name:'Catwoman', children:[ - { name:'Catalina', children:[] } ]}, - ]} - ) - }) + ) .to.eventually.eql ( + { ID:101, name:'Cat', children:[ + { name:'Kitty', children:[ + { name:'Kitty Cat', children:[ + { name:'Aristocat' }, ]}, // level 3 + { name:'Kitty Bat', children:[] }, ]}, + { name:'Catwoman', children:[ + { name:'Catalina', children:[] } ]}, + ]} + )) it ('supports cascaded deletes', async()=>{ const affectedRows = await DELETE.from (Cats) .where ({ID:[102,106]}) expect (affectedRows) .to.be.greaterThan (0) - const expected = [ + await expect (SELECT`ID,name`.from(Cats) ).to.eventually.eql ([ { ID:100, name:'Some Cats...' }, { ID:101, name:'Cat' }, { ID:108, name:'Catweazle' } - ] - expect ( await SELECT`ID,name`.from(Cats) ).to.eql (expected) + ]) }) }) diff --git a/test/odata.test.js b/test/odata.test.js index d67f7346..a35eab01 100644 --- a/test/odata.test.js +++ b/test/odata.test.js @@ -28,63 +28,66 @@ describe('cap/samples - Bookshop APIs', () => { ]) }) - it('supports $search in multiple fields', async () => { - const { data } = await GET `/browse/Books ${{ - params: { $search: 'Po', $select: `title,author` }, - }}` - expect(data.value).to.containSubset([ - { ID: 201, title: 'Wuthering Heights', author: 'Emily Brontë' }, - { ID: 207, title: 'Jane Eyre', author: 'Charlotte Brontë' }, - { ID: 251, title: 'The Raven', author: 'Edgar Allen Poe' }, - { ID: 252, title: 'Eleonora', author: 'Edgar Allen Poe' }, - ]) - }) + describe('query options...', () => { - it('supports $select', async () => { - const { data } = await GET(`/browse/Books`, { - params: { $select: `ID,title` }, + it('supports $search in multiple fields', async () => { + const { data } = await GET `/browse/Books ${{ + params: { $search: 'Po', $select: `title,author` }, + }}` + expect(data.value).to.containSubset([ + { ID: 201, title: 'Wuthering Heights', author: 'Emily Brontë' }, + { ID: 207, title: 'Jane Eyre', author: 'Charlotte Brontë' }, + { ID: 251, title: 'The Raven', author: 'Edgar Allen Poe' }, + { ID: 252, title: 'Eleonora', author: 'Edgar Allen Poe' }, + ]) }) - expect(data.value).to.containSubset([ - { ID: 201, title: 'Wuthering Heights' }, - { ID: 207, title: 'Jane Eyre' }, - { ID: 251, title: 'The Raven' }, - { ID: 252, title: 'Eleonora' }, - { ID: 271, title: 'Catweazle' }, - ]) - }) - it('supports $expand', async () => { - const { data } = await GET(`/admin/Authors`, { - params: { - $select: `name`, - $expand: `books($select=title)`, - }, + it('supports $select', async () => { + const { data } = await GET(`/browse/Books`, { + params: { $select: `ID,title` }, + }) + expect(data.value).to.containSubset([ + { ID: 201, title: 'Wuthering Heights' }, + { ID: 207, title: 'Jane Eyre' }, + { ID: 251, title: 'The Raven' }, + { ID: 252, title: 'Eleonora' }, + { ID: 271, title: 'Catweazle' }, + ]) }) - expect(data.value).to.containSubset([ - { name: 'Emily Brontë', books: [{ title: 'Wuthering Heights' }] }, - { name: 'Charlotte Brontë', books: [{ title: 'Jane Eyre' }] }, - { name: 'Edgar Allen Poe', books: [{ title: 'The Raven' }, { title: 'Eleonora' }] }, - { name: 'Richard Carpenter', books: [{ title: 'Catweazle' }] }, - ]) - }) - it('supports $value requests', async () => { - const { data } = await GET`/admin/Books/201/stock/$value` - expect(data).to.equal(12) - }) + it('supports $expand', async () => { + const { data } = await GET(`/admin/Authors`, { + params: { + $select: `name`, + $expand: `books($select=title)`, + }, + }) + expect(data.value).to.containSubset([ + { name: 'Emily Brontë', books: [{ title: 'Wuthering Heights' }] }, + { name: 'Charlotte Brontë', books: [{ title: 'Jane Eyre' }] }, + { name: 'Edgar Allen Poe', books: [{ title: 'The Raven' }, { title: 'Eleonora' }] }, + { name: 'Richard Carpenter', books: [{ title: 'Catweazle' }] }, + ]) + }) - it('supports $top/$skip paging', async () => { - const { data: p1 } = await GET`/browse/Books?$select=title&$top=3` - expect(p1.value).to.containSubset([ - { ID: 201, title: 'Wuthering Heights' }, - { ID: 207, title: 'Jane Eyre' }, - { ID: 251, title: 'The Raven' }, - ]) - const { data: p2 } = await GET`/browse/Books?$select=title&$skip=3` - expect(p2.value).to.containSubset([ - { ID: 252, title: 'Eleonora' }, - { ID: 271, title: 'Catweazle' }, - ]) + it('supports $value requests', async () => { + const { data } = await GET`/admin/Books/201/stock/$value` + expect(data).to.equal(12) + }) + + it('supports $top/$skip paging', async () => { + const { data: p1 } = await GET`/browse/Books?$select=title&$top=3` + expect(p1.value).to.containSubset([ + { ID: 201, title: 'Wuthering Heights' }, + { ID: 207, title: 'Jane Eyre' }, + { ID: 251, title: 'The Raven' }, + ]) + const { data: p2 } = await GET`/browse/Books?$select=title&$skip=3` + expect(p2.value).to.containSubset([ + { ID: 252, title: 'Eleonora' }, + { ID: 271, title: 'Catweazle' }, + ]) + }) }) it('serves user info', async () => {