From 85621ad0acf6bbe49afc94ccb5b10b139fc9da57 Mon Sep 17 00:00:00 2001 From: I548646 Date: Wed, 15 Jan 2025 10:31:26 +0100 Subject: [PATCH 1/3] fix: contains not evaluating to boolean --- db-service/lib/cql-functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db-service/lib/cql-functions.js b/db-service/lib/cql-functions.js index 2f1f8f7f0..8c0d7ba19 100644 --- a/db-service/lib/cql-functions.js +++ b/db-service/lib/cql-functions.js @@ -49,7 +49,7 @@ const StandardFunctions = { * @param {...string} args * @returns {string} */ - contains: (...args) => `ifnull(instr(${args}),0)`, + contains: (...args) => `(ifnull(instr(${args}),0) > 0)`, /** * Generates SQL statement that produces the number of elements in a given collection * @param {string} x From 47a8bcf68e79c7a032596911c9096d50815bc967 Mon Sep 17 00:00:00 2001 From: I548646 Date: Wed, 15 Jan 2025 10:32:04 +0100 Subject: [PATCH 2/3] test: compare contains result to boolean explicitly --- test/scenarios/bookshop/funcs.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/scenarios/bookshop/funcs.test.js b/test/scenarios/bookshop/funcs.test.js index b634f101b..3d4c46f43 100644 --- a/test/scenarios/bookshop/funcs.test.js +++ b/test/scenarios/bookshop/funcs.test.js @@ -23,6 +23,24 @@ describe('Bookshop - Functions', () => { expect(res.data.value.length).to.be.eq(2) }) + test('contains with explicit equals boolean value', async () => { + const res = await GET("/browse/Books?$filter=contains(author,'Allen') eq true") + expect(res.status).to.be.eq(200) + expect(res.data.value.length).to.be.eq(2) + }) + + test('contains with explicit not equals boolean value', async () => { + const res = await GET("/browse/Books?$filter=contains(author,'Allen') ne false") + expect(res.status).to.be.eq(200) + expect(res.data.value.length).to.be.eq(2) + }) + + test('not contains with explicit equals boolean value', async () => { + const res = await GET("/browse/Books?$filter=not contains(author,'Allen') eq false") + expect(res.status).to.be.eq(200) + expect(res.data.value.length).to.be.eq(2) + }) + test('avg', async () => { const { Books } = cds.entities const res = await cds.run(CQL`SELECT from ${Books} { From ce65cee659681d3206b149a2511727b7213eaa31 Mon Sep 17 00:00:00 2001 From: I548646 Date: Wed, 15 Jan 2025 11:42:53 +0100 Subject: [PATCH 3/3] test: update test cases --- test/scenarios/bookshop/funcs.test.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/test/scenarios/bookshop/funcs.test.js b/test/scenarios/bookshop/funcs.test.js index 3d4c46f43..f3207fe05 100644 --- a/test/scenarios/bookshop/funcs.test.js +++ b/test/scenarios/bookshop/funcs.test.js @@ -23,23 +23,29 @@ describe('Bookshop - Functions', () => { expect(res.data.value.length).to.be.eq(2) }) + test('contains with search string that can not be found', async () => { + const res = await GET(`/browse/Books?$filter=contains(author,'string that can not be found in any author name')`) + expect(res.status).to.be.eq(200) + expect(res.data.value.length).to.be.eq(0) + }) + + test('contains with search string null', async () => { + const res = await GET(`/browse/Books?$filter=contains(author,null)`) + expect(res.status).to.be.eq(200) + expect(res.data.value.length).to.be.eq(0) + }) + test('contains with explicit equals boolean value', async () => { const res = await GET("/browse/Books?$filter=contains(author,'Allen') eq true") expect(res.status).to.be.eq(200) expect(res.data.value.length).to.be.eq(2) }) - + test('contains with explicit not equals boolean value', async () => { const res = await GET("/browse/Books?$filter=contains(author,'Allen') ne false") expect(res.status).to.be.eq(200) expect(res.data.value.length).to.be.eq(2) }) - - test('not contains with explicit equals boolean value', async () => { - const res = await GET("/browse/Books?$filter=not contains(author,'Allen') eq false") - expect(res.status).to.be.eq(200) - expect(res.data.value.length).to.be.eq(2) - }) test('avg', async () => { const { Books } = cds.entities