Skip to content

Commit

Permalink
feat(intersection_syntactic_variations): add remaining test for post-…
Browse files Browse the repository at this point in the history
…processing feature. pass document pointer to script as first arg instead of being bound to $this
  • Loading branch information
missinglink committed Jan 30, 2019
1 parent e63c833 commit b92c404
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Document.prototype.removePostProcessingScript = function( fn ){

// call all post-processing scripts
Document.prototype.callPostProcessingScripts = function(){
this._post.forEach(function(fn){ fn.call(this); }, this);
this._post.forEach(function(fn){ fn.call(null, this); }, this);
return this;
};

Expand Down
24 changes: 12 additions & 12 deletions post/intersections.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ const INTERSECTION_LAYER_NAME = 'intersection';
const ADDRESS_STREET_PROP = 'street';
const ADDRESS_CROSS_STREET_PROP = 'cross_street';

function intersections(){
function intersections( doc ){

// only apply to docs from the intersection layer
if( this.getLayer() !== INTERSECTION_LAYER_NAME ){ return; }
if( doc.getLayer() !== INTERSECTION_LAYER_NAME ){ return; }

// ensure both street & cross street props are set
let street = this.getAddress(ADDRESS_STREET_PROP);
let street = doc.getAddress(ADDRESS_STREET_PROP);
if( !_.isString(street) || _.isEmpty(street) ){ return; }

let cross_street = this.getAddress(ADDRESS_CROSS_STREET_PROP);
let cross_street = doc.getAddress(ADDRESS_CROSS_STREET_PROP);
if( !_.isString(cross_street) || _.isEmpty(cross_street) ){ return; }

// generate name aliases for the intersection based on common syntactic variations.
// note: currently only english is supported, PRs for additional languages welcome.

// corner of A & B
this.setNameAlias('default', `${street} & ${cross_street}`);
this.setNameAlias('default', `${street} @ ${cross_street}`);
this.setNameAlias('default', `${street} at ${cross_street}`);
this.setNameAlias('default', `Corner of ${street} & ${cross_street}`);
doc.setNameAlias('default', `${street} & ${cross_street}`);
doc.setNameAlias('default', `${street} @ ${cross_street}`);
doc.setNameAlias('default', `${street} at ${cross_street}`);
doc.setNameAlias('default', `Corner of ${street} & ${cross_street}`);

// corner of B & A
this.setNameAlias('default', `${cross_street} & ${street}`);
this.setNameAlias('default', `${cross_street} @ ${street}`);
this.setNameAlias('default', `${cross_street} at ${street}`);
this.setNameAlias('default', `Corner of ${cross_street} & ${street}`);
doc.setNameAlias('default', `${cross_street} & ${street}`);
doc.setNameAlias('default', `${cross_street} @ ${street}`);
doc.setNameAlias('default', `${cross_street} at ${street}`);
doc.setNameAlias('default', `Corner of ${cross_street} & ${street}`);
}

module.exports = intersections;
95 changes: 95 additions & 0 deletions test/document/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

const Document = require('../../Document');
const intersections = require('../../post/intersections');
const DEFAULT_SCRIPTS = [ intersections ];

module.exports.tests = {};

module.exports.tests.addPostProcessingScript = function(test) {
test('default scripts', function(t) {
let doc = new Document('mysource','mylayer','myid');
t.deepEqual(doc._post, DEFAULT_SCRIPTS, 'default processing scripts');
t.end();
});
test('invalid type', function(t) {
let doc = new Document('mysource','mylayer','myid');
t.throws( doc.addPostProcessingScript.bind(doc, 'invalid'), /invalid document type, expecting: function got/ );
t.throws( doc.addPostProcessingScript.bind(doc, 100), /invalid document type, expecting: function got/ );
t.throws( doc.addPostProcessingScript.bind(doc, []), /invalid document type, expecting: function got/ );
t.throws( doc.addPostProcessingScript.bind(doc, {}), /invalid document type, expecting: function got/ );
t.end();
});
test('set function', function(t) {
let script = function(){};
let doc = new Document('mysource','mylayer','myid');
doc.addPostProcessingScript( script );
t.deepEqual(doc._post, DEFAULT_SCRIPTS.concat( script ), 'default processing scripts');
t.end();
});
test('set same function twice (allowed)', function(t) {
let script = function(){};
let doc = new Document('mysource','mylayer','myid');
doc.addPostProcessingScript( script );
doc.addPostProcessingScript( script );
t.deepEqual(doc._post, DEFAULT_SCRIPTS.concat( script, script ), 'default processing scripts');
t.end();
});
};

module.exports.tests.removePostProcessingScript = function(test) {
test('invalid type', function(t) {
let doc = new Document('mysource','mylayer','myid');
t.throws( doc.removePostProcessingScript.bind(doc, 'invalid'), /invalid document type, expecting: function got/ );
t.throws( doc.removePostProcessingScript.bind(doc, 100), /invalid document type, expecting: function got/ );
t.throws( doc.removePostProcessingScript.bind(doc, []), /invalid document type, expecting: function got/ );
t.throws( doc.removePostProcessingScript.bind(doc, {}), /invalid document type, expecting: function got/ );
t.end();
});
test('remove function', function(t) {
let script = function(){};
let doc = new Document('mysource','mylayer','myid');
doc._post = [ script ];
doc.removePostProcessingScript( script );
t.deepEqual(doc._post, [], 'script removed');
doc.removePostProcessingScript( script );
t.deepEqual(doc._post, [], 'no-op');
t.end();
});
test('remove duplicate functions (allowed)', function(t) {
let script = function(){};
let doc = new Document('mysource','mylayer','myid');
doc._post = [ script, script ];
doc.removePostProcessingScript( script );
t.deepEqual(doc._post, [], 'scripts removed');
doc.removePostProcessingScript( script );
t.deepEqual(doc._post, [], 'no-op');
t.end();
});
};

module.exports.tests.callPostProcessingScript = function(test) {
test('call all scripts', function(t) {
let doc = new Document('mysource','mylayer','myid');
doc._post = []; // remove any default scripts
t.plan(3);

// document pointer passed as first arg to scripts
doc.addPostProcessingScript((ref) => t.equal(doc, ref));
doc.addPostProcessingScript((ref) => t.equal(doc, ref));
doc.addPostProcessingScript((ref) => t.equal(doc, ref));

// call all scripts
doc.callPostProcessingScripts();
});
};

module.exports.all = function (tape, common) {

function test(name, testFunction) {
return tape('post processing: ' + name, testFunction);
}

for( let testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
17 changes: 17 additions & 0 deletions test/document/toESDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ module.exports.tests.toESDocumentWithCustomConfig = function(test) {
});
};

module.exports.tests.toESDocumentCallsProcessingScripts = function(test) {
test('toESDocument must call all post-processing scripts', function(t) {
let Document = proxyquire('../../Document', { 'pelias-config': fakeConfig });
let doc = new Document('mysource','mylayer','myid');
doc._post = []; // remove any default scripts
t.plan(3);

// document pointer passed as first arg to scripts
doc.addPostProcessingScript((ref) => t.equal(doc, ref));
doc.addPostProcessingScript((ref) => t.equal(doc, ref));
doc.addPostProcessingScript((ref) => t.equal(doc, ref));

// toESDocument() should, in tern, call callPostProcessingScripts()
doc.toESDocument();
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('Document: ' + name, testFunction);
Expand Down
6 changes: 3 additions & 3 deletions test/post/intersections.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ module.exports.tests.functional = function(test) {
var doc = new Document('mysource','intersection','myid');

// street and cross_street not set
intersections.call(doc);
intersections(doc);
t.deepEqual(doc.getNameAliases('default'), [], 'no names set');

// set street
doc.setAddress('street', 'Example Street');

// street set, cross_street not set
intersections.call(doc);
intersections(doc);
t.deepEqual(doc.getNameAliases('default'), [], 'no names set');

// set cross_street
doc.setAddress('cross_street', 'Cross Street');

// street and cross_street set
intersections.call(doc);
intersections(doc);

// intersection aliases defined
t.deepEqual(doc.getNameAliases('default'), [
Expand Down
1 change: 1 addition & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var tests = [
require('./document/layer.js'),
require('./document/source_id.js'),
require('./document/toESDocument.js'),
require('./document/post.js'),
require('./post/intersections.js'),
require('./DocumentMapperStream.js'),
require('./util/transform.js'),
Expand Down

0 comments on commit b92c404

Please sign in to comment.