aggregateOperator.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { aggregationOperator } from '@superset-ui/chart-controls';
  21. describe('aggregationOperator', () => {
  22. const formData: SqlaFormData = {
  23. metrics: [
  24. 'count(*)',
  25. { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
  26. ],
  27. time_range: '2015 : 2016',
  28. granularity: 'month',
  29. datasource: 'foo',
  30. viz_type: VizType.Table,
  31. };
  32. const queryObject: QueryObject = {
  33. metrics: [
  34. 'count(*)',
  35. { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
  36. ],
  37. time_range: '2015 : 2016',
  38. granularity: 'month',
  39. };
  40. it('should return undefined for LAST_VALUE aggregation', () => {
  41. const formDataWithLastValue = {
  42. ...formData,
  43. aggregation: 'LAST_VALUE',
  44. };
  45. expect(
  46. aggregationOperator(formDataWithLastValue, queryObject),
  47. ).toBeUndefined();
  48. });
  49. it('should return undefined when metrics is empty', () => {
  50. const queryObjectWithoutMetrics = {
  51. ...queryObject,
  52. metrics: [],
  53. };
  54. const formDataWithSum = {
  55. ...formData,
  56. aggregation: 'sum',
  57. };
  58. expect(
  59. aggregationOperator(formDataWithSum, queryObjectWithoutMetrics),
  60. ).toBeUndefined();
  61. });
  62. it('should apply sum aggregation to all metrics', () => {
  63. const formDataWithSum = {
  64. ...formData,
  65. aggregation: 'sum',
  66. };
  67. expect(aggregationOperator(formDataWithSum, queryObject)).toEqual({
  68. operation: 'aggregate',
  69. options: {
  70. groupby: [],
  71. aggregates: {
  72. 'count(*)': {
  73. operator: 'sum',
  74. column: 'count(*)',
  75. },
  76. 'sum(val)': {
  77. operator: 'sum',
  78. column: 'sum(val)',
  79. },
  80. },
  81. },
  82. });
  83. });
  84. it('should apply mean aggregation to all metrics', () => {
  85. const formDataWithMean = {
  86. ...formData,
  87. aggregation: 'mean',
  88. };
  89. expect(aggregationOperator(formDataWithMean, queryObject)).toEqual({
  90. operation: 'aggregate',
  91. options: {
  92. groupby: [],
  93. aggregates: {
  94. 'count(*)': {
  95. operator: 'mean',
  96. column: 'count(*)',
  97. },
  98. 'sum(val)': {
  99. operator: 'mean',
  100. column: 'sum(val)',
  101. },
  102. },
  103. },
  104. });
  105. });
  106. it('should use default aggregation when not specified', () => {
  107. expect(aggregationOperator(formData, queryObject)).toBeUndefined();
  108. });
  109. });