Skip to content

Commit

Permalink
Merge 6d61f1a into af889d7
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambie committed Apr 16, 2019
2 parents af889d7 + 6d61f1a commit 875b44b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
12 changes: 11 additions & 1 deletion dadi/lib/datasource/index.js
Expand Up @@ -169,8 +169,18 @@ Datasource.prototype.loadDatasource = function (done) {

try {
const body = fs.readFileSync(filepath, { encoding: 'utf-8' })

schema = JSON.parse(body)

// If the schema has been provided as a flat structure,
// without the "datasource" property, wrap the provided data
// in a new "datasource" property
if (!schema.datasource) {
schema = {
datasource: {
...schema
}
}
}
done(null, schema)
} catch (err) {
log.error(
Expand Down
2 changes: 1 addition & 1 deletion dadi/lib/page/index.js
Expand Up @@ -11,7 +11,7 @@ const Page = function (name, schema, hostKey, templateCandidate) {
schema.settings = schema.settings || {}

this.name = name
this.key = schema.page.key || name
this.key = schema.page && schema.page.key ? schema.page.key : name
this.hostKey = hostKey || ''
this.template = schema.template || templateCandidate
this.contentType = schema.contentType || 'text/html'
Expand Down
5 changes: 5 additions & 0 deletions dadi/lib/providers/dadiapi.js
Expand Up @@ -369,6 +369,11 @@ DadiApiProvider.prototype.processDatasourceParameters = function (
params.push({ cache: datasourceParams.cache })
}

// pass compose parameter to API endpoint
if (datasourceParams.hasOwnProperty('compose')) {
params.push({ compose: datasourceParams.compose })
}

// pass language to api endpoint
if (datasourceParams.lang) {
params.push({ lang: datasourceParams.lang })
Expand Down
75 changes: 69 additions & 6 deletions test/unit/datasource.js
@@ -1,3 +1,4 @@
const fs = require('fs')
const path = require('path')
const should = require('should')
const sinon = require('sinon')
Expand Down Expand Up @@ -52,14 +53,45 @@ describe('Datasource', done => {
const p = page(name, schema)
const dsName = 'car_makes'
const options = TestHelper.getPathOptions()

let flattenedSchema = {
key: 'car_makes',
name: 'Makes datasource',
source: {
type: 'static',
endpoint: '1.0/cars/makes'
},
caching: {
ttl: 300,
directory: {
enabled: true,
path: './cache/web/',
extension: 'json'
},
redis: {
enabled: false
}
},
paginate: true,
count: 20,
sort: { name: 1 },
fields: { name: 1, _id: 0 },
requestParams: [{ param: 'make', field: 'name' }]
}

let filePath = options.datasourcePath + '/' + dsName + '.json'
let stub = sinon
.stub(fs, 'readFileSync')
.withArgs(filePath, { encoding: 'utf-8' })
.callsFake(() => {
return JSON.stringify(flattenedSchema)
})

new Datasource(p, dsName, options).init((err, ds) => {
if (err) done(err)
const fsSchema = TestHelper.getSchemaFromFile(
options.datasourcePath,
dsName,
null
)
ds.schema.datasource.key.should.eql(fsSchema.datasource.key)

fs.readFileSync.restore()
ds.schema.datasource.key.should.eql('car_makes')
done()
})
})
Expand Down Expand Up @@ -266,6 +298,7 @@ describe('Datasource', done => {
const schema = TestHelper.getPageSchema()
const dsName = 'car_makes_nosource'

config.set('api.host', '127.0.0.1')
config.set('api.port', 80)

new Datasource(null, dsName, TestHelper.getPathOptions()).init(
Expand Down Expand Up @@ -825,6 +858,36 @@ describe('Datasource', done => {
})
})

it('should pass compose param to the endpoint', done => {
const name = 'test'
const schema = TestHelper.getPageSchema()
const p = page(name, schema)
const dsName = 'car_makes'
const options = TestHelper.getPathOptions()
const dsSchema = TestHelper.getSchemaFromFile(
options.datasourcePath,
dsName
)

// modify the endpoint to give it a compose setting
dsSchema.datasource.compose = 'all'

sinon
.stub(Datasource.Datasource.prototype, 'loadDatasource')
.yields(null, dsSchema)

const req = { params: {}, url: '/1.0/makes/ford/2' }

new Datasource(p, dsName, options).init((err, ds) => {
Datasource.Datasource.prototype.loadDatasource.restore()
ds.processRequest(dsName, req)
ds.provider.endpoint.should.eql(
'http://127.0.0.1:3000/1.0/cars/makes?count=20&page=1&filter={}&fields={"name":1,"_id":0}&sort={"name":1}&compose=all'
)
done()
})
})

it('should pass page param to the endpoint when page.passFilters is true', done => {
const name = 'test'
const schema = TestHelper.getPageSchema()
Expand Down
8 changes: 8 additions & 0 deletions test/unit/page.js
Expand Up @@ -35,6 +35,14 @@ describe('Page', done => {
done()
})

it('should use filename if no name supplied', done => {
const name = 'test-no-name-given'
const schema = TestHelper.getPageSchema()
delete schema.page
page(name, schema).key.should.eql('test-no-name-given')
done()
})

it('should attach key using name if not supplied', done => {
const name = 'test'
const schema = TestHelper.getPageSchema()
Expand Down

0 comments on commit 875b44b

Please sign in to comment.