From 0dcb66954887d3e470ec708b3b2b42e97d063fc5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 7 Apr 2020 16:38:39 +0200 Subject: [PATCH] Added tests for programmatical service consumption --- package.json | 2 +- test/cds.ql.test.js | 2 +- test/consuming-services.test.js | 93 +++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 test/consuming-services.test.js diff --git a/package.json b/package.json index 8fbbfe59..ae759021 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ }, "scripts": { "mocha": "npx mocha || echo", - "jest": "npx jest", + "jest": "npx jest --verbose", "test": "npm run jest -s" }, "license": "SAP SAMPLE CODE LICENSE", diff --git a/test/cds.ql.test.js b/test/cds.ql.test.js index ed2d362d..f3c08eeb 100644 --- a/test/cds.ql.test.js +++ b/test/cds.ql.test.js @@ -14,7 +14,7 @@ if (!is_cds_333) { // while jest has 'test' as alias to 'it', mocha doesn't if (!global.test) global.test = it -describe('cds.ql', () => { +describe('cds.ql → cqn', () => { // describe(`BUGS + GAPS...`, () => { diff --git a/test/consuming-services.test.js b/test/consuming-services.test.js new file mode 100644 index 00000000..1a91456d --- /dev/null +++ b/test/consuming-services.test.js @@ -0,0 +1,93 @@ +const { expect } = require('./capire') +const cds = require('@sap/cds') + +describe('Consuming Services locally', () => { + // + before('bootstrap db and services', async () => { + const model = await cds.load(['@capire/bookshop', '@capire/common']) + await cds.deploy(model).to('sqlite::memory:') + const { AdminService } = await cds.serve('AdminService').from(model) + const { Authors } = AdminService.entities + expect(AdminService).not.to.be.undefined + expect(Authors).not.to.be.undefined + }) + + it('supports targets as strings or reflected defs', async () => { + const AdminService = cds.connect.to('AdminService') + const { Authors } = AdminService.entities + expect(await AdminService.read(Authors)) + .to.eql(await AdminService.read('Authors')) + .to.eql(await AdminService.run(SELECT.from(Authors))) + // temporary workaround + if (AdminService.read.fix_50) + expect(await AdminService.run(SELECT.from(Authors))).to.eql( + await AdminService.run(SELECT.from('Authors')) + ) + }) + + it('allows reading from local services using cds.ql', async () => { + const AdminService = cds.connect.to('AdminService') + const query = SELECT.from('Authors', (a) => { + a.name, + a.books((b) => { + b.title, + b.currency((c) => { + c.name, c.symbol + }) + }) + }).where(`name like`, 'E%') + // temporary workaround + if (!AdminService.read.fix_50) { + query.SELECT.from.ref[0] = 'AdminService.Authors' + } + const authors = await AdminService.run(query) + expect(authors).to.containSubset([ + { + name: 'Emily Brontë', + books: [ + { + title: 'Wuthering Heights', + currency: { name: 'British Pound', symbol: '£' }, + }, + ], + }, + { + name: 'Edgar Allen Poe', + books: [ + { title: 'The Raven', currency: { name: 'US Dollar', symbol: '$' } }, + { title: 'Eleonora', currency: { name: 'US Dollar', symbol: '$' } }, + ], + }, + ]) + }) + + it('provides CRUD-style convenience methods', async () => {}) + + it('uses same methods for all kind of services, including dbs', async () => { + const srv = cds.connect.to('AdminService') + const db = cds.connect.to('db') + const { Authors } = srv.entities + const projection = (a) => { + a.name, + a.books((b) => { + b.title, + b.currency((c) => { + c.name, c.symbol + }) + }) + } + const query1 = SELECT.from(Authors, projection).where(`name like`, 'E%') + expect(await cds.run(query1)) + .to.eql(await db.run(query1)) + .to.eql(await srv.run(query1)) + .to.eql(await srv.read(Authors, projection).where(`name like`, 'E%')) + const query2 = cds.read(Authors, projection).where(`name like`, 'E%') + // temporary workaround + if (srv.read.fix_50) + expect(await cds.run(query1)) + .to.eql(await cds.run(query2)) + .to.eql(await db.run(query2)) + .to.eql(await srv.run(query2)) + .to.eql(await db.read(Authors, projection).where(`name like`, 'E%')) // FIXME!! + }) +})