Browse Source

updated code+tests to findById name array output format

pull/932/head
Stephen Hess 7 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 service = require('../service/language');
const _ = require('lodash');
/**
example response from language web service:
@ -28,7 +29,6 @@ example response from language web service:
**/
function setup() {
var transport = service.findById();
var middleware = function(req, res, next) {
@ -145,18 +145,18 @@ function updateDocs( req, res, translations ){
if( !translations[id].hasOwnProperty( 'names' ) ){ continue; }
// 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 );
continue;
}
// 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
// the 'name.default' property.
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'),
tmp = require('tmp'),
setup = require('../../../middleware/changeLanguage');
const proxyquire = require('proxyquire').noCallThru();
// load middleware using the default pelias config
var load = function(){
@ -144,7 +145,7 @@ module.exports.tests.hit = function(test, common) {
name: { default: 'London' },
parent: {
locality_id: [ 101735809 ],
locaity: [ 'London' ]
locality: [ 'London' ]
}
}
]};
@ -158,23 +159,23 @@ module.exports.tests.hit = function(test, common) {
cb( null, {
'101750367': {
'names': {
'default':'London',
'chi':'倫敦',
'spa':'Londres',
'eng':'London',
'hin':'लदन',
'ara':'لندن',
'por':'Londres',
'ben':'লনডন',
'rus':'Лондон',
'jpn':'ロンドン',
'kor':'런던'
'default':['London'],
'chi':['倫敦'],
'spa':['Londres'],
'eng':['London'],
'hin':['लदन'],
'ara':['لندن'],
'por':['Londres'],
'ben':['লনডন'],
'rus':['Лондон'],
'jpn':['ロンドン'],
'kor':['런던']
}
},
'101735809': {
'names':{
'default':'London',
'eng':'London'
'default':['London'],
'eng':['London']
}
}
});
@ -195,13 +196,66 @@ module.exports.tests.hit = function(test, common) {
name: { default: 'London' },
parent: {
locality_id: [ 101735809 ],
locaity: [ 'London' ]
locality: [ 'London' ]
}
}
]});
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) {

Loading…
Cancel
Save