|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { migrations, RawLensSavedXYObject770 } from './migrations'; |
| 8 | +import { SimpleSavedObject } from 'src/core/public'; |
| 9 | + |
| 10 | +describe('Lens migrations', () => { |
| 11 | + describe('7.7.0 missing dimensions in XY', () => { |
| 12 | + const migrate = (doc: SimpleSavedObject | RawLensSavedXYObject770) => migrations['7.7.0'](doc); |
| 13 | + |
| 14 | + const example: RawLensSavedXYObject770 = { |
| 15 | + type: 'lens', |
| 16 | + attributes: { |
| 17 | + expression: |
| 18 | + 'kibana\n| kibana_context query="{\\"language\\":\\"kuery\\",\\"query\\":\\"\\"}" \n| lens_merge_tables layerIds="c61a8afb-a185-4fae-a064-fb3846f6c451" \n tables={esaggs index="logstash-*" metricsAtAllLevels=false partialRows=false includeFormatHints=true aggConfigs="[{\\"id\\":\\"2cd09808-3915-49f4-b3b0-82767eba23f7\\",\\"enabled\\":true,\\"type\\":\\"max\\",\\"schema\\":\\"metric\\",\\"params\\":{\\"field\\":\\"bytes\\"}}]" | lens_rename_columns idMap="{\\"col-0-2cd09808-3915-49f4-b3b0-82767eba23f7\\":\\"2cd09808-3915-49f4-b3b0-82767eba23f7\\"}"}\n| lens_metric_chart title="Maximum of bytes" accessor="2cd09808-3915-49f4-b3b0-82767eba23f7"', |
| 19 | + state: { |
| 20 | + datasourceMetaData: { |
| 21 | + filterableIndexPatterns: [ |
| 22 | + { |
| 23 | + id: 'logstash-*', |
| 24 | + title: 'logstash-*', |
| 25 | + }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + datasourceStates: { |
| 29 | + indexpattern: { |
| 30 | + currentIndexPatternId: 'logstash-*', |
| 31 | + layers: { |
| 32 | + 'c61a8afb-a185-4fae-a064-fb3846f6c451': { |
| 33 | + columnOrder: ['2cd09808-3915-49f4-b3b0-82767eba23f7'], |
| 34 | + columns: { |
| 35 | + '2cd09808-3915-49f4-b3b0-82767eba23f7': { |
| 36 | + dataType: 'number', |
| 37 | + isBucketed: false, |
| 38 | + label: 'Maximum of bytes', |
| 39 | + operationType: 'max', |
| 40 | + scale: 'ratio', |
| 41 | + sourceField: 'bytes', |
| 42 | + }, |
| 43 | + 'd3e62a7a-c259-4fff-a2fc-eebf20b7008a': { |
| 44 | + dataType: 'number', |
| 45 | + isBucketed: false, |
| 46 | + label: 'Minimum of bytes', |
| 47 | + operationType: 'min', |
| 48 | + scale: 'ratio', |
| 49 | + sourceField: 'bytes', |
| 50 | + }, |
| 51 | + 'd6e40cea-6299-43b4-9c9d-b4ee305a2ce8': { |
| 52 | + dataType: 'date', |
| 53 | + isBucketed: true, |
| 54 | + label: 'Date Histogram of @timestamp', |
| 55 | + operationType: 'date_histogram', |
| 56 | + params: { |
| 57 | + interval: 'auto', |
| 58 | + }, |
| 59 | + scale: 'interval', |
| 60 | + sourceField: '@timestamp', |
| 61 | + }, |
| 62 | + }, |
| 63 | + indexPatternId: 'logstash-*', |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + filters: [], |
| 69 | + query: { |
| 70 | + language: 'kuery', |
| 71 | + query: '', |
| 72 | + }, |
| 73 | + visualization: { |
| 74 | + accessor: '2cd09808-3915-49f4-b3b0-82767eba23f7', |
| 75 | + isHorizontal: false, |
| 76 | + layerId: 'c61a8afb-a185-4fae-a064-fb3846f6c451', |
| 77 | + layers: [ |
| 78 | + { |
| 79 | + accessors: [ |
| 80 | + 'd3e62a7a-c259-4fff-a2fc-eebf20b7008a', |
| 81 | + '26ef70a9-c837-444c-886e-6bd905ee7335', |
| 82 | + ], |
| 83 | + layerId: 'c61a8afb-a185-4fae-a064-fb3846f6c451', |
| 84 | + seriesType: 'area', |
| 85 | + splitAccessor: '54cd64ed-2a44-4591-af84-b2624504569a', |
| 86 | + xAccessor: 'd6e40cea-6299-43b4-9c9d-b4ee305a2ce8', |
| 87 | + }, |
| 88 | + ], |
| 89 | + legend: { |
| 90 | + isVisible: true, |
| 91 | + position: 'right', |
| 92 | + }, |
| 93 | + preferredSeriesType: 'area', |
| 94 | + }, |
| 95 | + }, |
| 96 | + title: 'Artistpreviouslyknownaslens', |
| 97 | + visualizationType: 'lnsXY', |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + it('should not change anything by XY visualizations', () => { |
| 102 | + const target = { |
| 103 | + ...example, |
| 104 | + attributes: { |
| 105 | + ...example.attributes, |
| 106 | + visualizationType: 'lnsMetric', |
| 107 | + }, |
| 108 | + }; |
| 109 | + const result = migrate(target as SimpleSavedObject); |
| 110 | + expect(result).toEqual(target); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle missing layers', () => { |
| 114 | + const result = migrate({ |
| 115 | + ...example, |
| 116 | + attributes: { |
| 117 | + ...example.attributes, |
| 118 | + state: { |
| 119 | + ...example.attributes.state, |
| 120 | + datasourceStates: { |
| 121 | + indexpattern: { |
| 122 | + layers: [], |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } as SimpleSavedObject) as RawLensSavedXYObject770; |
| 128 | + |
| 129 | + expect(result.attributes.state.visualization.layers).toEqual([ |
| 130 | + { |
| 131 | + layerId: 'c61a8afb-a185-4fae-a064-fb3846f6c451', |
| 132 | + seriesType: 'area', |
| 133 | + // Removed split accessor |
| 134 | + splitAccessor: undefined, |
| 135 | + xAccessor: undefined, |
| 136 | + // Removed a yAcccessor |
| 137 | + accessors: [], |
| 138 | + }, |
| 139 | + ]); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should remove only missing accessors', () => { |
| 143 | + const result = migrate(example) as RawLensSavedXYObject770; |
| 144 | + |
| 145 | + expect(result.attributes.state.visualization.layers).toEqual([ |
| 146 | + { |
| 147 | + layerId: 'c61a8afb-a185-4fae-a064-fb3846f6c451', |
| 148 | + seriesType: 'area', |
| 149 | + xAccessor: 'd6e40cea-6299-43b4-9c9d-b4ee305a2ce8', |
| 150 | + // Removed split accessor |
| 151 | + splitAccessor: undefined, |
| 152 | + // Removed a yAcccessor |
| 153 | + accessors: ['d3e62a7a-c259-4fff-a2fc-eebf20b7008a'], |
| 154 | + }, |
| 155 | + ]); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments