buildQueryContext.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 { buildQueryContext, VizType } from '@superset-ui/core';
  20. import * as queryModule from '../../src/query/normalizeTimeColumn';
  21. describe('buildQueryContext', () => {
  22. it('should build datasource for table sources and apply defaults', () => {
  23. const queryContext = buildQueryContext({
  24. datasource: '5__table',
  25. granularity_sqla: 'ds',
  26. viz_type: VizType.Table,
  27. });
  28. expect(queryContext.datasource.id).toBe(5);
  29. expect(queryContext.datasource.type).toBe('table');
  30. expect(queryContext.force).toBe(false);
  31. expect(queryContext.result_format).toBe('json');
  32. expect(queryContext.result_type).toBe('full');
  33. });
  34. it('should build datasource for table sources with columns', () => {
  35. const queryContext = buildQueryContext(
  36. {
  37. datasource: '5__table',
  38. granularity_sqla: 'ds',
  39. viz_type: VizType.Table,
  40. source: 'source_column',
  41. source_category: 'source_category_column',
  42. target: 'target_column',
  43. target_category: 'target_category_column',
  44. },
  45. {
  46. queryFields: {
  47. source: 'columns',
  48. source_category: 'columns',
  49. target: 'columns',
  50. target_category: 'columns',
  51. },
  52. },
  53. );
  54. expect(queryContext.datasource.id).toBe(5);
  55. expect(queryContext.datasource.type).toBe('table');
  56. expect(queryContext.force).toBe(false);
  57. expect(queryContext.result_format).toBe('json');
  58. expect(queryContext.result_type).toBe('full');
  59. expect(queryContext.queries).toEqual(
  60. expect.arrayContaining([
  61. expect.objectContaining({
  62. columns: [
  63. 'source_column',
  64. 'source_category_column',
  65. 'target_column',
  66. 'target_category_column',
  67. ],
  68. }),
  69. ]),
  70. );
  71. });
  72. it('should build datasource for table sources and process with custom function', () => {
  73. const queryContext = buildQueryContext(
  74. {
  75. datasource: '5__table',
  76. granularity_sqla: 'ds',
  77. viz_type: VizType.Table,
  78. source: 'source_column',
  79. source_category: 'source_category_column',
  80. target: 'target_column',
  81. target_category: 'target_category_column',
  82. },
  83. function addExtraColumn(queryObject) {
  84. return [{ ...queryObject, columns: ['dummy_column'] }];
  85. },
  86. );
  87. expect(queryContext.datasource.id).toBe(5);
  88. expect(queryContext.datasource.type).toBe('table');
  89. expect(queryContext.force).toBe(false);
  90. expect(queryContext.result_format).toBe('json');
  91. expect(queryContext.result_type).toBe('full');
  92. expect(queryContext.queries).toEqual(
  93. expect.arrayContaining([
  94. expect.objectContaining({
  95. columns: ['dummy_column'],
  96. }),
  97. ]),
  98. );
  99. });
  100. // todo(Yongjie): move these test case into buildQueryObject.test.ts
  101. it('should remove undefined value in post_processing', () => {
  102. const queryContext = buildQueryContext(
  103. {
  104. datasource: '5__table',
  105. viz_type: VizType.Table,
  106. },
  107. () => [
  108. {
  109. post_processing: [
  110. undefined,
  111. undefined,
  112. {
  113. operation: 'flatten',
  114. },
  115. undefined,
  116. ],
  117. },
  118. ],
  119. );
  120. expect(queryContext.queries[0].post_processing).toEqual([
  121. {
  122. operation: 'flatten',
  123. },
  124. ]);
  125. });
  126. it('should call normalizeTimeColumn if has x_axis', () => {
  127. const spyNormalizeTimeColumn = jest.spyOn(
  128. queryModule,
  129. 'normalizeTimeColumn',
  130. );
  131. buildQueryContext(
  132. {
  133. datasource: '5__table',
  134. viz_type: VizType.Table,
  135. x_axis: 'axis',
  136. },
  137. () => [{}],
  138. );
  139. expect(spyNormalizeTimeColumn).toHaveBeenCalled();
  140. spyNormalizeTimeColumn.mockRestore();
  141. });
  142. });