Browse Source

updated code+tests to findById name array output format

pull/932/head
Stephen Hess 8 years ago
parent
commit
775fd1f568
  1. 8
      middleware/changeLanguage.js
  2. 84
      test/unit/middleware/changeLanguage.js

8
middleware/changeLanguage.js

@ -1,6 +1,7 @@
var logger = require( 'pelias-logger' ).get( 'api' ); var logger = require( 'pelias-logger' ).get( 'api' );
var service = require('../service/language'); var service = require('../service/language');
const _ = require('lodash');
/** /**
example response from language web service: example response from language web service:
@ -28,7 +29,6 @@ example response from language web service:
**/ **/
function setup() { function setup() {
var transport = service.findById(); var transport = service.findById();
var middleware = function(req, res, next) { var middleware = function(req, res, next) {
@ -145,18 +145,18 @@ function updateDocs( req, res, translations ){
if( !translations[id].hasOwnProperty( 'names' ) ){ continue; } if( !translations[id].hasOwnProperty( 'names' ) ){ continue; }
// requested language is not available // requested language is not available
if( !translations[id].names.hasOwnProperty( requestLanguage ) ){ if (_.isEmpty(_.get(translations[id].names, requestLanguage, [] ))) {
logger.info( '[language] [info]', 'missing translation', requestLanguage, id ); logger.info( '[language] [info]', 'missing translation', requestLanguage, id );
continue; continue;
} }
// translate 'parent.*' property // translate 'parent.*' property
adminValues[i] = translations[id].names[ requestLanguage ]; adminValues[i] = translations[id].names[ requestLanguage ][0];
// if the record is an admin record we also translate // if the record is an admin record we also translate
// the 'name.default' property. // the 'name.default' property.
if( adminKey === doc.layer ){ if( adminKey === doc.layer ){
doc.name.default = translations[id].names[ requestLanguage ]; doc.name.default = translations[id].names[ requestLanguage ][0];
} }
} }
} }

84
test/unit/middleware/changeLanguage.js

@ -2,6 +2,7 @@
var fs = require('fs'), var fs = require('fs'),
tmp = require('tmp'), tmp = require('tmp'),
setup = require('../../../middleware/changeLanguage'); setup = require('../../../middleware/changeLanguage');
const proxyquire = require('proxyquire').noCallThru();
// load middleware using the default pelias config // load middleware using the default pelias config
var load = function(){ var load = function(){
@ -144,7 +145,7 @@ module.exports.tests.hit = function(test, common) {
name: { default: 'London' }, name: { default: 'London' },
parent: { parent: {
locality_id: [ 101735809 ], locality_id: [ 101735809 ],
locaity: [ 'London' ] locality: [ 'London' ]
} }
} }
]}; ]};
@ -158,23 +159,23 @@ module.exports.tests.hit = function(test, common) {
cb( null, { cb( null, {
'101750367': { '101750367': {
'names': { 'names': {
'default':'London', 'default':['London'],
'chi':'倫敦', 'chi':['倫敦'],
'spa':'Londres', 'spa':['Londres'],
'eng':'London', 'eng':['London'],
'hin':'लदन', 'hin':['लदन'],
'ara':'لندن', 'ara':['لندن'],
'por':'Londres', 'por':['Londres'],
'ben':'লনডন', 'ben':['লনডন'],
'rus':'Лондон', 'rus':['Лондон'],
'jpn':'ロンドン', 'jpn':['ロンドン'],
'kor':'런던' 'kor':['런던']
} }
}, },
'101735809': { '101735809': {
'names':{ 'names':{
'default':'London', 'default':['London'],
'eng':'London' 'eng':['London']
} }
} }
}); });
@ -195,13 +196,66 @@ module.exports.tests.hit = function(test, common) {
name: { default: 'London' }, name: { default: 'London' },
parent: { parent: {
locality_id: [ 101735809 ], locality_id: [ 101735809 ],
locaity: [ 'London' ] locality: [ 'London' ]
} }
} }
]}); ]});
t.end(); t.end();
}); });
}); });
test('empty array name translation should not change the value', t => {
t.plan(2);
const req = { language: { iso6393: 'ISO3 value' } };
const res = {
data: [
{
layer: 'locality',
name: { default: 'original name' },
parent: {
locality_id: [ 123 ],
locality: [ 'original name' ]
}
}
]
};
const changeLanguage = proxyquire('../../../middleware/changeLanguage', {
'../service/language': {
findById: () => ({
query: (ids, callback) => {
t.deepEquals(ids, ['123']);
callback(null, {
'123': {
'names': {
'ISO3 value':[]
}
}
});
}
})
}
})();
changeLanguage(req, res, () => {
t.deepEqual( res, { data: [
{
layer: 'locality',
name: {
default: 'original name'
},
parent: {
locality_id: [ 123 ],
locality: [ 'original name' ]
}
}
]});
});
});
}; };
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {

Loading…
Cancel
Save