Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
fix: issue #315 lowercase INSERT results (#316)
Browse files Browse the repository at this point in the history
closes #315 

* test: check case of the result elements

* test: add read request to TypeChecks

* feat: add createBeer to compare INSERT
between PostgreSQL and sqlite

* fix: issue #315

* test: for issue #315

* chore: set ecmaVers. that allows optional chaining

* fix: use optional chaining
  • Loading branch information
gregorwolf committed Jun 13, 2022
1 parent 8c94807 commit 7ad25a1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2020
},
"globals": {
"SELECT": true,
Expand Down
5 changes: 4 additions & 1 deletion __tests__/__assets__/cap-proj/rest-client-test/draft.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ POST http:https://localhost:4004/beershop/TypeChecksWithDraft
Accept:application/json;odata.metadata=minimal;IEEE754Compatible=true
Content-Type:application/json;charset=UTF-8;IEEE754Compatible=true

{}
{}

### Read TypeChecksWithDraft
GET http:https://localhost:4004/beershop/TypeChecksWithDraft
5 changes: 5 additions & 0 deletions __tests__/__assets__/cap-proj/rest-client-test/requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ POST {{$dotenv scpServiceURL}}/beershop/reset
Content-Type: application/json

{}
###
# create a beer using action
POST {{$dotenv scpServiceURL}}/beershop/createBeer
Content-Type: application/json

{}
###
# delete a brewery
DELETE {{$dotenv scpServiceURL}}/beershop/Breweries(0465e9ca-6255-4f5c-b8ba-7439531f8d28)
1 change: 1 addition & 0 deletions __tests__/__assets__/cap-proj/srv/beershop-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ service BeershopService {

extend service BeershopService with {
action reset();
action createBeer();
}
6 changes: 6 additions & 0 deletions __tests__/__assets__/cap-proj/srv/beershop-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module.exports = (srv) => {
}
await cds.deploy('./srv/', {}).to(db)
})
srv.on('createBeer', async () => {
const { Beers } = cds.entities('csw')
const entries = [{ name: 'Beer1', abv: 1.0, ibu: 1, brewery_ID: '0465e9ca-6255-4f5c-b8ba-7439531f8d28' }]
const insertResult = await cds.run(INSERT.into(Beers).entries(entries))
console.log(insertResult)
})
srv.before('READ', '*', async (req) => {
if (req.headers.schema) {
req.user.schema = req.headers.schema
Expand Down
11 changes: 8 additions & 3 deletions __tests__/lib/pg/ql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ describe('QL to PostgreSQL', () => {
expect(results[0].brewery).toHaveProperty('name', 'Rittmayer Hallerndorf')
expect(results.length).toStrictEqual(4)
})
test('-> case of the query result', async () => {
const { TypeChecks } = cds.entities('csw')
const results = await cds.run(SELECT.one.from(TypeChecks))
expect(results).toHaveProperty('type_Boolean')
})
})

describe('INSERT', () => {
Expand Down Expand Up @@ -176,11 +181,11 @@ describe('QL to PostgreSQL', () => {
expect(insertResult.affectedRows).toStrictEqual(3)
expect(insertResult == 3).toStrictEqual(true)
expect(insertResult.valueOf()).toStrictEqual(insertResult.affectedRows)
const beers = [...entries]
const beers = insertResult.results
expect(beers.length).toStrictEqual(3)
expect(beers[0].ID).toMatch(uuidRegex)
expect(beers[0].createdAt).toMatch(timestampRegex)
expect(beers[0].modifiedAt).toMatch(timestampRegex)
expect(beers[0].createdAt.toISOString()).toMatch(timestampRegex)
expect(beers[0].modifiedAt.toISOString()).toMatch(timestampRegex)
})
})

Expand Down
3 changes: 2 additions & 1 deletion lib/pg/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ const executeInsertCQN = async (model, dbc, cqn, user, locale, txTimestamp) => {

let results = await Promise.all(resultPromises)
results = flattenArray(results)
results = results.map((result) => remapColumnNames(model.definitions[cqn.INSERT.into], result))
const entity = cqn.INSERT?.into?.ref?.[0] ? cqn.INSERT.into.ref[0] : cqn.INSERT.into
results = results.map((result) => remapColumnNames(model.definitions[entity], result))
return results
}

Expand Down

0 comments on commit 7ad25a1

Please sign in to comment.