From f15a4f807e6bee705445825e7769b8a241cb3377 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 5 Apr 2020 18:55:30 +0200 Subject: [PATCH] Added more tests --- test/bookshop.test.js | 180 +++++++++++++++++++++++++++------------ test/hello-world.test.js | 21 +++++ test/lib/helpers.js | 39 ++++++--- 3 files changed, 172 insertions(+), 68 deletions(-) create mode 100644 test/hello-world.test.js diff --git a/test/bookshop.test.js b/test/bookshop.test.js index 1075e47f..ca5b71e1 100644 --- a/test/bookshop.test.js +++ b/test/bookshop.test.js @@ -1,95 +1,167 @@ 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` - expect (headers) .to.contain ({ - "content-type": 'application/xml', - "odata-version": "4.0", + it('should serve $metadata documents in v4', async () => { + const { headers, status, data } = await GET`/browse/$metadata` + expect(headers).to.contain({ + 'content-type': 'application/xml', + 'odata-version': '4.0', }) - expect (data) .to.contain ( - '', + expect(data).to.contain( + '' ) - expect (data) .to.contain ( + expect(data).to.contain( '' ) - expect (status) .to.equal (200) + expect(status).to.equal(200) }) - - it ('should serve localized $metadata documents', async () => { - const {data} = await GET `/browse/$metadata?sap-language=de` - expect (data) .to.contain ( + it('should serve localized $metadata documents', async () => { + const { data } = await GET`/browse/$metadata?sap-language=de` + expect(data).to.contain( '' ) }) - - it ('should serve localized Books with $expanded currency', async () => { - const {data} = await GET `/browse/Books?&$select=title,author&$expand=currency&sap-language=de` - expect (data.value) .to.containSubset ([ + it('should serve localized Books with $expanded currency', async () => { + const { + data, + } = 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', descr: 'Britische 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', - } + }, }, { - ID:251, title: 'The Raven', author: 'Edgar Allen Poe', currency: { + ID: 251, + title: 'The Raven', + author: 'Edgar Allen Poe', + currency: { code: 'USD', 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', name: 'Euro', - symbol: '€' - } - } + symbol: '€', + }, + }, ]) }) - - it ('should serve localized Authors w/ $expanded book and currency', async () => { - const {data} = await GET (`/admin/Authors/101?sap-language=de` - + `&$expand=books($select=title;$expand=currency($select=code,name,symbol))` - + `&$select=name`) - expect (data) .to.eql ({ - '@odata.context': '$metadata#Authors(name,ID,books(title,ID,currency(code,name,symbol)))/$entity', - ID:101, name: 'Emily Brontë', books: [ - { ID:201, title: 'Sturmhöhe', currency: { - code:'GBP', name: 'Pfund', symbol: '£' - }} + it('should serve localized Authors w/ $expanded book and currency', async () => { + const { data } = await GET( + `/admin/Authors/101?sap-language=de` + + `&$expand=books($select=title;$expand=currency($select=code,name,symbol))` + + `&$select=name` + ) + expect(data).to.eql({ + '@odata.context': + '$metadata#Authors(name,ID,books(title,ID,currency(code,name,symbol)))/$entity', + 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 () => { - const { data } = await GET `/admin/Books/201/stock/$value` - expect(data) .to.equal (12) + it('should check on current stocks with $value', async () => { + const { data } = await GET`/admin/Books/201/stock/$value` + expect(data).to.equal(12) }) - - it ('should reject out-of-stock orders', ()=> { - return expect (Promise.all ([ - 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/) + it('should reject out-of-stock orders', () => { + return expect( + Promise.all([ + 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/) }) + 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' }], + }, + ]) + }) }) diff --git a/test/hello-world.test.js b/test/hello-world.test.js new file mode 100644 index 00000000..790c03bf --- /dev/null +++ b/test/hello-world.test.js @@ -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!') + }) + +}) diff --git a/test/lib/helpers.js b/test/lib/helpers.js index 25664118..ba616786 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -6,7 +6,9 @@ module.exports = exports = { } // 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.after = global.afterAll } else { // it's mocha @@ -25,16 +27,7 @@ function _chai(){ /** Launching and testing a cds server */ -exports.launch = (project, args=['--in-memory?']) => { - - const cds = require('@sap/cds') - - // Supporting .launch () - 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}'`) - } +exports.launch = (project, ...args) => { // Setting up test server const console = global.console, logs=[] @@ -61,6 +54,19 @@ exports.launch = (project, args=['--in-memory?']) => { // launch cds server... 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 () + 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, time: ()=>{}, timeEnd: (...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) }, } + // return done (new Error(11)) process.env.PORT = '0' - const p = cds.exec ('run', project, ...args) // TODO w/ @sap/cds@3.33.3: , '--port', '0') - if (p && 'catch' in p) p.catch (done) + const p = cds.exec (cmd, project, ...args) // TODO w/ @sap/cds@3.33.3: , '--port', '0') + 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 }) => { Object.assign (test,{server,url}) @@ -83,7 +94,7 @@ exports.launch = (project, args=['--in-memory?']) => { // shutdown cds server... after (done => { if (global.console !== console) global.console = console - test.server.close (done) + test.server ? test.server.close (done) : done() }) function _error (e) {