extractExtraMetrics.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { QueryFormData, QueryFormMetric, VizType } from '@superset-ui/core';
  20. import { extractExtraMetrics } from '@superset-ui/chart-controls';
  21. const baseFormData: QueryFormData = {
  22. datasource: 'dummy',
  23. viz_type: VizType.Table,
  24. metrics: ['a', 'b'],
  25. columns: ['foo', 'bar'],
  26. limit: 100,
  27. metrics_b: ['c', 'd'],
  28. columns_b: ['hello', 'world'],
  29. limit_b: 200,
  30. };
  31. const metric: QueryFormMetric = {
  32. expressionType: 'SQL',
  33. sqlExpression: 'case when 1 then 1 else 2 end',
  34. label: 'foo',
  35. };
  36. test('returns empty array if relevant controls missing', () => {
  37. expect(
  38. extractExtraMetrics({
  39. ...baseFormData,
  40. }),
  41. ).toEqual([]);
  42. });
  43. test('returns empty array if x_axis_sort is not same as timeseries_limit_metric', () => {
  44. expect(
  45. extractExtraMetrics({
  46. ...baseFormData,
  47. timeseries_limit_metric: 'foo',
  48. x_axis_sort: 'bar',
  49. }),
  50. ).toEqual([]);
  51. });
  52. test('returns correct column if sort columns match', () => {
  53. expect(
  54. extractExtraMetrics({
  55. ...baseFormData,
  56. timeseries_limit_metric: 'foo',
  57. x_axis_sort: 'foo',
  58. }),
  59. ).toEqual(['foo']);
  60. });
  61. test('handles adhoc metrics correctly', () => {
  62. expect(
  63. extractExtraMetrics({
  64. ...baseFormData,
  65. timeseries_limit_metric: metric,
  66. x_axis_sort: 'foo',
  67. }),
  68. ).toEqual([metric]);
  69. expect(
  70. extractExtraMetrics({
  71. ...baseFormData,
  72. timeseries_limit_metric: metric,
  73. x_axis_sort: 'bar',
  74. }),
  75. ).toEqual([]);
  76. });
  77. test('returns empty array if groupby populated', () => {
  78. expect(
  79. extractExtraMetrics({
  80. ...baseFormData,
  81. groupby: ['bar'],
  82. timeseries_limit_metric: 'foo',
  83. x_axis_sort: 'foo',
  84. }),
  85. ).toEqual([]);
  86. });
  87. test('returns empty array if timeseries_limit_metric and x_axis_sort are included in main metrics array', () => {
  88. expect(
  89. extractExtraMetrics({
  90. ...baseFormData,
  91. timeseries_limit_metric: 'a',
  92. x_axis_sort: 'a',
  93. }),
  94. ).toEqual([]);
  95. });
  96. test('returns empty array if timeseries_limit_metric and x_axis_sort are included in main metrics array with adhoc metrics', () => {
  97. expect(
  98. extractExtraMetrics({
  99. ...baseFormData,
  100. metrics: [
  101. 'a',
  102. {
  103. expressionType: 'SIMPLE',
  104. aggregate: 'SUM',
  105. column: { column_name: 'num' },
  106. },
  107. ],
  108. timeseries_limit_metric: {
  109. expressionType: 'SIMPLE',
  110. aggregate: 'SUM',
  111. column: { column_name: 'num' },
  112. },
  113. x_axis_sort: 'SUM(num)',
  114. }),
  115. ).toEqual([]);
  116. });
  117. test('returns empty array if timeseries_limit_metric is an empty array', () => {
  118. expect(
  119. extractExtraMetrics({
  120. ...baseFormData,
  121. // @ts-ignore
  122. timeseries_limit_metric: [],
  123. }),
  124. ).toEqual([]);
  125. });