timeComparePivotOperator.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 {
  21. timeCompareOperator,
  22. timeComparePivotOperator,
  23. } from '@superset-ui/chart-controls';
  24. const formData: SqlaFormData = {
  25. metrics: [
  26. 'count(*)',
  27. { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
  28. ],
  29. time_range: '2015 : 2016',
  30. granularity: 'month',
  31. datasource: 'foo',
  32. viz_type: VizType.Table,
  33. show_empty_columns: true,
  34. };
  35. const queryObject: QueryObject = {
  36. metrics: [
  37. 'count(*)',
  38. { label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
  39. ],
  40. columns: ['foo', 'bar'],
  41. time_range: '2015 : 2016',
  42. granularity: 'month',
  43. post_processing: [],
  44. };
  45. test('should skip pivot', () => {
  46. expect(timeComparePivotOperator(formData, queryObject)).toEqual(undefined);
  47. expect(
  48. timeComparePivotOperator({ ...formData, time_compare: [] }, queryObject),
  49. ).toEqual(undefined);
  50. expect(
  51. timeComparePivotOperator(
  52. { ...formData, comparison_type: null },
  53. queryObject,
  54. ),
  55. ).toEqual(undefined);
  56. expect(
  57. timeCompareOperator(
  58. { ...formData, comparison_type: 'foobar' },
  59. queryObject,
  60. ),
  61. ).toEqual(undefined);
  62. });
  63. test('should pivot on any type of timeCompare', () => {
  64. const anyTimeCompareTypes = ['values', 'difference', 'percentage', 'ratio'];
  65. anyTimeCompareTypes.forEach(cType => {
  66. expect(
  67. timeComparePivotOperator(
  68. {
  69. ...formData,
  70. comparison_type: cType,
  71. time_compare: ['1 year ago', '1 year later'],
  72. granularity_sqla: 'time_column',
  73. },
  74. {
  75. ...queryObject,
  76. },
  77. ),
  78. ).toEqual({
  79. operation: 'pivot',
  80. options: {
  81. aggregates: {
  82. 'count(*)': { operator: 'mean' },
  83. 'count(*)__1 year ago': { operator: 'mean' },
  84. 'count(*)__1 year later': { operator: 'mean' },
  85. 'sum(val)': { operator: 'mean' },
  86. 'sum(val)__1 year ago': {
  87. operator: 'mean',
  88. },
  89. 'sum(val)__1 year later': {
  90. operator: 'mean',
  91. },
  92. },
  93. drop_missing_columns: false,
  94. columns: ['foo', 'bar'],
  95. index: ['__timestamp'],
  96. },
  97. });
  98. });
  99. });
  100. test('should pivot on x-axis', () => {
  101. expect(
  102. timeComparePivotOperator(
  103. {
  104. ...formData,
  105. comparison_type: 'values',
  106. time_compare: ['1 year ago', '1 year later'],
  107. x_axis: 'ds',
  108. },
  109. queryObject,
  110. ),
  111. ).toEqual({
  112. operation: 'pivot',
  113. options: {
  114. aggregates: {
  115. 'count(*)': { operator: 'mean' },
  116. 'count(*)__1 year ago': { operator: 'mean' },
  117. 'count(*)__1 year later': { operator: 'mean' },
  118. 'sum(val)': {
  119. operator: 'mean',
  120. },
  121. 'sum(val)__1 year ago': {
  122. operator: 'mean',
  123. },
  124. 'sum(val)__1 year later': {
  125. operator: 'mean',
  126. },
  127. },
  128. drop_missing_columns: false,
  129. columns: ['foo', 'bar'],
  130. index: ['ds'],
  131. },
  132. });
  133. });
  134. test('should pivot on x-axis with series_columns', () => {
  135. expect(
  136. timeComparePivotOperator(
  137. {
  138. ...formData,
  139. comparison_type: 'values',
  140. time_compare: ['1 year ago', '1 year later'],
  141. x_axis: 'ds',
  142. },
  143. {
  144. ...queryObject,
  145. columns: ['ds', 'foo', 'bar'],
  146. series_columns: ['foo', 'bar'],
  147. },
  148. ),
  149. ).toEqual({
  150. operation: 'pivot',
  151. options: {
  152. aggregates: {
  153. 'count(*)': { operator: 'mean' },
  154. 'count(*)__1 year ago': { operator: 'mean' },
  155. 'count(*)__1 year later': { operator: 'mean' },
  156. 'sum(val)': {
  157. operator: 'mean',
  158. },
  159. 'sum(val)__1 year ago': {
  160. operator: 'mean',
  161. },
  162. 'sum(val)__1 year later': {
  163. operator: 'mean',
  164. },
  165. },
  166. drop_missing_columns: false,
  167. columns: ['foo', 'bar'],
  168. index: ['ds'],
  169. },
  170. });
  171. });
  172. test('should pivot on adhoc x-axis', () => {
  173. expect(
  174. timeComparePivotOperator(
  175. {
  176. ...formData,
  177. comparison_type: 'values',
  178. time_compare: ['1 year ago', '1 year later'],
  179. x_axis: {
  180. label: 'my_case_expr',
  181. expressionType: 'SQL',
  182. sqlExpression: 'case when a = 1 then 1 else 0 end',
  183. },
  184. },
  185. queryObject,
  186. ),
  187. ).toEqual({
  188. operation: 'pivot',
  189. options: {
  190. aggregates: {
  191. 'count(*)': { operator: 'mean' },
  192. 'count(*)__1 year ago': { operator: 'mean' },
  193. 'count(*)__1 year later': { operator: 'mean' },
  194. 'sum(val)': {
  195. operator: 'mean',
  196. },
  197. 'sum(val)__1 year ago': {
  198. operator: 'mean',
  199. },
  200. 'sum(val)__1 year later': {
  201. operator: 'mean',
  202. },
  203. },
  204. drop_missing_columns: false,
  205. columns: ['foo', 'bar'],
  206. index: ['my_case_expr'],
  207. },
  208. });
  209. });