extractQueryFields.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 { configure, QueryMode } from '@superset-ui/core';
  20. import extractQueryFields from '../../src/query/extractQueryFields';
  21. import { NUM_METRIC } from '../fixtures';
  22. configure();
  23. describe('extractQueryFields', () => {
  24. it('should return default object', () => {
  25. expect(extractQueryFields({})).toEqual({
  26. columns: [],
  27. metrics: [],
  28. orderby: undefined,
  29. });
  30. });
  31. it('should group single value to arrays', () => {
  32. expect(
  33. extractQueryFields({
  34. metric: 'my_metric',
  35. columns: 'abc',
  36. orderby: '["ccc",true]',
  37. }),
  38. ).toEqual({
  39. metrics: ['my_metric'],
  40. columns: ['abc'],
  41. orderby: [['ccc', true]],
  42. });
  43. });
  44. it('should combine field aliases', () => {
  45. expect(
  46. extractQueryFields(
  47. {
  48. metric: 'metric_1',
  49. metric_2: 'metric_2',
  50. my_custom_metric: 'my_custom_metric',
  51. },
  52. { my_custom_metric: 'metrics' },
  53. ).metrics,
  54. ).toEqual(['metric_1', 'metric_2', 'my_custom_metric']);
  55. });
  56. it('should extract columns', () => {
  57. expect(extractQueryFields({ columns: 'col_1' })).toEqual({
  58. columns: ['col_1'],
  59. metrics: [],
  60. orderby: undefined,
  61. });
  62. });
  63. it('should extract groupby as columns and set empty metrics', () => {
  64. expect(extractQueryFields({ groupby: 'col_1' })).toEqual({
  65. columns: ['col_1'],
  66. metrics: [],
  67. orderby: undefined,
  68. });
  69. });
  70. it('should remove duplicate metrics', () => {
  71. expect(
  72. extractQueryFields({
  73. metrics: ['col_1', { ...NUM_METRIC }, { ...NUM_METRIC }],
  74. }),
  75. ).toEqual({
  76. columns: [],
  77. metrics: ['col_1', NUM_METRIC],
  78. orderby: undefined,
  79. });
  80. });
  81. it('should extract custom columns fields', () => {
  82. expect(
  83. extractQueryFields(
  84. { series: 'col_1', metric: 'metric_1' },
  85. { series: 'groupby' },
  86. ),
  87. ).toEqual({
  88. columns: ['col_1'],
  89. metrics: ['metric_1'],
  90. orderby: undefined,
  91. });
  92. });
  93. it('should merge custom groupby into columns', () => {
  94. expect(
  95. extractQueryFields(
  96. { groupby: 'col_1', series: 'col_2', metric: 'metric_1' },
  97. { series: 'groupby' },
  98. ),
  99. ).toEqual({
  100. columns: ['col_1', 'col_2'],
  101. metrics: ['metric_1'],
  102. orderby: undefined,
  103. });
  104. });
  105. it('should ignore null values', () => {
  106. expect(
  107. extractQueryFields({ series: ['a'], columns: null }).columns,
  108. ).toEqual(['a']);
  109. });
  110. it('should ignore groupby and metrics when in raw QueryMode', () => {
  111. expect(
  112. extractQueryFields({
  113. columns: ['a'],
  114. groupby: ['b'],
  115. metric: ['m'],
  116. query_mode: QueryMode.Raw,
  117. }),
  118. ).toEqual({
  119. columns: ['a'],
  120. metrics: undefined,
  121. orderby: undefined,
  122. });
  123. });
  124. it('should ignore columns when in aggregate QueryMode', () => {
  125. expect(
  126. extractQueryFields({
  127. columns: ['a'],
  128. groupby: [],
  129. metric: ['m'],
  130. query_mode: QueryMode.Aggregate,
  131. }),
  132. ).toEqual({
  133. metrics: ['m'],
  134. columns: [],
  135. orderby: undefined,
  136. });
  137. expect(
  138. extractQueryFields({
  139. columns: ['a'],
  140. groupby: ['b'],
  141. metric: ['m'],
  142. query_mode: QueryMode.Aggregate,
  143. }),
  144. ).toEqual({
  145. metrics: ['m'],
  146. columns: ['b'],
  147. orderby: undefined,
  148. });
  149. });
  150. it('should parse orderby if needed', () => {
  151. expect(
  152. extractQueryFields({
  153. columns: ['a'],
  154. order_by_cols: ['["foo",false]', '["bar",true]'],
  155. orderby: [['abc', true]],
  156. }),
  157. ).toEqual({
  158. columns: ['a'],
  159. metrics: [],
  160. orderby: [
  161. ['foo', false],
  162. ['bar', true],
  163. ['abc', true],
  164. ],
  165. });
  166. });
  167. it('should throw error if parse orderby failed', () => {
  168. expect(() => {
  169. extractQueryFields({
  170. orderby: ['ccc'],
  171. });
  172. }).toThrow('invalid orderby');
  173. });
  174. });