dashboardMigrations.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { AnnotationQuery, DataQuery } from '@grafana/data';
  2. import { CloudWatchMetricsQuery, LegacyAnnotationQuery, MetricEditorMode, MetricQueryType } from '../types';
  3. import {
  4. migrateCloudWatchQuery,
  5. migrateMultipleStatsAnnotationQuery,
  6. migrateMultipleStatsMetricsQuery,
  7. } from './dashboardMigrations';
  8. describe('dashboardMigrations', () => {
  9. describe('migrateMultipleStatsMetricsQuery', () => {
  10. const queryToMigrate = {
  11. statistics: ['Average', 'Sum', 'Maximum'],
  12. refId: 'A',
  13. };
  14. const panelQueries: DataQuery[] = [
  15. { ...queryToMigrate },
  16. {
  17. refId: 'B',
  18. },
  19. ];
  20. const newQueries = migrateMultipleStatsMetricsQuery(queryToMigrate as CloudWatchMetricsQuery, panelQueries);
  21. const newMetricQueries = newQueries as CloudWatchMetricsQuery[];
  22. it('should create one new query for each stat', () => {
  23. expect(newQueries.length).toBe(2);
  24. });
  25. it('should assign new queries the right stats', () => {
  26. expect(newMetricQueries[0].statistic).toBe('Sum');
  27. expect(newMetricQueries[1].statistic).toBe('Maximum');
  28. });
  29. it('should assign new queries the right ref id', () => {
  30. expect(newQueries[0].refId).toBe('C');
  31. expect(newQueries[1].refId).toBe('D');
  32. });
  33. it('should not have statistics prop anymore', () => {
  34. expect(queryToMigrate).not.toHaveProperty('statistics');
  35. expect(newQueries[0]).not.toHaveProperty('statistics');
  36. expect(newQueries[1]).not.toHaveProperty('statistics');
  37. });
  38. });
  39. describe('migrateMultipleStatsMetricsQuery with only one stat', () => {
  40. const queryToMigrate = {
  41. statistics: ['Average'],
  42. refId: 'A',
  43. } as CloudWatchMetricsQuery;
  44. const panelQueries: DataQuery[] = [
  45. { ...queryToMigrate },
  46. {
  47. refId: 'B',
  48. },
  49. ];
  50. const newQueries = migrateMultipleStatsMetricsQuery(queryToMigrate as CloudWatchMetricsQuery, panelQueries);
  51. it('should not create any new queries', () => {
  52. expect(newQueries.length).toBe(0);
  53. });
  54. it('should have the right stats', () => {
  55. expect(queryToMigrate.statistic).toBe('Average');
  56. });
  57. it('should not have statistics prop anymore', () => {
  58. expect(queryToMigrate).not.toHaveProperty('statistics');
  59. });
  60. });
  61. describe('migrateMultipleStatsAnnotationQuery', () => {
  62. const annotationToMigrate: AnnotationQuery<LegacyAnnotationQuery> = {
  63. statistics: ['p23.23', 'SampleCount'],
  64. name: 'Test annotation',
  65. enable: false,
  66. iconColor: '',
  67. };
  68. const newAnnotations = migrateMultipleStatsAnnotationQuery(annotationToMigrate);
  69. const newCloudWatchAnnotations = newAnnotations;
  70. it('should create one new annotation for each stat', () => {
  71. expect(newAnnotations.length).toBe(1);
  72. });
  73. it('should assign new queries the right stats', () => {
  74. expect(newCloudWatchAnnotations[0].statistic).toBe('SampleCount');
  75. });
  76. it('should assign new queries the right ref id', () => {
  77. expect(newAnnotations[0].name).toBe('Test annotation - SampleCount');
  78. });
  79. it('should not have statistics prop anymore', () => {
  80. expect(newCloudWatchAnnotations[0]).not.toHaveProperty('statistics');
  81. });
  82. it('should migrate original query correctly', () => {
  83. expect(annotationToMigrate).not.toHaveProperty('statistics');
  84. expect(annotationToMigrate.name).toBe('Test annotation - p23.23');
  85. });
  86. describe('migrateMultipleStatsAnnotationQuery with only with stat', () => {
  87. const annotationToMigrate: AnnotationQuery<LegacyAnnotationQuery> = {
  88. statistics: ['p23.23'],
  89. name: 'Test annotation',
  90. enable: false,
  91. iconColor: '',
  92. };
  93. const newAnnotations = migrateMultipleStatsAnnotationQuery(annotationToMigrate);
  94. it('should not create new annotations', () => {
  95. expect(newAnnotations.length).toBe(0);
  96. });
  97. it('should not change the name', () => {
  98. expect(annotationToMigrate.name).toBe('Test annotation');
  99. });
  100. it('should use statistics prop and remove statistics prop', () => {
  101. expect('statistic' in annotationToMigrate && annotationToMigrate.statistic).toEqual('p23.23');
  102. expect(annotationToMigrate).not.toHaveProperty('statistics');
  103. });
  104. });
  105. describe('migrateCloudWatchQuery', () => {
  106. describe('and query doesnt have an expression', () => {
  107. const query: CloudWatchMetricsQuery = {
  108. statistic: 'Average',
  109. refId: 'A',
  110. id: '',
  111. region: '',
  112. namespace: '',
  113. period: '',
  114. alias: '',
  115. metricName: '',
  116. dimensions: {},
  117. matchExact: false,
  118. expression: '',
  119. };
  120. migrateCloudWatchQuery(query);
  121. it('should have basic metricEditorMode', () => {
  122. expect(query.metricQueryType).toBe(MetricQueryType.Search);
  123. });
  124. it('should have Builder BasicEditorMode', () => {
  125. expect(query.metricEditorMode).toBe(MetricEditorMode.Builder);
  126. });
  127. });
  128. describe('and query has an expression', () => {
  129. const query: CloudWatchMetricsQuery = {
  130. statistic: 'Average',
  131. refId: 'A',
  132. id: '',
  133. region: '',
  134. namespace: '',
  135. period: '',
  136. alias: '',
  137. metricName: '',
  138. dimensions: {},
  139. matchExact: false,
  140. expression: 'SUM(x)',
  141. };
  142. migrateCloudWatchQuery(query);
  143. migrateCloudWatchQuery(query);
  144. it('should have basic metricEditorMode', () => {
  145. expect(query.metricQueryType).toBe(MetricQueryType.Search);
  146. });
  147. it('should have Expression BasicEditorMode', () => {
  148. expect(query.metricEditorMode).toBe(MetricEditorMode.Code);
  149. });
  150. });
  151. });
  152. });
  153. });