fieldLookup.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { FieldMatcherID, fieldMatchers, FieldType } from '@grafana/data';
  2. import { toDataFrame } from '@grafana/data/src/dataframe/processDataFrame';
  3. import { DataTransformerID } from '@grafana/data/src/transformations/transformers/ids';
  4. import { frameAsGazetter } from 'app/features/geo/gazetteer/gazetteer';
  5. import { addFieldsFromGazetteer } from './fieldLookup';
  6. describe('Lookup gazetteer', () => {
  7. it('adds lat/lon based on string field', async () => {
  8. const cfg = {
  9. id: DataTransformerID.fieldLookup,
  10. options: {
  11. lookupField: 'location',
  12. gazetteer: 'public/gazetteer/usa-states.json',
  13. },
  14. };
  15. const data = toDataFrame({
  16. name: 'locations',
  17. fields: [
  18. { name: 'location', type: FieldType.string, values: ['AL', 'AK', 'Arizona', 'Arkansas', 'Somewhere'] },
  19. { name: 'values', type: FieldType.number, values: [0, 10, 5, 1, 5] },
  20. ],
  21. });
  22. const matcher = fieldMatchers.get(FieldMatcherID.byName).get(cfg.options?.lookupField);
  23. const frame = toDataFrame({
  24. fields: [
  25. { name: 'id', values: ['AL', 'AK', 'AZ'] },
  26. { name: 'name', values: ['Alabama', 'Arkansas', 'Arizona'] },
  27. { name: 'lng', values: [-80.891064, -100.891064, -111.891064] },
  28. { name: 'lat', values: [12.448457, 24.448457, 33.448457] },
  29. ],
  30. });
  31. const gaz = frameAsGazetter(frame, { path: 'path/to/gaz.json' });
  32. const out = await addFieldsFromGazetteer([data], gaz, matcher)[0];
  33. expect(out.fields).toMatchInlineSnapshot(`
  34. Array [
  35. Object {
  36. "config": Object {},
  37. "name": "location",
  38. "type": "string",
  39. "values": Array [
  40. "AL",
  41. "AK",
  42. "Arizona",
  43. "Arkansas",
  44. "Somewhere",
  45. ],
  46. },
  47. Object {
  48. "config": Object {},
  49. "name": "id",
  50. "type": "string",
  51. "values": Array [
  52. "AL",
  53. "AK",
  54. ,
  55. ,
  56. ,
  57. ],
  58. },
  59. Object {
  60. "config": Object {},
  61. "name": "name",
  62. "type": "string",
  63. "values": Array [
  64. "Alabama",
  65. "Arkansas",
  66. ,
  67. ,
  68. ,
  69. ],
  70. },
  71. Object {
  72. "config": Object {},
  73. "name": "lng",
  74. "type": "number",
  75. "values": Array [
  76. -80.891064,
  77. -100.891064,
  78. ,
  79. ,
  80. ,
  81. ],
  82. },
  83. Object {
  84. "config": Object {},
  85. "name": "lat",
  86. "type": "number",
  87. "values": Array [
  88. 12.448457,
  89. 24.448457,
  90. ,
  91. ,
  92. ,
  93. ],
  94. },
  95. Object {
  96. "config": Object {},
  97. "name": "values",
  98. "state": Object {
  99. "displayName": "values",
  100. "multipleFrames": false,
  101. },
  102. "type": "number",
  103. "values": Array [
  104. 0,
  105. 10,
  106. 5,
  107. 1,
  108. 5,
  109. ],
  110. },
  111. ]
  112. `);
  113. });
  114. });