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

Commit

Permalink
feat: support SAP CP CF PG Hyperscaler Service (#43)
Browse files Browse the repository at this point in the history
* feat: support SAP CP CF PG Hyperscaler Service

* test(service): run with different data sets

allow running the same test suite
w/ different data sets
so we can once run against local/dockerized pg
and against hyperscaler(s).
excerpt from verbose log output:
[local] OData to Postgres dialect
//...
[scp] OData to Postgres dialect
//...

* test: refactor credentials properly

* fix: rename with proper r

* chore(deps): update

Co-authored-by: Volker Buzek <[email protected]>
  • Loading branch information
gregorwolf and vobu committed Nov 16, 2020
1 parent 2e11fdf commit c0ba1dd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
node_modules/
.idea/
__tests__/__assets__/cap-proj/package*
*-env.json
51 changes: 41 additions & 10 deletions __tests__/lib/pg/service.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/srv/db/deploy')
const path = require('path')

// mock (package|.cds'rc).json entries
cds.env.requires.db = { kind: 'postgres' }
Expand All @@ -9,28 +10,58 @@ cds.env.requires.postgres = {

const guidRegEx = /\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b/

describe('OData to Postgres dialect', () => {
// construct suite data sets
const localCredentials = {
host: 'localhost',
port: '5432',
database: 'beershop',
username: 'postgres',
password: 'postgres',
}
const localModel = './__tests__/__assets__/cap-proj/srv/'
const scpPostgresCredentials = {
hostname: 'localhost',
port: '5432',
dbname: 'beershop',
username: 'postgres',
password: 'postgres',
}
const scpModel = './__tests__/__assets__/cap-proj/srv/'

// run test suite with different sets of data
describe.each([
['local', localCredentials, localModel],
['scp', scpPostgresCredentials, scpModel],
])('[%s] OData to Postgres dialect', (_suitename /* translates to %s via printf */, credentials, model) => {
const app = require('express')()
const request = require('supertest')(app)

beforeAll(async () => {
this._model = './__tests__/__assets__/cap-proj/srv/'
// mock console.*
// in order not to pollute test logs
global.console = {
log: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
error: jest.fn(),
}

this._model = model
this._dbProperties = {
kind: 'postgres',
model: this._model,
credentials: {
host: 'localhost',
port: '5432',
database: 'beershop',
username: 'postgres',
password: 'postgres',
},
credentials: credentials,
}
cds.db = await cds.connect.to(this._dbProperties)

// serve only a plain beershop
// that matches the db content/setup in dockered pg
await cds.serve('BeershopService').from(`${__dirname}/../../__assets__/cap-proj/srv/beershop-service`).in(app)
const servicePath = path.resolve(this._model, 'beershop-service')
await cds.serve('BeershopService').from(servicePath).in(app)
})

afterAll(() => {
delete global.console // avoid side effect
})

beforeEach(async () => {
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const Service = require('./lib/pg/Sevice')
const Service = require('./lib/pg/Service')

module.exports = Service
17 changes: 17 additions & 0 deletions lib/pg/Sevice.js → lib/pg/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ module.exports = class PostgresDatabase extends cds.DatabaseService {
if (this.options.credentials && this.options.credentials.username) {
this.options.credentials.user = this.options.credentials.username
}
// Special handling for:
// SAP Cloud Platform - Cloud Foundry - PostgreSQL Hyperscaler Service
if (this.options.credentials && this.options.credentials.hostname) {
this.options.credentials.host = this.options.credentials.hostname
}
if (this.options.credentials && this.options.credentials.dbname) {
this.options.credentials.database = this.options.credentials.dbname
}
if (this.options.credentials && this.options.credentials.sslrootcert) {
if (typeof this.options.credentials.sslRequired === 'undefined') {
this.options.credentials.sslRequired = true
}
this.options.credentials.ssl = {
rejectUnauthorized: false,
ca: this.options.credentials.sslrootcert,
}
}
this._pool = new Pool(this.options.credentials)

// Mapping of the pg specific executions. Inspired by the SQLite and HANA adapters.
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0ba1dd

Please sign in to comment.