normalizeTimeColumn.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 {
  20. normalizeTimeColumn,
  21. QueryObject,
  22. SqlaFormData,
  23. VizType,
  24. } from '@superset-ui/core';
  25. test('should return original QueryObject if x_axis is empty', () => {
  26. const formData: SqlaFormData = {
  27. datasource: '5__table',
  28. viz_type: VizType.Table,
  29. granularity: 'time_column',
  30. time_grain_sqla: 'P1Y',
  31. time_range: '1 year ago : 2013',
  32. columns: ['col1'],
  33. metrics: ['count(*)'],
  34. };
  35. const query: QueryObject = {
  36. datasource: '5__table',
  37. viz_type: VizType.Table,
  38. granularity: 'time_column',
  39. extras: {
  40. time_grain_sqla: 'P1Y',
  41. },
  42. time_range: '1 year ago : 2013',
  43. orderby: [['count(*)', true]],
  44. columns: ['col1'],
  45. metrics: ['count(*)'],
  46. is_timeseries: true,
  47. };
  48. expect(normalizeTimeColumn(formData, query)).toEqual(query);
  49. });
  50. test('should support different columns for x-axis and granularity', () => {
  51. const formData: SqlaFormData = {
  52. datasource: '5__table',
  53. viz_type: VizType.Table,
  54. granularity: 'time_column',
  55. time_grain_sqla: 'P1Y',
  56. time_range: '1 year ago : 2013',
  57. x_axis: 'time_column_in_x_axis',
  58. columns: ['col1'],
  59. metrics: ['count(*)'],
  60. };
  61. const query: QueryObject = {
  62. datasource: '5__table',
  63. viz_type: VizType.Table,
  64. granularity: 'time_column',
  65. extras: {
  66. time_grain_sqla: 'P1Y',
  67. where: '',
  68. having: '',
  69. },
  70. time_range: '1 year ago : 2013',
  71. orderby: [['count(*)', true]],
  72. columns: ['time_column_in_x_axis', 'col1'],
  73. metrics: ['count(*)'],
  74. is_timeseries: true,
  75. };
  76. expect(normalizeTimeColumn(formData, query)).toEqual({
  77. datasource: '5__table',
  78. viz_type: VizType.Table,
  79. granularity: 'time_column',
  80. extras: { where: '', having: '', time_grain_sqla: 'P1Y' },
  81. time_range: '1 year ago : 2013',
  82. orderby: [['count(*)', true]],
  83. columns: [
  84. {
  85. timeGrain: 'P1Y',
  86. columnType: 'BASE_AXIS',
  87. sqlExpression: 'time_column_in_x_axis',
  88. label: 'time_column_in_x_axis',
  89. expressionType: 'SQL',
  90. },
  91. 'col1',
  92. ],
  93. metrics: ['count(*)'],
  94. });
  95. });
  96. test('should support custom SQL in x-axis', () => {
  97. const formData: SqlaFormData = {
  98. datasource: '5__table',
  99. viz_type: VizType.Table,
  100. granularity: 'time_column',
  101. time_grain_sqla: 'P1Y',
  102. time_range: '1 year ago : 2013',
  103. x_axis: {
  104. expressionType: 'SQL',
  105. label: 'Order Data + 1 year',
  106. sqlExpression: '"Order Date" + interval \'1 year\'',
  107. },
  108. columns: ['col1'],
  109. metrics: ['count(*)'],
  110. };
  111. const query: QueryObject = {
  112. datasource: '5__table',
  113. viz_type: VizType.Table,
  114. granularity: 'time_column',
  115. extras: {
  116. time_grain_sqla: 'P1Y',
  117. where: '',
  118. having: '',
  119. },
  120. time_range: '1 year ago : 2013',
  121. orderby: [['count(*)', true]],
  122. columns: [
  123. {
  124. expressionType: 'SQL',
  125. label: 'Order Data + 1 year',
  126. sqlExpression: '"Order Date" + interval \'1 year\'',
  127. },
  128. 'col1',
  129. ],
  130. metrics: ['count(*)'],
  131. is_timeseries: true,
  132. };
  133. expect(normalizeTimeColumn(formData, query)).toEqual({
  134. datasource: '5__table',
  135. viz_type: VizType.Table,
  136. granularity: 'time_column',
  137. extras: { where: '', having: '', time_grain_sqla: 'P1Y' },
  138. time_range: '1 year ago : 2013',
  139. orderby: [['count(*)', true]],
  140. columns: [
  141. {
  142. timeGrain: 'P1Y',
  143. columnType: 'BASE_AXIS',
  144. expressionType: 'SQL',
  145. label: 'Order Data + 1 year',
  146. sqlExpression: `"Order Date" + interval '1 year'`,
  147. },
  148. 'col1',
  149. ],
  150. metrics: ['count(*)'],
  151. });
  152. });
  153. test('fallback and invalid columns value', () => {
  154. const formData: SqlaFormData = {
  155. datasource: '5__table',
  156. viz_type: VizType.Table,
  157. granularity: 'time_column',
  158. time_grain_sqla: 'P1Y',
  159. time_range: '1 year ago : 2013',
  160. x_axis: {
  161. expressionType: 'SQL',
  162. label: 'Order Data + 1 year',
  163. sqlExpression: '"Order Date" + interval \'1 year\'',
  164. },
  165. columns: ['col1'],
  166. metrics: ['count(*)'],
  167. };
  168. const query: QueryObject = {
  169. datasource: '5__table',
  170. viz_type: VizType.Table,
  171. granularity: 'time_column',
  172. extras: {
  173. time_grain_sqla: 'P1Y',
  174. where: '',
  175. having: '',
  176. },
  177. time_range: '1 year ago : 2013',
  178. orderby: [['count(*)', true]],
  179. metrics: ['count(*)'],
  180. is_timeseries: true,
  181. };
  182. expect(normalizeTimeColumn(formData, query)).toEqual(query);
  183. });