location.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { Point } from 'ol/geom';
  2. import { toLonLat } from 'ol/proj';
  3. import { toDataFrame, FieldType, FrameGeometrySourceMode } from '@grafana/data';
  4. import { getGeometryField, getLocationFields, getLocationMatchers } from './location';
  5. const longitude = [0, -74.1];
  6. const latitude = [0, 40.7];
  7. const geohash = ['9q94r', 'dr5rs'];
  8. const names = ['A', 'B'];
  9. describe('handle location parsing', () => {
  10. it('auto should find geohash field', async () => {
  11. const frame = toDataFrame({
  12. name: 'simple',
  13. fields: [
  14. { name: 'name', type: FieldType.string, values: names },
  15. { name: 'geohash', type: FieldType.number, values: geohash },
  16. ],
  17. });
  18. const matchers = await getLocationMatchers();
  19. const fields = getLocationFields(frame, matchers);
  20. expect(fields.mode).toEqual(FrameGeometrySourceMode.Geohash);
  21. expect(fields.geohash).toBeDefined();
  22. expect(fields.geohash?.name).toEqual('geohash');
  23. const info = getGeometryField(frame, matchers);
  24. expect(info.field!.type).toBe(FieldType.geo);
  25. expect(info.field!.values.toArray().map((p) => toLonLat((p as Point).getCoordinates()))).toMatchInlineSnapshot(`
  26. Array [
  27. Array [
  28. -122.01416015625001,
  29. 36.979980468750014,
  30. ],
  31. Array [
  32. -73.98193359375,
  33. 40.71533203125,
  34. ],
  35. ]
  36. `);
  37. });
  38. it('auto should find coordinate fields', async () => {
  39. const frame = toDataFrame({
  40. name: 'simple',
  41. fields: [
  42. { name: 'name', type: FieldType.string, values: names },
  43. { name: 'latitude', type: FieldType.number, values: latitude },
  44. { name: 'longitude', type: FieldType.number, values: longitude },
  45. ],
  46. });
  47. const matchers = await getLocationMatchers();
  48. const geo = getGeometryField(frame, matchers).field!;
  49. expect(geo.values.toArray().map((p) => toLonLat((p as Point).getCoordinates()))).toMatchInlineSnapshot(`
  50. Array [
  51. Array [
  52. 0,
  53. 0,
  54. ],
  55. Array [
  56. -74.1,
  57. 40.69999999999999,
  58. ],
  59. ]
  60. `);
  61. });
  62. });