buildQuery.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 { TimeGranularity } from '@superset-ui/core';
  20. import buildQuery from '../../src/plugin/buildQuery';
  21. import { PivotTableQueryFormData } from '../../src/types';
  22. const formData: PivotTableQueryFormData = {
  23. groupbyRows: ['row1', 'row2'],
  24. groupbyColumns: ['col1', 'col2'],
  25. metrics: ['metric1', 'metric2'],
  26. tableRenderer: 'Table With Subtotal',
  27. colOrder: 'key_a_to_z',
  28. rowOrder: 'key_a_to_z',
  29. aggregateFunction: 'Sum',
  30. transposePivot: true,
  31. rowSubtotalPosition: true,
  32. colSubtotalPosition: true,
  33. colTotals: true,
  34. colSubTotals: true,
  35. rowTotals: true,
  36. rowSubTotals: true,
  37. valueFormat: 'SMART_NUMBER',
  38. datasource: '5__table',
  39. viz_type: 'my_chart',
  40. width: 800,
  41. height: 600,
  42. combineMetric: false,
  43. verboseMap: {},
  44. columnFormats: {},
  45. currencyFormats: {},
  46. metricColorFormatters: [],
  47. dateFormatters: {},
  48. setDataMask: () => {},
  49. legacy_order_by: 'count',
  50. order_desc: true,
  51. margin: 0,
  52. time_grain_sqla: TimeGranularity.MONTH,
  53. temporal_columns_lookup: { col1: true },
  54. currencyFormat: { symbol: 'USD', symbolPosition: 'prefix' },
  55. };
  56. test('should build groupby with series in form data', () => {
  57. const queryContext = buildQuery(formData);
  58. const [query] = queryContext.queries;
  59. expect(query.columns).toEqual([
  60. {
  61. columnType: 'BASE_AXIS',
  62. expressionType: 'SQL',
  63. label: 'col1',
  64. sqlExpression: 'col1',
  65. timeGrain: 'P1M',
  66. },
  67. 'col2',
  68. 'row1',
  69. 'row2',
  70. ]);
  71. });
  72. test('should work with old charts', () => {
  73. const modifiedFormData = {
  74. ...formData,
  75. time_grain_sqla: TimeGranularity.MONTH,
  76. granularity_sqla: 'col1',
  77. };
  78. const queryContext = buildQuery(modifiedFormData);
  79. const [query] = queryContext.queries;
  80. expect(query.columns).toEqual([
  81. {
  82. timeGrain: 'P1M',
  83. columnType: 'BASE_AXIS',
  84. sqlExpression: 'col1',
  85. label: 'col1',
  86. expressionType: 'SQL',
  87. },
  88. 'col2',
  89. 'row1',
  90. 'row2',
  91. ]);
  92. });
  93. test('should prefer extra_form_data.time_grain_sqla over formData.time_grain_sqla', () => {
  94. const modifiedFormData = {
  95. ...formData,
  96. extra_form_data: { time_grain_sqla: TimeGranularity.QUARTER },
  97. };
  98. const queryContext = buildQuery(modifiedFormData);
  99. const [query] = queryContext.queries;
  100. expect(query.columns?.[0]).toEqual({
  101. timeGrain: TimeGranularity.QUARTER,
  102. columnType: 'BASE_AXIS',
  103. sqlExpression: 'col1',
  104. label: 'col1',
  105. expressionType: 'SQL',
  106. });
  107. });
  108. test('should fallback to formData.time_grain_sqla if extra_form_data.time_grain_sqla is not set', () => {
  109. const queryContext = buildQuery(formData);
  110. const [query] = queryContext.queries;
  111. expect(query.columns?.[0]).toEqual({
  112. timeGrain: formData.time_grain_sqla,
  113. columnType: 'BASE_AXIS',
  114. sqlExpression: 'col1',
  115. label: 'col1',
  116. expressionType: 'SQL',
  117. });
  118. });
  119. test('should not omit extras.time_grain_sqla from queryContext so dashboards apply them', () => {
  120. const modifiedFormData = {
  121. ...formData,
  122. extra_form_data: { time_grain_sqla: TimeGranularity.QUARTER },
  123. };
  124. const queryContext = buildQuery(modifiedFormData);
  125. const [query] = queryContext.queries;
  126. expect(query.extras?.time_grain_sqla).toEqual(TimeGranularity.QUARTER);
  127. });