Skip to content

Commit

Permalink
no need to add is null check for unmanaged
Browse files Browse the repository at this point in the history
  • Loading branch information
patricebender committed Feb 12, 2025
1 parent b9550ce commit a175c52
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 6 additions & 5 deletions db-service/lib/assert-constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ function attachConstraints(_results, req) {
const { condition, element } = constraint
const xpr = []
// if the element is nullable, we prepend xpr with `<element> IS NULL OR …`
if (!element.notNull) {
if (element.on)
// null check the whole xpr for unmanaged assocs which vanish in the result
xpr.unshift({ xpr: condition.xpr }, 'is', 'null', 'or')
else xpr.unshift({ ref: [element.name] }, 'is', 'null', 'or')
if (!element.notNull && !element.on) {
xpr.unshift({ ref: [element.name] }, 'is', 'null', 'or')
}
xpr.push({ xpr: condition.xpr })
return {
Expand Down Expand Up @@ -138,6 +135,10 @@ function attachConstraints(_results, req) {
return elmConstraints
}
}

function wrapInCaseWhen(xpr) {
return ['case', 'when', { xpr }, 'then', { val: true }, 'else', { val: false }, 'end']
}
}

async function checkConstraints(req) {
Expand Down
3 changes: 2 additions & 1 deletion test/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ entity Genres : sap.common.CodeList {
@assert.constraint: ( children.name in (
'Fiction', 'Drama', 'Poetry', 'Fantasy', 'Science Fiction',
'Romance', 'Mystery', 'Thriller', 'Dystopia', 'Fairy Tale',
'Non-Fiction', 'Biography', 'Autobiography', 'Essay', 'Speech'
'Non-Fiction', 'Biography', 'Autobiography', 'Essay', 'Speech',
'New Genre', 'New Sub-Genre'
) )
children : Composition of many Genres
on children.parent = $self;
Expand Down
12 changes: 11 additions & 1 deletion test/scenarios/bookshop/assert-constraint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,19 @@ describe('Bookshop - assertions', () => {
]
}, { auth: { username: 'alice' } })
).to.be.rejectedWith(/@assert.constraint children failed/)
// nothing should have been created
const genre = await SELECT.from(Genres).where('ID in (90, 91)')
expect(genre).to.be.empty
// positive case
await POST('admin/Genres', {
ID: 100,
name: 'New Genre', // OK
children: [
{ ID: 101, name: 'New Sub-Genre' }, // also OK
],
}, { auth: { username: 'alice' } })
const genres = await SELECT.from(Genres).where('ID in (100, 101)')
// both should have been created
expect(genres).to.have.length(2)
})
})
})

0 comments on commit a175c52

Please sign in to comment.