From 96d63cc22e7bbe37173fa525069845f72100f6f9 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 22 Dec 2020 13:13:07 -0500 Subject: [PATCH] [Maps] Use Json for mvt-tests (#86492) (#86588) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- package.json | 2 + .../server/mvt/__tests__/pbf/0_0_0_docs.pbf | Bin 155 -> 0 bytes .../mvt/__tests__/pbf/0_0_0_grid_asgrid.pbf | Bin 91 -> 0 bytes .../mvt/__tests__/pbf/0_0_0_grid_aspoint.pbf | Bin 83 -> 0 bytes .../plugins/maps/server/mvt/get_tile.test.ts | 135 +++++++++++++++--- yarn.lock | 4 +- 6 files changed, 122 insertions(+), 19 deletions(-) delete mode 100644 x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_docs.pbf delete mode 100644 x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_grid_asgrid.pbf delete mode 100644 x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_grid_aspoint.pbf diff --git a/package.json b/package.json index bfb7d01e32fe..24caa1e051c2 100644 --- a/package.json +++ b/package.json @@ -374,6 +374,7 @@ "@mapbox/geojson-rewind": "^0.5.0", "@mapbox/mapbox-gl-draw": "^1.2.0", "@mapbox/mapbox-gl-rtl-text": "^0.2.3", + "@mapbox/vector-tile": "1.3.1", "@microsoft/api-documenter": "7.7.2", "@microsoft/api-extractor": "7.7.0", "@octokit/rest": "^16.35.0", @@ -740,6 +741,7 @@ "ora": "^4.0.4", "p-limit": "^3.0.1", "parse-link-header": "^1.0.1", + "pbf": "3.2.1", "pirates": "^4.0.1", "pixelmatch": "^5.1.0", "pkg-up": "^2.0.0", diff --git a/x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_docs.pbf b/x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_docs.pbf deleted file mode 100644 index 9a9296e2ece3f94ac726f6a83f2958ba2e22074b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 155 zcmb1|!C1k>#Z#PLT9lj`pOaXbTBOmSAfzP3#=yYH$iyVUtR%)cfww_Yse%1Hdjp%m z1GW`x?>R5<@Jq49XXd4(R!A|&XQoIA$H!+U<;BORr6!h?7Nr7(;^URrxL6AEb1Id@ lxJ2B|1A=@b0-e$;E2DHXOfw@hld_a#xuikzR@fx13;_42EcXBa diff --git a/x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_grid_asgrid.pbf b/x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_grid_asgrid.pbf deleted file mode 100644 index f2289865b80229bd3c96d44cfba54ef8aab2d16b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 91 zcmb1&tYG5eDb6n~N=}W>NvupQ(r8c+5*K1&U|?jFU{>PgY!GgckYaGKXJD}Bm*Pyx uPmWK{FU>2F;!i9~kIzqw5AhFi^oe&2Q)1ERJY diff --git a/x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_grid_aspoint.pbf b/x-pack/plugins/maps/server/mvt/__tests__/pbf/0_0_0_grid_aspoint.pbf deleted file mode 100644 index 54b0791ccd1361b7d26b2c689235c433e7f1c378..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 83 zcmb0NtYG5eDb6n~N=}W>NvupQ(r8c+;um6JU|?jFU{qq|d?VB*A;p=JpB$f@Uz%4U m#h+M~9-p5UAL1Y4=o9Z4ro^JrdzMLwQ*x{OUx}m{A&vm0mKV|h diff --git a/x-pack/plugins/maps/server/mvt/get_tile.test.ts b/x-pack/plugins/maps/server/mvt/get_tile.test.ts index 1e00fd27e3d1..3660039f2513 100644 --- a/x-pack/plugins/maps/server/mvt/get_tile.test.ts +++ b/x-pack/plugins/maps/server/mvt/get_tile.test.ts @@ -7,9 +7,26 @@ import { getGridTile, getTile } from './get_tile'; import { TILE_GRIDAGGS, TILE_SEARCHES } from './__tests__/tile_es_responses'; import { Logger } from 'src/core/server'; -import * as path from 'path'; -import * as fs from 'fs'; -import { ES_GEO_FIELD_TYPE, RENDER_AS } from '../../common/constants'; +import { ES_GEO_FIELD_TYPE, MVT_SOURCE_LAYER_NAME, RENDER_AS } from '../../common/constants'; + +// @ts-expect-error +import { VectorTile, VectorTileLayer } from '@mapbox/vector-tile'; +// @ts-expect-error +import Protobuf from 'pbf'; + +interface ITileLayerJsonExpectation { + version: number; + name: string; + extent: number; + length: number; + features: Array<{ + id: string | number | undefined; + type: number; + properties: object; + extent: number; + pointArrays: object; + }>; +} describe('getTile', () => { const mockCallElasticsearch = jest.fn(); @@ -39,7 +56,7 @@ describe('getTile', () => { } }); - const tile = await getTile({ + const pbfTile = await getTile({ x: 0, y: 0, z: 0, @@ -53,7 +70,35 @@ describe('getTile', () => { geoFieldType: ES_GEO_FIELD_TYPE.GEO_SHAPE, }); - compareTiles('./__tests__/pbf/0_0_0_docs.pbf', tile); + const jsonTile = new VectorTile(new Protobuf(pbfTile)); + compareJsonTiles(jsonTile, { + version: 2, + name: 'source_layer', + extent: 4096, + length: 1, + features: [ + { + id: undefined, + type: 3, + properties: { + __kbn__feature_id__: 'poly:G7PRMXQBgyyZ-h5iYibj:0', + _id: 'G7PRMXQBgyyZ-h5iYibj', + _index: 'poly', + }, + extent: 4096, + pointArrays: [ + [ + { x: 840, y: 1600 }, + { x: 1288, y: 1096 }, + { x: 1672, y: 1104 }, + { x: 2104, y: 1508 }, + { x: 1472, y: 2316 }, + { x: 840, y: 1600 }, + ], + ], + }, + ], + }); }); }); @@ -115,22 +160,78 @@ describe('getGridTile', () => { }; test('0.0.0 tile (clusters)', async () => { - const tile = await getGridTile(defaultParams); - compareTiles('./__tests__/pbf/0_0_0_grid_aspoint.pbf', tile); + const pbfTile = await getGridTile(defaultParams); + const jsonTile = new VectorTile(new Protobuf(pbfTile)); + compareJsonTiles(jsonTile, { + version: 2, + name: 'source_layer', + extent: 4096, + length: 1, + features: [ + { + id: undefined, + type: 1, + properties: { + ['avg_of_TOTAL_AV']: 5398920.390458991, + doc_count: 42637, + }, + extent: 4096, + pointArrays: [[{ x: 1206, y: 1539 }]], + }, + ], + }); }); test('0.0.0 tile (grids)', async () => { - const tile = await getGridTile({ ...defaultParams, requestType: RENDER_AS.GRID }); - compareTiles('./__tests__/pbf/0_0_0_grid_asgrid.pbf', tile); + const pbfTile = await getGridTile({ ...defaultParams, requestType: RENDER_AS.GRID }); + const jsonTile = new VectorTile(new Protobuf(pbfTile)); + compareJsonTiles(jsonTile, { + version: 2, + name: 'source_layer', + extent: 4096, + length: 1, + features: [ + { + id: undefined, + type: 3, + properties: { + ['avg_of_TOTAL_AV']: 5398920.390458991, + doc_count: 42637, + }, + extent: 4096, + pointArrays: [ + [ + { x: 1216, y: 1536 }, + { x: 1216, y: 1568 }, + { x: 1184, y: 1568 }, + { x: 1184, y: 1536 }, + { x: 1216, y: 1536 }, + ], + ], + }, + ], + }); }); }); -function compareTiles(expectedRelativePath: string, actualTile: Buffer | null) { - if (actualTile === null) { - throw new Error('Tile should be created'); - } - const expectedPath = path.resolve(__dirname, expectedRelativePath); - const expectedBin = fs.readFileSync(expectedPath, 'binary'); - const expectedTile = Buffer.from(expectedBin, 'binary'); - expect(expectedTile.equals(actualTile)).toBe(true); +/** + * Verifies JSON-representation of tile-contents + * @param actualTileJson + * @param expectedLayer + */ +function compareJsonTiles(actualTileJson: VectorTile, expectedLayer: ITileLayerJsonExpectation) { + const actualLayer: VectorTileLayer = actualTileJson.layers[MVT_SOURCE_LAYER_NAME]; + expect(actualLayer.version).toEqual(expectedLayer.version); + expect(actualLayer.extent).toEqual(expectedLayer.extent); + expect(actualLayer.name).toEqual(expectedLayer.name); + expect(actualLayer.length).toEqual(expectedLayer.features.length); + + expectedLayer.features.forEach((expectedFeature, index) => { + const actualFeature = actualLayer.feature(index); + expect(actualFeature.type).toEqual(expectedFeature.type); + expect(actualFeature.extent).toEqual(expectedFeature.extent); + expect(actualFeature.id).toEqual(expectedFeature.id); + expect(actualFeature.properties).toEqual(expectedFeature.properties); + expect(actualFeature.loadGeometry()).toEqual(expectedFeature.pointArrays); + }); } diff --git a/yarn.lock b/yarn.lock index 09e5959c464f..107a930f43b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2996,7 +2996,7 @@ resolved "https://registry.yarnpkg.com/@mapbox/unitbezier/-/unitbezier-0.0.0.tgz#15651bd553a67b8581fb398810c98ad86a34524e" integrity sha1-FWUb1VOme4WB+zmIEMmK2Go0Uk4= -"@mapbox/vector-tile@^1.3.1": +"@mapbox/vector-tile@1.3.1", "@mapbox/vector-tile@^1.3.1": version "1.3.1" resolved "https://registry.yarnpkg.com/@mapbox/vector-tile/-/vector-tile-1.3.1.tgz#d3a74c90402d06e89ec66de49ec817ff53409666" integrity sha512-MCEddb8u44/xfQ3oD+Srl/tNcQoqTw3goGk2oLsrFxOTc3dUp+kAnby3PvAeeBYSMSjSPD1nd1AJA6W49WnoUw== @@ -21984,7 +21984,7 @@ pathval@^1.1.0: resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0" integrity sha1-uULm1L3mUwBe9rcTYd74cn0GReA= -pbf@^3.0.5, pbf@^3.2.1: +pbf@3.2.1, pbf@^3.0.5, pbf@^3.2.1: version "3.2.1" resolved "https://registry.yarnpkg.com/pbf/-/pbf-3.2.1.tgz#b4c1b9e72af966cd82c6531691115cc0409ffe2a" integrity sha512-ClrV7pNOn7rtmoQVF4TS1vyU0WhYRnP92fzbfF75jAIwpnzdJXf8iTd4CMEqO4yUenH6NDqLiwjqlh6QgZzgLQ==