Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions query/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var query = new peliasQuery.layout.FilteredBooleanQuery();

// scoring boost
query.sort( peliasQuery.view.sort_distance );
query.sort( peliasQuery.view.sort_popularity );

// non-scoring hard filters
query.filter( peliasQuery.view.boundary_circle );
Expand Down Expand Up @@ -96,6 +97,10 @@ function generateQuery( clean ){
vs.var('input:categories', clean.categories);
}

if (_.isString(clean.sort)) {
vs.var('sort:field', clean.sort);
}

return {
type: 'reverse',
body: query.render(vs)
Expand Down
2 changes: 2 additions & 0 deletions query/reverse_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module.exports = _.merge({}, peliasQuery.defaults, {

'centroid:field': 'center_point',

'sort:field': 'distance',

'sort:distance:order': 'asc',
'sort:distance:distance_type': 'plane',

Expand Down
34 changes: 34 additions & 0 deletions sanitizer/_sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const _ = require('lodash');
const DEFAULT_SORT = 'distance';

const allowed_values = ['distance', 'popularity'];

function _setup(){

return {
sanitize: function _sanitize( raw, clean ){

// error & warning messages
var messages = { errors: [], warnings: [] };

clean.sort = raw.sort;

if (!clean.sort) {
clean.sort = DEFAULT_SORT;
} else if( !allowed_values.includes(clean.sort) ){
messages.warnings.push('invalid \'sort\', using \'distance\'');
clean.sort = DEFAULT_SORT;
}

return messages;
},

expected: function _expected() {
// add size as a valid parameter
return [{ name: 'sort' }];
}
};
}

// export function
module.exports = _setup;
5 changes: 3 additions & 2 deletions sanitizer/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ module.exports.middleware = (_api_pelias_config) => {
geo_reverse: require('../sanitizer/_geo_reverse')(),
boundary_country: require('../sanitizer/_boundary_country')(),
categories: require('../sanitizer/_categories')(),
request_language: require('../sanitizer/_request_language')()
request_language: require('../sanitizer/_request_language')(),
sort: require('../sanitizer/_sort')()
};

return function( req, res, next ){
sanitizeAll.runAllChecks(req, sanitizers);
next();
};
};
};
24 changes: 23 additions & 1 deletion test/unit/query/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const views = {
layers: 'layers view',
categories: 'categories view',
sort_distance: 'sort_distance view',
sort_popularity: 'sort_popularity view',
boundary_gid: 'boundary_gid view'
};

Expand Down Expand Up @@ -70,7 +71,8 @@ module.exports.tests.query = (test, common) => {
]);

t.deepEquals(query.body.sort_functions, [
'sort_distance view'
'sort_distance view',
'sort_popularity view'
]);

t.end();
Expand Down Expand Up @@ -98,6 +100,26 @@ module.exports.tests.query = (test, common) => {

});

test('clean.sort should set sort:field parameter', t => {
const clean = {
sort: 'popularity'
};

const query = proxyquire('../../../query/reverse', {
'pelias-query': {
layout: {
FilteredBooleanQuery: MockQuery
},
view: views,
Vars: require('pelias-query').Vars
},
'./reverse_defaults': {}
})(clean);

t.deepEquals(query.body.vs.var('sort:field').toString(), 'popularity');
t.end();

});
};

module.exports.tests.sources = (test, common) => {
Expand Down
1 change: 1 addition & 0 deletions test/unit/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var tests = [
require('./sanitizer/_tokenizer'),
require('./sanitizer/_categories'),
require('./sanitizer/_boundary_gid'),
require('./sanitizer/_sort'),
require('./sanitizer/nearby'),
require('./sanitizer/autocomplete'),
require('./sanitizer/structured_geocoding'),
Expand Down
56 changes: 56 additions & 0 deletions test/unit/sanitizer/_sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var sanitizer = require('../../../sanitizer/_sort');

module.exports.tests = {};

module.exports.tests.sanitize_sort = function(test, common) {
test('sort=foo', function(t) {
var raw = { sort: 'foo' };
var clean = {};
var res = sanitizer().sanitize(raw, clean);
t.equal(res.errors.length, 0, 'should return no errors');
t.equal(res.warnings.length, 1, 'should return warning');
t.equal(res.warnings[0], 'invalid \'sort\', using \'distance\'');
t.equal(clean.sort, 'distance', 'default to distance');
t.end();
});

test('sort not set', function(t) {
var raw = {};
var clean = {};
var res = sanitizer().sanitize(raw, clean);
t.equal(res.errors.length, 0, 'should return no errors');
t.equal(res.warnings.length, 0, 'should return no warning');
t.equal(clean.sort, 'distance', 'default to distance');
t.end();
});

test('return an array of expected parameters in object form for validation', function(t) {
const expected = [{ name: 'sort' }];
const validParameters = sanitizer().expected();
t.deepEquals(validParameters, expected);
t.end();
});

var valid_sorts = ['popularity', 'distance'];
valid_sorts.forEach(function (sort) {
test('sort=' + sort, function (t) {
var raw = {sort: sort};
var clean = {};
var res = sanitizer().sanitize(raw, clean);
t.equal(res.errors.length, 0, 'should return no errors');
t.equal(res.warnings.length, 0, 'should return warning');
t.equal(clean.sort, sort, 'set to correct value');
t.end();
});
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANITIZE _sort ' + name, testFunction);
}

for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
13 changes: 11 additions & 2 deletions test/unit/sanitizer/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ module.exports.tests.sanitize = function(test, common) {
return { errors: [], warnings: [] };
}
};
}
},
'../sanitizer/_sort': function () {
return {
sanitize: () => {
called_sanitizers.push('_sort');
return { errors: [], warnings: [] };
}
};
},
});

const expected_sanitizers = [
Expand All @@ -124,7 +132,8 @@ module.exports.tests.sanitize = function(test, common) {
'_geo_reverse',
'_boundary_country',
'_categories',
'_request_language'
'_request_language',
'_sort',
];

const req = {};
Expand Down