buildQuery.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 { SqlaFormData, VizType } from '@superset-ui/core';
  20. import buildQuery from '../../src/Timeseries/buildQuery';
  21. describe('Timeseries buildQuery', () => {
  22. const formData = {
  23. datasource: '5__table',
  24. granularity_sqla: 'ds',
  25. metrics: ['bar', 'baz'],
  26. viz_type: 'my_chart',
  27. };
  28. it('should build groupby with series in form data', () => {
  29. const queryContext = buildQuery(formData);
  30. const [query] = queryContext.queries;
  31. expect(query.metrics).toEqual(['bar', 'baz']);
  32. });
  33. it('should order by timeseries limit if orderby unspecified', () => {
  34. const queryContext = buildQuery({
  35. ...formData,
  36. timeseries_limit_metric: 'bar',
  37. order_desc: true,
  38. });
  39. const [query] = queryContext.queries;
  40. expect(query.metrics).toEqual(['bar', 'baz']);
  41. expect(query.series_limit_metric).toEqual('bar');
  42. expect(query.order_desc).toEqual(true);
  43. expect(query.orderby).toEqual([['bar', false]]);
  44. });
  45. it('should not order by timeseries limit if orderby provided', () => {
  46. const queryContext = buildQuery({
  47. ...formData,
  48. timeseries_limit_metric: 'bar',
  49. order_desc: true,
  50. orderby: [['foo', true]],
  51. });
  52. const [query] = queryContext.queries;
  53. expect(query.metrics).toEqual(['bar', 'baz']);
  54. expect(query.series_limit_metric).toEqual('bar');
  55. expect(query.order_desc).toEqual(true);
  56. expect(query.orderby).toEqual([['foo', true]]);
  57. });
  58. });
  59. describe('queryObject conversion', () => {
  60. const formData: SqlaFormData = {
  61. datasource: '5__table',
  62. viz_type: VizType.Table,
  63. granularity_sqla: 'time_column',
  64. time_grain_sqla: 'P1Y',
  65. time_range: '1 year ago : 2013',
  66. groupby: ['col1'],
  67. metrics: ['count(*)'],
  68. };
  69. it("shouldn't convert queryObject", () => {
  70. const { queries } = buildQuery(formData);
  71. expect(queries[0]).toEqual(
  72. expect.objectContaining({
  73. granularity: 'time_column',
  74. time_range: '1 year ago : 2013',
  75. extras: { time_grain_sqla: 'P1Y', having: '', where: '' },
  76. columns: ['col1'],
  77. series_columns: ['col1'],
  78. metrics: ['count(*)'],
  79. is_timeseries: true,
  80. post_processing: [
  81. {
  82. operation: 'pivot',
  83. options: {
  84. aggregates: { 'count(*)': { operator: 'mean' } },
  85. columns: ['col1'],
  86. drop_missing_columns: true,
  87. index: ['__timestamp'],
  88. },
  89. },
  90. { operation: 'flatten' },
  91. ],
  92. }),
  93. );
  94. });
  95. it('should convert queryObject', () => {
  96. const { queries } = buildQuery({ ...formData, x_axis: 'time_column' });
  97. expect(queries[0]).toEqual(
  98. expect.objectContaining({
  99. granularity: 'time_column',
  100. time_range: '1 year ago : 2013',
  101. extras: { having: '', where: '', time_grain_sqla: 'P1Y' },
  102. columns: [
  103. {
  104. columnType: 'BASE_AXIS',
  105. expressionType: 'SQL',
  106. label: 'time_column',
  107. sqlExpression: 'time_column',
  108. timeGrain: 'P1Y',
  109. },
  110. 'col1',
  111. ],
  112. series_columns: ['col1'],
  113. metrics: ['count(*)'],
  114. post_processing: [
  115. {
  116. operation: 'pivot',
  117. options: {
  118. aggregates: { 'count(*)': { operator: 'mean' } },
  119. columns: ['col1'],
  120. drop_missing_columns: true,
  121. index: ['time_column'],
  122. },
  123. },
  124. { operation: 'flatten' },
  125. ],
  126. }),
  127. );
  128. });
  129. });