buildQueryObject.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. JsonObject,
  21. AnnotationLayer,
  22. AnnotationOpacity,
  23. AnnotationSourceType,
  24. AnnotationStyle,
  25. AnnotationType,
  26. buildQueryObject,
  27. QueryObject,
  28. VizType,
  29. } from '@superset-ui/core';
  30. describe('buildQueryObject', () => {
  31. let query: QueryObject;
  32. it('should build granularity for sqlalchemy datasources', () => {
  33. query = buildQueryObject({
  34. datasource: '5__table',
  35. granularity_sqla: 'ds',
  36. viz_type: VizType.Table,
  37. });
  38. expect(query.granularity).toEqual('ds');
  39. });
  40. it('should build metrics based on default queryFields', () => {
  41. query = buildQueryObject({
  42. datasource: '5__table',
  43. granularity_sqla: 'ds',
  44. viz_type: VizType.Table,
  45. metric: 'sum__num',
  46. secondary_metric: 'avg__num',
  47. });
  48. expect(query.metrics).toEqual(['sum__num', 'avg__num']);
  49. });
  50. it('should merge original and append filters', () => {
  51. query = buildQueryObject({
  52. datasource: '5__table',
  53. granularity_sqla: 'ds',
  54. viz_type: VizType.Table,
  55. extra_filters: [{ col: 'abc', op: '==', val: 'qwerty' }],
  56. adhoc_filters: [
  57. {
  58. expressionType: 'SIMPLE',
  59. clause: 'WHERE',
  60. subject: 'foo',
  61. operator: '!=',
  62. comparator: 'bar',
  63. },
  64. ],
  65. where: 'a = b',
  66. extra_form_data: {
  67. adhoc_filters: [
  68. {
  69. expressionType: 'SQL',
  70. clause: 'WHERE',
  71. sqlExpression: '(1 = 1)',
  72. },
  73. ],
  74. },
  75. });
  76. expect(query.filters).toEqual([
  77. { col: 'abc', op: '==', val: 'qwerty' },
  78. { col: 'foo', op: '!=', val: 'bar' },
  79. ]);
  80. expect(query.extras?.where).toEqual('(a = b) AND ((1 = 1))');
  81. });
  82. it('should group custom metric control', () => {
  83. query = buildQueryObject(
  84. {
  85. datasource: '5__table',
  86. granularity_sqla: 'ds',
  87. viz_type: VizType.Table,
  88. my_custom_metric_control: 'sum__num',
  89. },
  90. { my_custom_metric_control: 'metrics' },
  91. );
  92. expect(query.metrics).toEqual(['sum__num']);
  93. });
  94. it('should group custom metric control with predefined metrics', () => {
  95. query = buildQueryObject(
  96. {
  97. datasource: '5__table',
  98. granularity_sqla: 'ds',
  99. viz_type: VizType.Table,
  100. metrics: ['sum__num'],
  101. my_custom_metric_control: 'avg__num',
  102. },
  103. { my_custom_metric_control: 'metrics' },
  104. );
  105. expect(query.metrics).toEqual(['sum__num', 'avg__num']);
  106. });
  107. it('should build series_limit from legacy control', () => {
  108. const series_limit = 2;
  109. query = buildQueryObject({
  110. datasource: '5__table',
  111. granularity_sqla: 'ds',
  112. viz_type: VizType.Table,
  113. limit: series_limit,
  114. });
  115. expect(query.series_limit).toEqual(series_limit);
  116. });
  117. it('should build series_limit', () => {
  118. const series_limit = 2;
  119. query = buildQueryObject({
  120. datasource: '5__table',
  121. granularity_sqla: 'ds',
  122. viz_type: VizType.Table,
  123. series_limit,
  124. });
  125. expect(query.series_limit).toEqual(series_limit);
  126. });
  127. it('should build order_desc', () => {
  128. const orderDesc = false;
  129. query = buildQueryObject({
  130. datasource: '5__table',
  131. granularity_sqla: 'ds',
  132. viz_type: VizType.Table,
  133. order_desc: orderDesc,
  134. });
  135. expect(query.order_desc).toEqual(orderDesc);
  136. });
  137. it('should build series_limit_metric from legacy control', () => {
  138. const metric = 'country';
  139. query = buildQueryObject({
  140. datasource: '5__table',
  141. granularity_sqla: 'ds',
  142. viz_type: VizType.Table,
  143. timeseries_limit_metric: metric,
  144. });
  145. expect(query.series_limit_metric).toEqual(metric);
  146. });
  147. it('should build series_limit_metric', () => {
  148. const metric = 'country';
  149. query = buildQueryObject({
  150. datasource: '5__table',
  151. granularity_sqla: 'ds',
  152. viz_type: VizType.PivotTable,
  153. series_limit_metric: metric,
  154. });
  155. expect(query.series_limit_metric).toEqual(metric);
  156. });
  157. it('should build series_limit_metric as undefined when empty array', () => {
  158. const metric: any = [];
  159. query = buildQueryObject({
  160. datasource: '5__table',
  161. granularity_sqla: 'ds',
  162. viz_type: VizType.PivotTable,
  163. series_limit_metric: metric,
  164. });
  165. expect(query.series_limit_metric).toEqual(undefined);
  166. });
  167. it('should handle null and non-numeric row_limit and row_offset', () => {
  168. const baseQuery = {
  169. datasource: '5__table',
  170. granularity_sqla: 'ds',
  171. viz_type: VizType.Table,
  172. row_limit: null,
  173. };
  174. // undefined
  175. query = buildQueryObject({ ...baseQuery });
  176. expect(query.row_limit).toBeUndefined();
  177. expect(query.row_offset).toBeUndefined();
  178. // null value
  179. query = buildQueryObject({
  180. ...baseQuery,
  181. row_limit: null,
  182. row_offset: null,
  183. });
  184. expect(query.row_limit).toBeUndefined();
  185. expect(query.row_offset).toBeUndefined();
  186. query = buildQueryObject({ ...baseQuery, row_limit: 1000, row_offset: 50 });
  187. expect(query.row_limit).toStrictEqual(1000);
  188. expect(query.row_offset).toStrictEqual(50);
  189. // valid string
  190. query = buildQueryObject({
  191. ...baseQuery,
  192. row_limit: '200',
  193. row_offset: '100',
  194. });
  195. expect(query.row_limit).toStrictEqual(200);
  196. expect(query.row_offset).toStrictEqual(100);
  197. // invalid string
  198. query = buildQueryObject({
  199. ...baseQuery,
  200. row_limit: 'two hundred',
  201. row_offset: 'twenty',
  202. });
  203. expect(query.row_limit).toBeUndefined();
  204. expect(query.row_offset).toBeUndefined();
  205. });
  206. it('should populate annotation_layers', () => {
  207. const annotationLayers: AnnotationLayer[] = [
  208. {
  209. annotationType: AnnotationType.Formula,
  210. color: '#ff7f44',
  211. name: 'My Formula',
  212. opacity: AnnotationOpacity.Low,
  213. show: true,
  214. showLabel: false,
  215. style: AnnotationStyle.Solid,
  216. value: '10*sin(x)',
  217. width: 1,
  218. },
  219. {
  220. annotationType: AnnotationType.Interval,
  221. color: null,
  222. show: false,
  223. showLabel: false,
  224. name: 'My Interval',
  225. sourceType: AnnotationSourceType.Native,
  226. style: AnnotationStyle.Dashed,
  227. value: 1,
  228. width: 100,
  229. },
  230. {
  231. annotationType: AnnotationType.Event,
  232. color: null,
  233. descriptionColumns: [],
  234. name: 'My Interval',
  235. overrides: {
  236. granularity: null,
  237. time_grain_sqla: null,
  238. time_range: null,
  239. },
  240. sourceType: AnnotationSourceType.Table,
  241. show: false,
  242. showLabel: false,
  243. timeColumn: 'ds',
  244. style: AnnotationStyle.Dashed,
  245. value: 1,
  246. width: 100,
  247. },
  248. ];
  249. query = buildQueryObject({
  250. datasource: '5__table',
  251. granularity_sqla: 'ds',
  252. viz_type: VizType.Table,
  253. annotation_layers: annotationLayers,
  254. });
  255. expect(query.annotation_layers).toEqual(annotationLayers);
  256. });
  257. it('should populate url_params', () => {
  258. expect(
  259. buildQueryObject({
  260. datasource: '5__table',
  261. granularity_sqla: 'ds',
  262. viz_type: VizType.Table,
  263. url_params: { abc: '123' },
  264. }).url_params,
  265. ).toEqual({ abc: '123' });
  266. expect(
  267. buildQueryObject({
  268. datasource: '5__table',
  269. granularity_sqla: 'ds',
  270. viz_type: VizType.Table,
  271. // @ts-expect-error
  272. url_params: null,
  273. }).url_params,
  274. ).toBeUndefined();
  275. });
  276. it('should populate granularity', () => {
  277. const granularity = 'ds';
  278. query = buildQueryObject({
  279. datasource: '5__table',
  280. granularity,
  281. viz_type: VizType.Table,
  282. });
  283. expect(query.granularity).toEqual(granularity);
  284. });
  285. it('should populate granularity from legacy field', () => {
  286. const granularity = 'ds';
  287. query = buildQueryObject({
  288. datasource: '5__table',
  289. granularity_sqla: granularity,
  290. viz_type: VizType.Table,
  291. });
  292. expect(query.granularity).toEqual(granularity);
  293. });
  294. it('should populate custom_params', () => {
  295. const customParams: JsonObject = {
  296. customObject: { id: 137, name: 'C-137' },
  297. };
  298. query = buildQueryObject({
  299. datasource: '5__table',
  300. granularity_sqla: 'ds',
  301. viz_type: VizType.Table,
  302. custom_params: customParams,
  303. });
  304. expect(query.custom_params).toEqual(customParams);
  305. });
  306. });