query_part.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import queryPart from '../query_part';
  2. describe('InfluxQueryPart', () => {
  3. describe('series with measurement only', () => {
  4. it('should handle nested function parts', () => {
  5. const part = queryPart.create({
  6. type: 'derivative',
  7. params: ['10s'],
  8. });
  9. expect(part.text).toBe('derivative(10s)');
  10. expect(part.render('mean(value)')).toBe('derivative(mean(value), 10s)');
  11. });
  12. it('should nest spread function', () => {
  13. const part = queryPart.create({
  14. type: 'spread',
  15. });
  16. expect(part.text).toBe('spread()');
  17. expect(part.render('value')).toBe('spread(value)');
  18. });
  19. it('should handle suffix parts', () => {
  20. const part = queryPart.create({
  21. type: 'math',
  22. params: ['/ 100'],
  23. });
  24. expect(part.text).toBe('math(/ 100)');
  25. expect(part.render('mean(value)')).toBe('mean(value) / 100');
  26. });
  27. it('should handle alias parts', () => {
  28. const part = queryPart.create({
  29. type: 'alias',
  30. params: ['test'],
  31. });
  32. expect(part.text).toBe('alias(test)');
  33. expect(part.render('mean(value)')).toBe('mean(value) AS "test"');
  34. });
  35. it('should nest distinct when count is selected', () => {
  36. const selectParts = [
  37. queryPart.create({
  38. type: 'field',
  39. category: queryPart.getCategories().Fields,
  40. }),
  41. queryPart.create({
  42. type: 'count',
  43. category: queryPart.getCategories().Aggregations,
  44. }),
  45. ];
  46. const partModel = queryPart.create({
  47. type: 'distinct',
  48. category: queryPart.getCategories().Aggregations,
  49. });
  50. queryPart.replaceAggregationAdd(selectParts, partModel);
  51. expect(selectParts[1].text).toBe('distinct()');
  52. expect(selectParts[2].text).toBe('count()');
  53. });
  54. it('should convert to count distinct when distinct is selected and count added', () => {
  55. const selectParts = [
  56. queryPart.create({
  57. type: 'field',
  58. category: queryPart.getCategories().Fields,
  59. }),
  60. queryPart.create({
  61. type: 'distinct',
  62. category: queryPart.getCategories().Aggregations,
  63. }),
  64. ];
  65. const partModel = queryPart.create({
  66. type: 'count',
  67. category: queryPart.getCategories().Aggregations,
  68. });
  69. queryPart.replaceAggregationAdd(selectParts, partModel);
  70. expect(selectParts[1].text).toBe('distinct()');
  71. expect(selectParts[2].text).toBe('count()');
  72. });
  73. it('should replace count distinct if an aggregation is selected', () => {
  74. const selectParts = [
  75. queryPart.create({
  76. type: 'field',
  77. category: queryPart.getCategories().Fields,
  78. }),
  79. queryPart.create({
  80. type: 'distinct',
  81. category: queryPart.getCategories().Aggregations,
  82. }),
  83. queryPart.create({
  84. type: 'count',
  85. category: queryPart.getCategories().Aggregations,
  86. }),
  87. ];
  88. const partModel = queryPart.create({
  89. type: 'mean',
  90. category: queryPart.getCategories().Selectors,
  91. });
  92. queryPart.replaceAggregationAdd(selectParts, partModel);
  93. expect(selectParts[1].text).toBe('mean()');
  94. expect(selectParts).toHaveLength(2);
  95. });
  96. it('should not allowed nested counts when count distinct is selected', () => {
  97. const selectParts = [
  98. queryPart.create({
  99. type: 'field',
  100. category: queryPart.getCategories().Fields,
  101. }),
  102. queryPart.create({
  103. type: 'distinct',
  104. category: queryPart.getCategories().Aggregations,
  105. }),
  106. queryPart.create({
  107. type: 'count',
  108. category: queryPart.getCategories().Aggregations,
  109. }),
  110. ];
  111. const partModel = queryPart.create({
  112. type: 'count',
  113. category: queryPart.getCategories().Aggregations,
  114. });
  115. queryPart.replaceAggregationAdd(selectParts, partModel);
  116. expect(selectParts[1].text).toBe('distinct()');
  117. expect(selectParts[2].text).toBe('count()');
  118. expect(selectParts).toHaveLength(3);
  119. });
  120. it('should not remove count distinct when distinct is added', () => {
  121. const selectParts = [
  122. queryPart.create({
  123. type: 'field',
  124. category: queryPart.getCategories().Fields,
  125. }),
  126. queryPart.create({
  127. type: 'distinct',
  128. category: queryPart.getCategories().Aggregations,
  129. }),
  130. queryPart.create({
  131. type: 'count',
  132. category: queryPart.getCategories().Aggregations,
  133. }),
  134. ];
  135. const partModel = queryPart.create({
  136. type: 'distinct',
  137. category: queryPart.getCategories().Aggregations,
  138. });
  139. queryPart.replaceAggregationAdd(selectParts, partModel);
  140. expect(selectParts[1].text).toBe('distinct()');
  141. expect(selectParts[2].text).toBe('count()');
  142. expect(selectParts).toHaveLength(3);
  143. });
  144. it('should remove distinct when sum aggregation is selected', () => {
  145. const selectParts = [
  146. queryPart.create({
  147. type: 'field',
  148. category: queryPart.getCategories().Fields,
  149. }),
  150. queryPart.create({
  151. type: 'distinct',
  152. category: queryPart.getCategories().Aggregations,
  153. }),
  154. ];
  155. const partModel = queryPart.create({
  156. type: 'sum',
  157. category: queryPart.getCategories().Aggregations,
  158. });
  159. queryPart.replaceAggregationAdd(selectParts, partModel);
  160. expect(selectParts[1].text).toBe('sum()');
  161. });
  162. });
  163. });