boxplotOperator.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 { QueryObject, SqlaFormData, VizType } from '@superset-ui/core';
  20. import { boxplotOperator } from '@superset-ui/chart-controls';
  21. const formData: SqlaFormData = {
  22. metrics: [
  23. 'count(*)',
  24. { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
  25. ],
  26. time_range: '2015 : 2016',
  27. time_grain_sqla: 'P1Y',
  28. datasource: 'foo',
  29. viz_type: VizType.Table,
  30. };
  31. const queryObject: QueryObject = {
  32. metrics: [
  33. 'count(*)',
  34. { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
  35. ],
  36. time_range: '2015 : 2016',
  37. granularity: 'P1Y',
  38. };
  39. test('should skip boxplotOperator', () => {
  40. expect(boxplotOperator(formData, queryObject)).toEqual(undefined);
  41. });
  42. test('should do tukey boxplot', () => {
  43. expect(
  44. boxplotOperator(
  45. {
  46. ...formData,
  47. whiskerOptions: 'Tukey',
  48. },
  49. queryObject,
  50. ),
  51. ).toEqual({
  52. operation: 'boxplot',
  53. options: {
  54. whisker_type: 'tukey',
  55. percentiles: undefined,
  56. groupby: [],
  57. metrics: ['count(*)', 'sum(val)'],
  58. },
  59. });
  60. });
  61. test('should do min/max boxplot', () => {
  62. expect(
  63. boxplotOperator(
  64. {
  65. ...formData,
  66. whiskerOptions: 'Min/max (no outliers)',
  67. },
  68. queryObject,
  69. ),
  70. ).toEqual({
  71. operation: 'boxplot',
  72. options: {
  73. whisker_type: 'min/max',
  74. percentiles: undefined,
  75. groupby: [],
  76. metrics: ['count(*)', 'sum(val)'],
  77. },
  78. });
  79. });
  80. test('should do percentile boxplot', () => {
  81. expect(
  82. boxplotOperator(
  83. {
  84. ...formData,
  85. whiskerOptions: '1/4 percentiles',
  86. },
  87. queryObject,
  88. ),
  89. ).toEqual({
  90. operation: 'boxplot',
  91. options: {
  92. whisker_type: 'percentile',
  93. percentiles: [1, 4],
  94. groupby: [],
  95. metrics: ['count(*)', 'sum(val)'],
  96. },
  97. });
  98. });
  99. test('should throw an error', () => {
  100. expect(() =>
  101. boxplotOperator(
  102. {
  103. ...formData,
  104. whiskerOptions: 'foobar',
  105. },
  106. queryObject,
  107. ),
  108. ).toThrow('Unsupported whisker type: foobar');
  109. });