Added more tests
This commit is contained in:
@@ -1,95 +1,167 @@
|
|||||||
describe('@capire/bookshop', () => {
|
describe('@capire/bookshop', () => {
|
||||||
|
const { GET, POST, expect } = require('@capire/tests').launch('@capire/bookshop')
|
||||||
|
|
||||||
const { GET, POST, expect } = require('@capire/tests') .launch ('@capire/bookshop')
|
it('should serve $metadata documents in v4', async () => {
|
||||||
|
const { headers, status, data } = await GET`/browse/$metadata`
|
||||||
it ('should serve $metadata documents in v4', async () => {
|
expect(headers).to.contain({
|
||||||
const {headers,status,data} = await GET `/browse/$metadata`
|
'content-type': 'application/xml',
|
||||||
expect (headers) .to.contain ({
|
'odata-version': '4.0',
|
||||||
"content-type": 'application/xml',
|
|
||||||
"odata-version": "4.0",
|
|
||||||
})
|
})
|
||||||
expect (data) .to.contain (
|
expect(data).to.contain(
|
||||||
'<EntitySet Name="Books" EntityType="CatalogService.Books">',
|
'<EntitySet Name="Books" EntityType="CatalogService.Books">'
|
||||||
)
|
)
|
||||||
expect (data) .to.contain (
|
expect(data).to.contain(
|
||||||
'<Annotation Term="Common.Label" String="Currency"/>'
|
'<Annotation Term="Common.Label" String="Currency"/>'
|
||||||
)
|
)
|
||||||
expect (status) .to.equal (200)
|
expect(status).to.equal(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should serve localized $metadata documents', async () => {
|
||||||
it ('should serve localized $metadata documents', async () => {
|
const { data } = await GET`/browse/$metadata?sap-language=de`
|
||||||
const {data} = await GET `/browse/$metadata?sap-language=de`
|
expect(data).to.contain(
|
||||||
expect (data) .to.contain (
|
|
||||||
'<Annotation Term="Common.Label" String="Währung"/>'
|
'<Annotation Term="Common.Label" String="Währung"/>'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should serve localized Books with $expanded currency', async () => {
|
||||||
it ('should serve localized Books with $expanded currency', async () => {
|
const {
|
||||||
const {data} = await GET `/browse/Books?&$select=title,author&$expand=currency&sap-language=de`
|
data,
|
||||||
expect (data.value) .to.containSubset ([
|
} = await GET`/browse/Books?&$select=title,author&$expand=currency&sap-language=de`
|
||||||
|
expect(data.value).to.containSubset([
|
||||||
{
|
{
|
||||||
ID:201, title: 'Sturmhöhe', author: 'Emily Brontë', currency: {
|
ID: 201,
|
||||||
|
title: 'Sturmhöhe',
|
||||||
|
author: 'Emily Brontë',
|
||||||
|
currency: {
|
||||||
code: 'GBP',
|
code: 'GBP',
|
||||||
descr: 'Britische Pfund',
|
descr: 'Britische Pfund',
|
||||||
name: 'Pfund',
|
name: 'Pfund',
|
||||||
symbol: '£'
|
symbol: '£',
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID:207, title: 'Jane Eyre', author: 'Charlotte Brontë', currency: {
|
ID: 207,
|
||||||
|
title: 'Jane Eyre',
|
||||||
|
author: 'Charlotte Brontë',
|
||||||
|
currency: {
|
||||||
descr: 'Britische Pfund',
|
descr: 'Britische Pfund',
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID:251, title: 'The Raven', author: 'Edgar Allen Poe', currency: {
|
ID: 251,
|
||||||
|
title: 'The Raven',
|
||||||
|
author: 'Edgar Allen Poe',
|
||||||
|
currency: {
|
||||||
code: 'USD',
|
code: 'USD',
|
||||||
name: 'US-Dollar',
|
name: 'US-Dollar',
|
||||||
symbol: '$'
|
symbol: '$',
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID:252, title: 'Eleonora', author: 'Edgar Allen Poe'
|
ID: 252,
|
||||||
|
title: 'Eleonora',
|
||||||
|
author: 'Edgar Allen Poe',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID:271, title: 'Catweazle', author: 'Richard Carpenter', currency: {
|
ID: 271,
|
||||||
|
title: 'Catweazle',
|
||||||
|
author: 'Richard Carpenter',
|
||||||
|
currency: {
|
||||||
code: 'EUR',
|
code: 'EUR',
|
||||||
name: 'Euro',
|
name: 'Euro',
|
||||||
symbol: '€'
|
symbol: '€',
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should serve localized Authors w/ $expanded book and currency', async () => {
|
||||||
it ('should serve localized Authors w/ $expanded book and currency', async () => {
|
const { data } = await GET(
|
||||||
const {data} = await GET (`/admin/Authors/101?sap-language=de`
|
`/admin/Authors/101?sap-language=de` +
|
||||||
+ `&$expand=books($select=title;$expand=currency($select=code,name,symbol))`
|
`&$expand=books($select=title;$expand=currency($select=code,name,symbol))` +
|
||||||
+ `&$select=name`)
|
`&$select=name`
|
||||||
expect (data) .to.eql ({
|
)
|
||||||
'@odata.context': '$metadata#Authors(name,ID,books(title,ID,currency(code,name,symbol)))/$entity',
|
expect(data).to.eql({
|
||||||
ID:101, name: 'Emily Brontë', books: [
|
'@odata.context':
|
||||||
{ ID:201, title: 'Sturmhöhe', currency: {
|
'$metadata#Authors(name,ID,books(title,ID,currency(code,name,symbol)))/$entity',
|
||||||
code:'GBP', name: 'Pfund', symbol: '£'
|
ID: 101,
|
||||||
}}
|
name: 'Emily Brontë',
|
||||||
|
books: [
|
||||||
|
{
|
||||||
|
ID: 201,
|
||||||
|
title: 'Sturmhöhe',
|
||||||
|
currency: {
|
||||||
|
code: 'GBP',
|
||||||
|
name: 'Pfund',
|
||||||
|
symbol: '£',
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should check on current stocks with $value', async () => {
|
||||||
it ('should check on current stocks with $value', async () => {
|
const { data } = await GET`/admin/Books/201/stock/$value`
|
||||||
const { data } = await GET `/admin/Books/201/stock/$value`
|
expect(data).to.equal(12)
|
||||||
expect(data) .to.equal (12)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should reject out-of-stock orders', () => {
|
||||||
it ('should reject out-of-stock orders', ()=> {
|
return expect(
|
||||||
return expect (Promise.all ([
|
Promise.all([
|
||||||
POST ('/browse/submitOrder', { book: 201, amount: 5 }),
|
POST('/browse/submitOrder', { book: 201, amount: 5 }),
|
||||||
POST ('/browse/submitOrder', { book: 201, amount: 5 }),
|
POST('/browse/submitOrder', { book: 201, amount: 5 }),
|
||||||
POST ('/browse/submitOrder', { book: 201, amount: 5 }),
|
POST('/browse/submitOrder', { book: 201, amount: 5 }),
|
||||||
])) .to.be.rejectedWith (/409 - 5 exceeds stock for book #201/)
|
])
|
||||||
|
).to.be.rejectedWith(/409 - 5 exceeds stock for book #201/)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should serve $select requests', async () => {
|
||||||
|
const { data } = await GET`/browse/Books?$select=ID,title`
|
||||||
|
expect(data.value).to.eql([
|
||||||
|
{ ID: 201, title: 'Wuthering Heights' },
|
||||||
|
{ ID: 207, title: 'Jane Eyre' },
|
||||||
|
{ ID: 251, title: 'The Raven' },
|
||||||
|
{ ID: 252, title: 'Eleonora' },
|
||||||
|
{ ID: 271, title: 'Catweazle' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should serve $search requests', async () => {
|
||||||
|
const { data } = await GET`/admin/Authors?$search=Bro&$select=name`
|
||||||
|
expect(data.value).to.eql([
|
||||||
|
{ ID: 101, name: 'Emily Brontë' },
|
||||||
|
{ ID: 107, name: 'Charlotte Brontë' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should serve $expand requests', async () => {
|
||||||
|
const {
|
||||||
|
data,
|
||||||
|
} = await GET`/admin/Authors?$select=name&$expand=books($select=ID,title)`
|
||||||
|
expect(data.value).to.eql([
|
||||||
|
{
|
||||||
|
ID: 101,
|
||||||
|
name: 'Emily Brontë',
|
||||||
|
books: [{ ID: 201, title: 'Wuthering Heights' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 107,
|
||||||
|
name: 'Charlotte Brontë',
|
||||||
|
books: [{ ID: 207, title: 'Jane Eyre' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 150,
|
||||||
|
name: 'Edgar Allen Poe',
|
||||||
|
books: [
|
||||||
|
{ ID: 251, title: 'The Raven' },
|
||||||
|
{ ID: 252, title: 'Eleonora' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 170,
|
||||||
|
name: 'Richard Carpenter',
|
||||||
|
books: [{ ID: 271, title: 'Catweazle' }],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
21
test/hello-world.test.js
Normal file
21
test/hello-world.test.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
describe('Hello world!', () => {
|
||||||
|
const { GET, expect } = require('@capire/tests').launch('cds serve', __dirname+'/../hello/world.cds', '')
|
||||||
|
|
||||||
|
it('should say hello with class impl', async () => {
|
||||||
|
const {data} = await GET `/say/hello(to='world')`
|
||||||
|
expect(data.value).to.eql('Hello world!')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should say hello with another impl', async () => {
|
||||||
|
const cds = require ('@sap/cds')
|
||||||
|
cds.serve('say').from(cds.model)
|
||||||
|
.at('/say-again').in(cds.app)
|
||||||
|
.with(srv => {
|
||||||
|
srv.on('hello', (req) => `Hello again ${req.data.to}!`)
|
||||||
|
})
|
||||||
|
const {data} = await GET `/say-again/hello(to='world')`
|
||||||
|
expect(data.value).to.eql('Hello again world!')
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
@@ -6,7 +6,9 @@ module.exports = exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// harmonizing jest and mocha
|
// harmonizing jest and mocha
|
||||||
if (global.test) { // it's jest
|
const is_mocha = !global.test
|
||||||
|
const is_jest = !!global.test
|
||||||
|
if (is_jest) { // it's jest
|
||||||
global.before = global.beforeAll
|
global.before = global.beforeAll
|
||||||
global.after = global.afterAll
|
global.after = global.afterAll
|
||||||
} else { // it's mocha
|
} else { // it's mocha
|
||||||
@@ -25,16 +27,7 @@ function _chai(){
|
|||||||
|
|
||||||
|
|
||||||
/** Launching and testing a cds server */
|
/** Launching and testing a cds server */
|
||||||
exports.launch = (project, args=['--in-memory?']) => {
|
exports.launch = (project, ...args) => {
|
||||||
|
|
||||||
const cds = require('@sap/cds')
|
|
||||||
|
|
||||||
// Supporting .launch (<package name>)
|
|
||||||
if (!cds.utils.existsSync(project)) try {
|
|
||||||
project = require('path').dirname (require.resolve(project+'/package.json'))
|
|
||||||
} catch(e) {
|
|
||||||
throw cds.error (`Cannot resolve project folder for '${project}'`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setting up test server
|
// Setting up test server
|
||||||
const console = global.console, logs=[]
|
const console = global.console, logs=[]
|
||||||
@@ -61,6 +54,19 @@ exports.launch = (project, args=['--in-memory?']) => {
|
|||||||
// launch cds server...
|
// launch cds server...
|
||||||
before (done => {
|
before (done => {
|
||||||
|
|
||||||
|
const cds = require('@sap/cds')
|
||||||
|
|
||||||
|
let cmd = 'run'
|
||||||
|
if (project.startsWith('cds ')) [ cmd, project ] = [ project.slice(4), args.shift() ]
|
||||||
|
if (!args.length) args = ['--in-memory?']
|
||||||
|
|
||||||
|
// Supporting .launch (<package name>)
|
||||||
|
if (cmd === 'run' && !cds.utils.existsSync(project)) try {
|
||||||
|
project = require('path').dirname (require.resolve(project+'/package.json'))
|
||||||
|
} catch(e) {
|
||||||
|
throw cds.error (`Cannot resolve project folder for '${project}'`)
|
||||||
|
}
|
||||||
|
|
||||||
if (!process.env.CDS_TEST_VERBOSE) global.console = { __proto__: global.console, logs,
|
if (!process.env.CDS_TEST_VERBOSE) global.console = { __proto__: global.console, logs,
|
||||||
time: ()=>{}, timeEnd: (...args)=> logs.push(args),
|
time: ()=>{}, timeEnd: (...args)=> logs.push(args),
|
||||||
debug: (...args)=> logs.push(args),
|
debug: (...args)=> logs.push(args),
|
||||||
@@ -70,9 +76,14 @@ exports.launch = (project, args=['--in-memory?']) => {
|
|||||||
dump(){ for (let each of logs) console.log (...each) },
|
dump(){ for (let each of logs) console.log (...each) },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return done (new Error(11))
|
||||||
process.env.PORT = '0'
|
process.env.PORT = '0'
|
||||||
const p = cds.exec ('run', project, ...args) // TODO w/ @sap/cds@3.33.3: , '--port', '0')
|
const p = cds.exec (cmd, project, ...args) // TODO w/ @sap/cds@3.33.3: , '--port', '0')
|
||||||
if (p && 'catch' in p) p.catch (done)
|
if (p && 'catch' in p) p.catch (e => {
|
||||||
|
if (is_mocha) console.error(e)
|
||||||
|
done(e)
|
||||||
|
})
|
||||||
|
// return done(new Error('dfghjkl'))
|
||||||
|
|
||||||
cds.once('listening', ({ server, url }) => {
|
cds.once('listening', ({ server, url }) => {
|
||||||
Object.assign (test,{server,url})
|
Object.assign (test,{server,url})
|
||||||
@@ -83,7 +94,7 @@ exports.launch = (project, args=['--in-memory?']) => {
|
|||||||
// shutdown cds server...
|
// shutdown cds server...
|
||||||
after (done => {
|
after (done => {
|
||||||
if (global.console !== console) global.console = console
|
if (global.console !== console) global.console = console
|
||||||
test.server.close (done)
|
test.server ? test.server.close (done) : done()
|
||||||
})
|
})
|
||||||
|
|
||||||
function _error (e) {
|
function _error (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user