Improve tests to also work with node --test

This commit is contained in:
Daniel Hutzel
2024-05-14 14:37:37 +02:00
parent 0f6809a45b
commit aaa94e2d5f
4 changed files with 84 additions and 87 deletions

View File

@@ -1,13 +1,10 @@
const cds = require ('@sap/cds') const cds = require ('@sap/cds')
describe('Hello world!', () => { 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') const {GET} = cds.test.in(__dirname,'../srv').run('serve', 'world.cds')
it('should say hello with class impl', async () => { it('should say hello with class impl', async () => {
const {data} = await GET`/say/hello(to='world')` 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)
}) })
}) })

View File

@@ -1,7 +1,8 @@
const cds = require('@sap/cds/lib')
const { expect } = cds.test
describe('cds.ql → cqn', () => { describe('cds.ql → cqn', () => {
const cds = require('@sap/cds/lib')
const { expect } = cds.test
const Foo = { name: 'Foo' } const Foo = { name: 'Foo' }
const Books = { name: 'capire.bookshop.Books' } const Books = { name: 'capire.bookshop.Books' }

View File

@@ -77,49 +77,45 @@ describe('cap/samples - Hierarchical Data', ()=>{
) )
}) })
it ('supports nested reads', async()=>{ it ('supports nested reads', ()=> expect (
expect (await SELECT.one.from (Cats, c=>{
SELECT.one.from (Cats, c=>{ c.ID, c.name.as('parent'), c.children (c=>{
c.ID, c.name.as('parent'), c.children (c=>{ c.name.as('child')
c.name.as('child') })
}) }) .where ({name:'Cat'})
}) .where ({name:'Cat'}) ) .to.eventually.eql (
) .to.eql ( { ID:101, parent:'Cat', children:[
{ ID:101, parent:'Cat', children:[ { child:'Kitty' },
{ child:'Kitty' }, { child:'Catwoman' },
{ child:'Catwoman' }, ]}
]} ))
)
})
it ('supports deeply nested reads', async()=>{ it ('supports deeply nested reads', ()=> expect (
expect (await SELECT.one.from (Cats, c=>{ SELECT.one.from (Cats, c=>{
c.ID, c.name, c.children ( c.ID, c.name, c.children (
c => { c.name }, c => { c.name },
{levels:3} {levels:3}
) )
}) .where ({name:'Cat'}) }) .where ({name:'Cat'})
) .to.eql ( ) .to.eventually.eql (
{ ID:101, name:'Cat', children:[ { ID:101, name:'Cat', children:[
{ name:'Kitty', children:[ { name:'Kitty', children:[
{ name:'Kitty Cat', children:[ { name:'Kitty Cat', children:[
{ name:'Aristocat' }, ]}, // level 3 { name:'Aristocat' }, ]}, // level 3
{ name:'Kitty Bat', children:[] }, ]}, { name:'Kitty Bat', children:[] }, ]},
{ name:'Catwoman', children:[ { name:'Catwoman', children:[
{ name:'Catalina', children:[] } ]}, { name:'Catalina', children:[] } ]},
]} ]}
) ))
})
it ('supports cascaded deletes', async()=>{ it ('supports cascaded deletes', async()=>{
const affectedRows = await DELETE.from (Cats) .where ({ID:[102,106]}) const affectedRows = await DELETE.from (Cats) .where ({ID:[102,106]})
expect (affectedRows) .to.be.greaterThan (0) expect (affectedRows) .to.be.greaterThan (0)
const expected = [ await expect (SELECT`ID,name`.from(Cats) ).to.eventually.eql ([
{ ID:100, name:'Some Cats...' }, { ID:100, name:'Some Cats...' },
{ ID:101, name:'Cat' }, { ID:101, name:'Cat' },
{ ID:108, name:'Catweazle' } { ID:108, name:'Catweazle' }
] ])
expect ( await SELECT`ID,name`.from(Cats) ).to.eql (expected)
}) })
}) })

View File

@@ -28,63 +28,66 @@ describe('cap/samples - Bookshop APIs', () => {
]) ])
}) })
it('supports $search in multiple fields', async () => { describe('query options...', () => {
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' },
])
})
it('supports $select', async () => { it('supports $search in multiple fields', async () => {
const { data } = await GET(`/browse/Books`, { const { data } = await GET `/browse/Books ${{
params: { $select: `ID,title` }, 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 () => { it('supports $select', async () => {
const { data } = await GET(`/admin/Authors`, { const { data } = await GET(`/browse/Books`, {
params: { params: { $select: `ID,title` },
$select: `name`, })
$expand: `books($select=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 () => { it('supports $expand', async () => {
const { data } = await GET`/admin/Books/201/stock/$value` const { data } = await GET(`/admin/Authors`, {
expect(data).to.equal(12) 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 () => { it('supports $value requests', async () => {
const { data: p1 } = await GET`/browse/Books?$select=title&$top=3` const { data } = await GET`/admin/Books/201/stock/$value`
expect(p1.value).to.containSubset([ expect(data).to.equal(12)
{ ID: 201, title: 'Wuthering Heights' }, })
{ ID: 207, title: 'Jane Eyre' },
{ ID: 251, title: 'The Raven' }, it('supports $top/$skip paging', async () => {
]) const { data: p1 } = await GET`/browse/Books?$select=title&$top=3`
const { data: p2 } = await GET`/browse/Books?$select=title&$skip=3` expect(p1.value).to.containSubset([
expect(p2.value).to.containSubset([ { ID: 201, title: 'Wuthering Heights' },
{ ID: 252, title: 'Eleonora' }, { ID: 207, title: 'Jane Eyre' },
{ ID: 271, title: 'Catweazle' }, { 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 () => { it('serves user info', async () => {