Skip to content

Commit

Permalink
Merge branch 'hotfix/2.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Jun 1, 2020
2 parents d5d63ed + ecd20ec commit 9190365
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 33 deletions.
4 changes: 3 additions & 1 deletion lib/mapper/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ function normalizeDate(date, isDateStart) {
}

function validate_filters(_values, _default) {
return Object.assign(_values, _default)
return {
..._default, ..._values
}
}

exports = module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion lib/mapper/ordination.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ function processQuery(query) {


function validate_sort(_values, _default) {
return Object.assign(_values, _default)
return {
..._default, ..._values
}
}

exports = module.exports = {
Expand Down
5 changes: 1 addition & 4 deletions lib/mapper/pagination.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function pagination(query, options) {
const result = {}

result.limit = options.default.pagination.limit

if (query.limit) {
Expand All @@ -26,9 +25,7 @@ function processQuery(query) {
if (query instanceof Array) {
query = query[0]
}
query = query.replace(/([^\d\s])|(\s{1,})/gi, '')
if (parseInt(query)) return (parseInt(query))
return undefined
return parseInt(query.replace(/([^\d\s])|(\s{1,})/gi, ''))
}

exports = module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/index.custom.config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('queryFilter()', function () {
})
})

context('when use pagination with page and query page is not a number', function () {
context('when use pagination with page and query page and limit is not a number', function () {
it('should return pagination param with default limit', function () {

const expect_pagination = {
Expand All @@ -94,7 +94,7 @@ describe('queryFilter()', function () {
const options = JSON.parse(JSON.stringify(custom_options))
options.default.pagination = expect_pagination

const query = '?limit=teen'
const query = '?page=current&limit=teen'

return request(app)
.get(query)
Expand Down
24 changes: 14 additions & 10 deletions test/unit/fields.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ describe('QueryString: Fields', function () {

context('when query fields are a simple string', function () {
it('should return a JSON with field params', function (done) {
const query = { fields: 'name,age,created_at' }
const query = {fields: 'name,age,created_at'}
verify(fields.fields(query, default_options))
done()
})
})

context('when query fields are an array of strings', function () {
it('should return a JSON with field params', function (done) {
const query = { fields: ['name,age', 'created_at'] }
const query = {fields: ['name,age', 'created_at']}
verify(fields.fields(query, default_options))
done()
})
})

context('when there are blank spaces between query fields', function () {
it('should return a JSON with field params, ignoring the blank space', function (done) {
const query = { fields: ' name , name, age , created_at' }
const query = {fields: ' name , name, age , created_at'}
verify(fields.fields(query, default_options))
done()
})
})

context('when there are null fields in query fields', function () {
it('should return a JSON with field params, ignoring the null fields', function (done) {
const query = { fields: ',,name,,,age,,,,,created_at,,' }
const query = {fields: ',,name,,,age,,,,,created_at,,'}
verify(fields.fields(query, default_options))
done()
})
})

context('when there are special characters in query fields', function () {
it('should return a JSON with field params, ignoring the special characteres', function (done) {
const query = { fields: ' ,,, ^ & * ( ´) @!n@a"m "e,$%ag" e",created _a t ' }
const query = {fields: ' ,,, ^ & * ( ´) @!n@a"m "e,$%ag" e",created _a t '}
verify(fields.fields(query, default_options))
done()

Expand All @@ -52,17 +52,21 @@ describe('QueryString: Fields', function () {
})
})

context('when use custom params without query', function () {
context('when use custom params', function () {
it('should return a JSON with custom params', function () {
const custom_options = { default: { fields: { name: 1, age: 1, _id: 0 } } }
const custom_options = {default: {fields: {name: 1, age: 1, _id: 0}}}
const result = fields.fields({}, custom_options)
expect(result).to.have.property('name')
expect(result).to.have.property('age')
expect(result).to.have.property('_id')
expect(result.name).to.eql(custom_options.default.fields.name)
expect(result.age).to.eql(custom_options.default.fields.age)
expect(result._id).to.eql(custom_options.default.fields._id)
})

it('should return a JSON with custom params and those of the query', function () {
const custom_options = {default: {fields: {name: 1, _id: 1}}}
const result = fields.fields({fields: 'age'}, custom_options)
expect(result.name).to.eql(custom_options.default.fields.name)
expect(result._id).to.eql(custom_options.default.fields._id)
expect(result.age).to.eql(1)
})
})
})
Expand Down
16 changes: 14 additions & 2 deletions test/unit/filters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ describe('QueryString: Filters', function () {
})
})

context('when use the default options without query', function () {
context('when use the default options', function () {
it('should return a JSON with default filters params', function (done) {
const result = filter.filters({}, default_options)
expect(result).to.eql(default_options.default.filters)
done()
})

it('should return a JSON with default filters params and those of the query', function (done) {
const options = {
...default_options, ...{
default: {filters: {age: '19', name: 'John'}}
}
}
const result = filter.filters({age: '21'}, options)
expect(result.name).to.eql(options.default.filters.name)
expect(result.age).to.eql(21)
done()
})
})
})

Expand All @@ -45,4 +57,4 @@ function verify(result) {
expect(result).to.have.property('age')
expect(result.name).to.eql('lucas')
expect(result.age).to.eql(30)
}
}
29 changes: 18 additions & 11 deletions test/unit/ordination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@ const expect = require('chai').expect
const ordination = require('../../lib/mapper/ordination')

describe('QueryString: Ordination', function () {

context('when ordination query is a simple string', function () {
it('should return a JSON with order params', function (done) {
verify(ordination.sort({ sort: '-name,age,created_at' }, default_options))
verify(ordination.sort({sort: '-name,age,created_at'}, default_options))
done()
})
})

context('when ordination query is an array of strings', function () {
it('should return a JSON with order params', function (done) {
verify(ordination.sort({ sort: ['-name,age', 'created_at'] }, default_options))
verify(ordination.sort({sort: ['-name,age', 'created_at']}, default_options))
done()
})
})

context('when there are blank spaces between ordination query', function () {
it('should return a JSON with order params, ignoring the blank space', function (done) {
verify(ordination.sort({ sort: '-na m e, age, cr eat ed_at' }, default_options))
verify(ordination.sort({sort: '-na m e, age, cr eat ed_at'}, default_options))
done()
})
})

context('when there are null fields in ordination query', function () {
it('should return a JSON with order params, ignoring the null fields', function (done) {
verify(ordination.sort({ sort: ',,,,,-name,,,,age,,,created_at,,,,,,,' }, default_options))
verify(ordination.sort({sort: ',,,,,-name,,,,age,,,created_at,,,,,,,'}, default_options))
done()
})
})

context('when there are special characters in ordination query', function () {
it('should return a JSON with order params, ignoring the special characteres', function (done) {
verify(ordination.sort({ sort: '-$%n@am#$e??,!!ag%e,c***r$@$eated_at' }, default_options))
verify(ordination.sort({sort: '-$%n@am#$e??,!!ag%e,c***r$@$eated_at'}, default_options))
done()
})

})
})

context('when use the default options without query', function () {
it('should return a JSON with default ordination params', function (done) {
Expand All @@ -48,16 +48,23 @@ describe('QueryString: Ordination', function () {
})
})

context('when use custom params without query', function () {
context('when use custom params', function () {
it('should return a JSON with custom params', function () {
const custom_options = { default: { sort: { created_at: 'asc' }}}
const custom_options = {default: {sort: {created_at: 'asc'}}}
const result = ordination.sort({}, custom_options)
expect(result).is.not.null
expect(result).to.have.property('created_at')
expect(result.created_at).to.eql('asc')
})
})

it('should return a JSON with custom parameters and those of the query', function () {
const custom_options = {default: {sort: {created_at: 'asc'}}}
const result = ordination.sort({sort: '-created_at,-age,name'}, custom_options)
expect(result.created_at).to.eql('desc')
expect(result.age).to.eql('desc')
expect(result.name).to.eql('asc')
})
})
})

function verify(result) {
Expand All @@ -68,4 +75,4 @@ function verify(result) {
expect(result.age).to.eql('asc')
expect(result.created_at).to.eql('asc')

}
}
21 changes: 19 additions & 2 deletions test/unit/pagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('QueryString: Pagination', function () {
})
})

context('when use custom options without query with limit and page params', function () {
context('when use custom params', function () {
it('should return a JSON with custom params', function (done) {
const custom_options = {
default: {
Expand All @@ -175,12 +175,29 @@ describe('QueryString: Pagination', function () {
},
use_page: true
}
const result = pagination.pagination({page: 'one'}, custom_options)
const result = pagination.pagination({}, custom_options)
verifyPage(result)
expect(result.limit).to.eql(15)
expect(result.page).to.eql(2)
done()
})

it('should return a JSON with custom parameters and those of the query', function (done) {
const custom_options = {
default: {
pagination: {
limit: 50,
page: 5
}
},
use_page: true
}
const result = pagination.pagination({page: '3', limit: '10'}, custom_options)
verifyPage(result)
expect(result.limit).to.eql(10)
expect(result.page).to.eql(3)
done()
})
})
})

Expand Down

0 comments on commit 9190365

Please sign in to comment.