Skip to content

Commit

Permalink
Merge pull request #6 from nreese/more_tests2
Browse files Browse the repository at this point in the history
add more tests to ensure getRequestAggs functions as intented
  • Loading branch information
stacey-gammon authored Aug 22, 2017
2 parents 625ee73 + ace667c commit cd982e6
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 10 deletions.
182 changes: 175 additions & 7 deletions src/ui/public/agg_types/__tests__/buckets/_geo_hash.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
import expect from 'expect.js';
import { AggTypesBucketsGeoHashProvider } from 'ui/agg_types/buckets/geo_hash';

describe('Geohash Agg', function () {
describe('Geohash Agg', () => {

// Mock BucketAggType that does not create an AggType, but instead returns the AggType parameters
const intialZoom = 10;
const initialMapBounds = {
top_left: { lat: 1.0, lon: -1.0 },
bottom_right: { lat: -1.0, lon: 1.0 }
};
const aggMock = {
getField: () => {
return {
name: 'location'
};
},
params: {
isFilteredByCollar: true,
useGeocentroid: true
},
vis: {
hasUiState: () => {
return false;
},
params: {
mapZoom: intialZoom
},
sessionState: {}
},
type: 'geohash_grid',
};
const BucketAggTypeMock = (aggOptions) => {
return aggOptions.params;
return aggOptions;
};
const AggConfigMock = (vis, aggOptions) => {
return aggOptions;
};
const PrivateMock = (provider) => {
switch (provider.name) {
case 'AggTypesBucketsBucketAggTypeProvider':
return BucketAggTypeMock;
break;
case 'VisAggConfigProvider':
return AggConfigMock;
break;
default:
return () => {};
}
Expand All @@ -21,18 +52,54 @@ describe('Geohash Agg', function () {
return 7;//"visualization:tileMap:maxPrecision"
}
};
const params = AggTypesBucketsGeoHashProvider(PrivateMock, configMock); // eslint-disable-line new-cap

describe('precision parameter', function () {
function initVisSessionState() {
aggMock.vis.sessionState = {
mapBounds: initialMapBounds
};
}

function initAggParams() {
aggMock.params.isFilteredByCollar = true;
aggMock.params.useGeocentroid = true;
}

function zoomMap(zoomChange) {
aggMock.vis.params.mapZoom += zoomChange;
}

function moveMap(newBounds) {
aggMock.vis.sessionState.mapBounds = newBounds;
}

function resetMap() {
aggMock.vis.params.mapZoom = intialZoom;
aggMock.vis.sessionState.mapBounds = initialMapBounds;
aggMock.vis.sessionState.mapCollar = {
top_left: { lat: 1.5, lon: -1.5 },
bottom_right: { lat: -1.5, lon: 1.5 },
zoom: intialZoom
};
}

let geohashAgg;
beforeEach(() => {
geohashAgg = AggTypesBucketsGeoHashProvider(PrivateMock, configMock); // eslint-disable-line new-cap
});

describe('precision parameter', () => {

const PRECISION_PARAM_INDEX = 6;
const precisionParam = params[PRECISION_PARAM_INDEX];
let precisionParam;
beforeEach(() => {
precisionParam = geohashAgg.params[PRECISION_PARAM_INDEX];
});

it('should select precision parameter', () => {
expect(precisionParam.name).to.equal('precision');
});

describe('precision parameter write', function () {
describe('precision parameter write', () => {

const zoomToGeoHashPrecision = {
0: 1,
Expand Down Expand Up @@ -77,4 +144,105 @@ describe('Geohash Agg', function () {
});

});

describe('getRequestAggs', () => {

describe('initial aggregation creation', () => {
let requestAggs;
beforeEach(() => {
initVisSessionState();
initAggParams();
requestAggs = geohashAgg.getRequestAggs(aggMock);
});

it('should create filter, geohash_grid, and geo_centroid aggregations', () => {
expect(requestAggs.length).to.equal(3);
expect(requestAggs[0].type).to.equal('filter');
expect(requestAggs[1].type).to.equal('geohash_grid');
expect(requestAggs[2].type).to.equal('geo_centroid');
});

it('should set mapCollar in vis session state', () => {
expect(aggMock.vis.sessionState).to.have.property('mapCollar');
expect(aggMock.vis.sessionState.mapCollar).to.have.property('top_left');
expect(aggMock.vis.sessionState.mapCollar).to.have.property('bottom_right');
expect(aggMock.vis.sessionState.mapCollar).to.have.property('zoom');
});
});

describe('aggregation options', () => {

beforeEach(() => {
initVisSessionState();
initAggParams();
});

it('should only create geohash_grid and geo_centroid aggregations when isFilteredByCollar is false', () => {
aggMock.params.isFilteredByCollar = false;
const requestAggs = geohashAgg.getRequestAggs(aggMock);
expect(requestAggs.length).to.equal(2);
expect(requestAggs[0].type).to.equal('geohash_grid');
expect(requestAggs[1].type).to.equal('geo_centroid');
});

it('should only create filter and geohash_grid aggregations when useGeocentroid is false', () => {
aggMock.params.useGeocentroid = false;
const requestAggs = geohashAgg.getRequestAggs(aggMock);
expect(requestAggs.length).to.equal(2);
expect(requestAggs[0].type).to.equal('filter');
expect(requestAggs[1].type).to.equal('geohash_grid');

});
});

describe('aggregation creation after map interaction', () => {

let origRequestAggs;
let origMapCollar;
beforeEach(() => {
resetMap();
initAggParams();
origRequestAggs = geohashAgg.getRequestAggs(aggMock);
origMapCollar = aggMock.vis.sessionState.mapCollar;
});

it('should not change geo_bounding_box filter aggregation and vis session state when map movement is within map collar', () => {
moveMap({
top_left: { lat: 1.1, lon: -1.1 },
bottom_right: { lat: -0.9, lon: 0.9 }
});

const newRequestAggs = geohashAgg.getRequestAggs(aggMock);
expect(JSON.stringify(origRequestAggs[0].params, null, '')).to.equal(JSON.stringify(newRequestAggs[0].params, null, ''));

const newMapCollar = aggMock.vis.sessionState.mapCollar;
expect(JSON.stringify(origMapCollar, null, '')).to.equal(JSON.stringify(newMapCollar, null, ''));
});

it('should change geo_bounding_box filter aggregation and vis session state when map movement is outside map collar', () => {
moveMap({
top_left: { lat: 10.0, lon: -10.0 },
bottom_right: { lat: 9.0, lon: -9.0 }
});

const newRequestAggs = geohashAgg.getRequestAggs(aggMock);
expect(JSON.stringify(origRequestAggs[0].params, null, '')).not.to.equal(JSON.stringify(newRequestAggs[0].params, null, ''));

const newMapCollar = aggMock.vis.sessionState.mapCollar;
expect(JSON.stringify(origMapCollar, null, '')).not.to.equal(JSON.stringify(newMapCollar, null, ''));
});

it('should change geo_bounding_box filter aggregation and vis session state when map zoom level changes', () => {
zoomMap(-1);

const newRequestAggs = geohashAgg.getRequestAggs(aggMock);
expect(JSON.stringify(origRequestAggs[0].params, null, '')).not.to.equal(JSON.stringify(newRequestAggs[0].params, null, ''));

const newMapCollar = aggMock.vis.sessionState.mapCollar;
expect(JSON.stringify(origMapCollar, null, '')).not.to.equal(JSON.stringify(newMapCollar, null, ''));
});

});

});
});
9 changes: 6 additions & 3 deletions src/ui/public/agg_types/buckets/geo_hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ export function AggTypesBucketsGeoHashProvider(Private, config) {
} else {
mapCollar = lastMapCollar;
}
const boundingBox = {};
delete mapCollar.zoom; // zoom is not part of bounding box filter
boundingBox[agg.getField().name] = mapCollar;
vis.sessionState.mapCollar = mapCollar;

const boundingBox = {};
boundingBox[agg.getField().name] = {
top_left: mapCollar.top_left,
bottom_right: mapCollar.bottom_right
};
aggs.push(new AggConfig(agg.vis, {
type: 'filter',
id: 'filter_agg',
Expand Down

0 comments on commit cd982e6

Please sign in to comment.