Skip to content

Commit

Permalink
Improve tests to also work with node --test
Browse files Browse the repository at this point in the history
  • Loading branch information
danjoa committed May 14, 2024
1 parent 0f6809a commit aaa94e2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 87 deletions.
5 changes: 1 addition & 4 deletions hello/test/hello-world-test.js
Original file line number Diff line number Diff line change
@@ -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)
})

})
5 changes: 3 additions & 2 deletions test/cds.ql.test.js
Original file line number Diff line number Diff line change
@@ -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' }

Expand Down
56 changes: 26 additions & 30 deletions test/hierarchical-data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
])
})

})
105 changes: 54 additions & 51 deletions test/odata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit aaa94e2

Please sign in to comment.