[Maps] Coerce term join values to string (#41503) (#41599)

This commit is contained in:
Thomas Neirynck 2019-07-19 16:27:15 -04:00 committed by GitHub
parent 51c0adb4ba
commit 6cef5daef0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 17 deletions

View file

@ -56,8 +56,9 @@ export class LeftInnerJoin {
delete feature.properties[stylePropertyName];
}
const joinKey = feature.properties[this._descriptor.leftField];
if (propertiesMap && propertiesMap.has(joinKey)) {
Object.assign(feature.properties, propertiesMap.get(joinKey));
const coercedKey = typeof joinKey === 'undefined' || joinKey === null ? null : joinKey.toString();
if (propertiesMap && coercedKey !== null && propertiesMap.has(coercedKey)) {
Object.assign(feature.properties, propertiesMap.get(coercedKey));
return true;
} else {
return false;

View file

@ -18,18 +18,21 @@ jest.mock('ui/timefilter/timefilter', () => {});
jest.mock('../vector_layer', () => {});
const rightSource = {
id: 'd3625663-5b34-4d50-a784-0d743f676a0c',
indexPatternId: '90943e30-9a47-11e8-b64d-95841ca0b247',
indexPatternTitle: 'kibana_sample_data_logs',
term: 'geo.dest',
};
const leftJoin = new LeftInnerJoin({
leftField: 'iso2',
right: {
id: 'd3625663-5b34-4d50-a784-0d743f676a0c',
indexPatternId: '90943e30-9a47-11e8-b64d-95841ca0b247',
indexPatternTitle: 'kibana_sample_data_logs',
term: 'geo.dest',
}
right: rightSource
});
const COUNT_PROPERTY_NAME = '__kbnjoin__count_groupby_kibana_sample_data_logs.geo.dest';
describe('joinPropertiesToFeature', () => {
const COUNT_PROPERTY_NAME = '__kbnjoin__count_groupby_kibana_sample_data_logs.geo.dest';
it('Should add join property to features in feature collection', () => {
const feature = {
@ -67,4 +70,73 @@ describe('joinPropertiesToFeature', () => {
iso2: 'CN',
});
});
it('Should coerce to string before joining', () => {
const leftJoin = new LeftInnerJoin({
leftField: 'zipcode',
right: rightSource
});
const feature = {
properties: {
zipcode: 40204
}
};
const propertiesMap = new Map();
propertiesMap.set('40204', { [COUNT_PROPERTY_NAME]: 61 });
leftJoin.joinPropertiesToFeature(feature, propertiesMap, [{
propertyKey: COUNT_PROPERTY_NAME
}]);
expect(feature.properties).toEqual({
zipcode: 40204,
[COUNT_PROPERTY_NAME]: 61,
});
});
it('Should handle undefined values', () => {
const feature = {//this feature does not have the iso2 field
properties: {
zipcode: 40204
}
};
const propertiesMap = new Map();
propertiesMap.set('40204', { [COUNT_PROPERTY_NAME]: 61 });
leftJoin.joinPropertiesToFeature(feature, propertiesMap, [{
propertyKey: COUNT_PROPERTY_NAME
}]);
expect(feature.properties).toEqual({
zipcode: 40204
});
});
it('Should handle falsy values', () => {
const leftJoin = new LeftInnerJoin({
leftField: 'code',
right: rightSource
});
const feature = {
properties: {
code: 0
}
}
;
const propertiesMap = new Map();
propertiesMap.set('0', { [COUNT_PROPERTY_NAME]: 61 });
leftJoin.joinPropertiesToFeature(feature, propertiesMap, [{
propertyKey: COUNT_PROPERTY_NAME
}]);
expect(feature.properties).toEqual({
code: 0,
[COUNT_PROPERTY_NAME]: 61
});
});
});

View file

@ -49,7 +49,7 @@ export function extractPropertiesMap(rawEsData, propertyNames, countPropertyName
properties[propertyName] = _.get(termBucket, [propertyName, 'value']);
}
});
propertiesMap.set(termBucket.key, properties);
propertiesMap.set((termBucket.key).toString(), properties);
});
return propertiesMap;
}

View file

@ -146,7 +146,7 @@ describe('_makeAggConfigs', () => {
});
describe('extractPropertiesMap', () => {
const resp = {
const responseWithNumberTypes = {
'aggregations': {
'join': {
'buckets': [
@ -173,29 +173,29 @@ describe('extractPropertiesMap', () => {
let propertiesMap;
beforeAll(() => {
propertiesMap = extractPropertiesMap(
resp,
responseWithNumberTypes,
[minPropName],
countPropName
);
});
it('should create key for each join term', () => {
expect(propertiesMap.has(109)).toBe(true);
expect(propertiesMap.has(62)).toBe(true);
expect(propertiesMap.has('109')).toBe(true);
expect(propertiesMap.has('62')).toBe(true);
});
it('should extract count property', () => {
const properties = propertiesMap.get(109);
const properties = propertiesMap.get('109');
expect(properties[countPropName]).toBe(1130);
});
it('should extract min property', () => {
const properties = propertiesMap.get(109);
const properties = propertiesMap.get('109');
expect(properties[minPropName]).toBe(36);
});
it('should extract property with value of "0"', () => {
const properties = propertiesMap.get(62);
const properties = propertiesMap.get('62');
expect(properties[minPropName]).toBe(0);
});
});