From b733643f2aa02baae5691623ada3eef52830aeae Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 9 Apr 2022 12:38:24 +0200 Subject: [PATCH] fixed accidential push --- bookshop/srv/cat-service.js | 14 +++-------- bookshop/test/requests.http | 4 +-- test/odata.test.js | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/bookshop/srv/cat-service.js b/bookshop/srv/cat-service.js index 57315634..9fa85cca 100644 --- a/bookshop/srv/cat-service.js +++ b/bookshop/srv/cat-service.js @@ -1,20 +1,13 @@ const cds = require('@sap/cds') -class CatalogService extends cds.ApplicationService { async init(){ + +class CatalogService extends cds.ApplicationService { init(){ const { Books } = cds.entities ('sap.capire.bookshop') - const { ListOfBooks } = this.entities - - this.on('READ','ListOfBooks', ()=>{ - Error.stackTraceLimit = 22 - cds._debug = true - return SELECT.from (ListOfBooks) - }) // Reduce stock of ordered books if available stock suffices this.on ('submitOrder', async req => { const {book,quantity} = req.data if (quantity < 1) return req.reject (400,`quantity has to be 1 or more`) - cds._debug = true let b = await SELECT `stock` .from (Books,book) if (!b) return req.error (404,`Book #${book} doesn't exist`) let {stock} = b @@ -29,8 +22,7 @@ class CatalogService extends cds.ApplicationService { async init(){ if (each.stock > 111) each.title += ` -- 11% discount!` }) - await super.init() - return + return super.init() }} module.exports = { CatalogService } diff --git a/bookshop/test/requests.http b/bookshop/test/requests.http index a4af87d6..cbd8faff 100644 --- a/bookshop/test/requests.http +++ b/bookshop/test/requests.http @@ -16,9 +16,9 @@ GET {{server}}/browse/$metadata ### ------------------------------------------------------------------------ # Browse Books as any user -GET {{server}}/browse/Books? +GET {{server}}/browse/ListOfBooks? # &$select=title,stock - # &$expand=currency + &$expand=genre # &sap-language=de {{me}} diff --git a/test/odata.test.js b/test/odata.test.js index 8cee00ee..741fe413 100644 --- a/test/odata.test.js +++ b/test/odata.test.js @@ -4,6 +4,46 @@ axios.defaults.auth = { username: 'alice', password: 'admin' } describe('cap/samples - Bookshop APIs', () => { + // Genres + const Drama = { + "name": "Drama", + "descr": null, + "ID": 11, + "parent_ID": 10 + } + const Mystery = { + "name": "Mystery", + "descr": null, + "ID": 16, + "parent_ID": 10 + } + const Fantasy = { + "name": "Fantasy", + "descr": null, + "ID": 13, + "parent_ID": 10 + } + + // Currencies + const GBP = { + "name": "British Pound", + "descr": null, + "code": "GBP", + "symbol": "£" + } + const USD = { + "name": "US Dollar", + "descr": null, + "code": "USD", + "symbol": "$" + } + const JPY = { + "name": "Yen", + "descr": null, + "code": "JPY", + "symbol": "¥" + } + it('serves $metadata documents in v4', async () => { const { headers, status, data } = await GET `/browse/$metadata` @@ -16,6 +56,16 @@ describe('cap/samples - Bookshop APIs', () => { expect(data).to.contain('') }) + it('serves ListOfBooks?$expand=genre,currency', async () => { + const { data } = await GET `/browse/ListOfBooks ${{ + params: { $search: 'Po', $select: `title,author`, $expand:`genre,currency` }, + }}` + expect(data.value).to.eql([ + { ID: 251, title: 'The Raven', author: 'Edgar Allen Poe', genre:Mystery, currency:USD }, + { ID: 252, title: 'Eleonora', author: 'Edgar Allen Poe', genre:Mystery, currency:USD }, + ]) + }) + it('supports $search in multiple fields', async () => { const { data } = await GET `/browse/Books ${{ params: { $search: 'Po', $select: `title,author` },