pivotOperator.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 { pivotOperator } 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. granularity: 'month',
  28. datasource: 'foo',
  29. viz_type: VizType.Table,
  30. show_empty_columns: true,
  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. post_processing: [
  40. {
  41. operation: 'pivot',
  42. options: {
  43. index: ['__timestamp'],
  44. columns: ['nation'],
  45. aggregates: {
  46. 'count(*)': {
  47. operator: 'mean',
  48. },
  49. },
  50. drop_missing_columns: false,
  51. },
  52. },
  53. ],
  54. };
  55. test('skip pivot', () => {
  56. expect(pivotOperator(formData, queryObject)).toEqual(undefined);
  57. expect(
  58. pivotOperator(formData, {
  59. ...queryObject,
  60. metrics: [],
  61. }),
  62. ).toEqual(undefined);
  63. });
  64. test('pivot by __timestamp without columns', () => {
  65. expect(
  66. pivotOperator(
  67. { ...formData, granularity_sqla: 'time_column' },
  68. queryObject,
  69. ),
  70. ).toEqual({
  71. operation: 'pivot',
  72. options: {
  73. index: ['__timestamp'],
  74. columns: [],
  75. aggregates: {
  76. 'count(*)': { operator: 'mean' },
  77. 'sum(val)': { operator: 'mean' },
  78. },
  79. drop_missing_columns: false,
  80. },
  81. });
  82. });
  83. test('pivot by __timestamp with columns', () => {
  84. expect(
  85. pivotOperator(
  86. { ...formData, granularity_sqla: 'time_column' },
  87. {
  88. ...queryObject,
  89. columns: ['foo', 'bar'],
  90. },
  91. ),
  92. ).toEqual({
  93. operation: 'pivot',
  94. options: {
  95. index: ['__timestamp'],
  96. columns: ['foo', 'bar'],
  97. aggregates: {
  98. 'count(*)': { operator: 'mean' },
  99. 'sum(val)': { operator: 'mean' },
  100. },
  101. drop_missing_columns: false,
  102. },
  103. });
  104. });
  105. test('pivot by __timestamp with series_columns', () => {
  106. expect(
  107. pivotOperator(
  108. { ...formData, granularity_sqla: 'time_column' },
  109. {
  110. ...queryObject,
  111. series_columns: ['foo', 'bar'],
  112. },
  113. ),
  114. ).toEqual({
  115. operation: 'pivot',
  116. options: {
  117. index: ['__timestamp'],
  118. columns: ['foo', 'bar'],
  119. aggregates: {
  120. 'count(*)': { operator: 'mean' },
  121. 'sum(val)': { operator: 'mean' },
  122. },
  123. drop_missing_columns: false,
  124. },
  125. });
  126. });
  127. test('pivot by x_axis with groupby', () => {
  128. expect(
  129. pivotOperator(
  130. {
  131. ...formData,
  132. x_axis: 'baz',
  133. },
  134. {
  135. ...queryObject,
  136. series_columns: ['foo', 'bar'],
  137. },
  138. ),
  139. ).toEqual({
  140. operation: 'pivot',
  141. options: {
  142. index: ['baz'],
  143. columns: ['foo', 'bar'],
  144. aggregates: {
  145. 'count(*)': { operator: 'mean' },
  146. 'sum(val)': { operator: 'mean' },
  147. },
  148. drop_missing_columns: false,
  149. },
  150. });
  151. });
  152. test('pivot by adhoc x_axis', () => {
  153. expect(
  154. pivotOperator(
  155. {
  156. ...formData,
  157. x_axis: {
  158. label: 'my_case_expr',
  159. expressionType: 'SQL',
  160. sqlExpression: 'case when a = 1 then 1 else 0 end',
  161. },
  162. },
  163. {
  164. ...queryObject,
  165. series_columns: ['foo', 'bar'],
  166. },
  167. ),
  168. ).toEqual({
  169. operation: 'pivot',
  170. options: {
  171. index: ['my_case_expr'],
  172. columns: ['foo', 'bar'],
  173. aggregates: {
  174. 'count(*)': { operator: 'mean' },
  175. 'sum(val)': { operator: 'mean' },
  176. },
  177. drop_missing_columns: false,
  178. },
  179. });
  180. });
  181. test('pivot by x_axis with extra metrics', () => {
  182. expect(
  183. pivotOperator(
  184. {
  185. ...formData,
  186. x_axis: 'foo',
  187. x_axis_sort: 'bar',
  188. groupby: [],
  189. timeseries_limit_metric: 'bar',
  190. },
  191. {
  192. ...queryObject,
  193. series_columns: [],
  194. },
  195. ),
  196. ).toEqual({
  197. operation: 'pivot',
  198. options: {
  199. index: ['foo'],
  200. columns: [],
  201. aggregates: {
  202. 'count(*)': { operator: 'mean' },
  203. 'sum(val)': { operator: 'mean' },
  204. bar: { operator: 'mean' },
  205. },
  206. drop_missing_columns: false,
  207. },
  208. });
  209. });