operationUtils.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { createAggregationOperation, createAggregationOperationWithParam } from './operationUtils';
  2. describe('createAggregationOperation', () => {
  3. it('returns correct aggregation definitions with overrides', () => {
  4. expect(createAggregationOperation('test_aggregation', { category: 'test_category' })).toMatchObject([
  5. {
  6. addOperationHandler: {},
  7. alternativesKey: 'plain aggregations',
  8. category: 'test_category',
  9. defaultParams: [],
  10. explainHandler: {},
  11. id: 'test_aggregation',
  12. name: 'Test aggregation',
  13. paramChangedHandler: {},
  14. params: [
  15. {
  16. name: 'By label',
  17. optional: true,
  18. restParam: true,
  19. type: 'string',
  20. },
  21. ],
  22. renderer: {},
  23. },
  24. {
  25. alternativesKey: 'aggregations by',
  26. category: 'test_category',
  27. defaultParams: [''],
  28. explainHandler: {},
  29. hideFromList: true,
  30. id: '__test_aggregation_by',
  31. name: 'Test aggregation by',
  32. paramChangedHandler: {},
  33. params: [
  34. {
  35. editor: {},
  36. name: 'Label',
  37. optional: true,
  38. restParam: true,
  39. type: 'string',
  40. },
  41. ],
  42. renderer: {},
  43. },
  44. {
  45. alternativesKey: 'aggregations by',
  46. category: 'test_category',
  47. defaultParams: [''],
  48. explainHandler: {},
  49. hideFromList: true,
  50. id: '__test_aggregation_without',
  51. name: 'Test aggregation without',
  52. paramChangedHandler: {},
  53. params: [
  54. {
  55. name: 'Label',
  56. optional: true,
  57. restParam: true,
  58. type: 'string',
  59. },
  60. ],
  61. renderer: {},
  62. },
  63. ]);
  64. });
  65. });
  66. describe('createAggregationOperationWithParams', () => {
  67. it('returns correct aggregation definitions with overrides and params', () => {
  68. expect(
  69. createAggregationOperationWithParam(
  70. 'test_aggregation',
  71. {
  72. params: [{ name: 'K-value', type: 'number' }],
  73. defaultParams: [5],
  74. },
  75. { category: 'test_category' }
  76. )
  77. ).toMatchObject([
  78. {
  79. addOperationHandler: {},
  80. alternativesKey: 'plain aggregations',
  81. category: 'test_category',
  82. defaultParams: [5],
  83. explainHandler: {},
  84. id: 'test_aggregation',
  85. name: 'Test aggregation',
  86. paramChangedHandler: {},
  87. params: [
  88. { name: 'K-value', type: 'number' },
  89. { name: 'By label', optional: true, restParam: true, type: 'string' },
  90. ],
  91. renderer: {},
  92. },
  93. {
  94. alternativesKey: 'aggregations by',
  95. category: 'test_category',
  96. defaultParams: [5, ''],
  97. explainHandler: {},
  98. hideFromList: true,
  99. id: '__test_aggregation_by',
  100. name: 'Test aggregation by',
  101. paramChangedHandler: {},
  102. params: [
  103. { name: 'K-value', type: 'number' },
  104. { editor: {}, name: 'Label', optional: true, restParam: true, type: 'string' },
  105. ],
  106. renderer: {},
  107. },
  108. {
  109. alternativesKey: 'aggregations by',
  110. category: 'test_category',
  111. defaultParams: [5, ''],
  112. explainHandler: {},
  113. hideFromList: true,
  114. id: '__test_aggregation_without',
  115. name: 'Test aggregation without',
  116. paramChangedHandler: {},
  117. params: [
  118. { name: 'K-value', type: 'number' },
  119. { name: 'Label', optional: true, restParam: true, type: 'string' },
  120. ],
  121. renderer: {},
  122. },
  123. ]);
  124. });
  125. it('returns correct query string using aggregation definitions with overrides and number type param', () => {
  126. const def = createAggregationOperationWithParam(
  127. 'test_aggregation',
  128. {
  129. params: [{ name: 'K-value', type: 'number' }],
  130. defaultParams: [5],
  131. },
  132. { category: 'test_category' }
  133. );
  134. const topKByDefinition = def[1];
  135. expect(
  136. topKByDefinition.renderer(
  137. { id: '__topk_by', params: ['5', 'source', 'place'] },
  138. def[1],
  139. 'rate({place="luna"} |= `` [5m])'
  140. )
  141. ).toBe('test_aggregation by(source, place) (5, rate({place="luna"} |= `` [5m]))');
  142. });
  143. it('returns correct query string using aggregation definitions with overrides and string type param', () => {
  144. const def = createAggregationOperationWithParam(
  145. 'test_aggregation',
  146. {
  147. params: [{ name: 'Identifier', type: 'string' }],
  148. defaultParams: ['count'],
  149. },
  150. { category: 'test_category' }
  151. );
  152. const countValueDefinition = def[1];
  153. expect(
  154. countValueDefinition.renderer(
  155. { id: 'count_values', params: ['5', 'source', 'place'] },
  156. def[1],
  157. 'rate({place="luna"} |= `` [5m])'
  158. )
  159. ).toBe('test_aggregation by(source, place) ("5", rate({place="luna"} |= `` [5m]))');
  160. });
  161. });