normalizeOrderBy.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 { normalizeOrderBy, QueryObject, VizType } from '@superset-ui/core';
  20. describe('normalizeOrderBy', () => {
  21. it('should not change original queryObject when orderby populated', () => {
  22. const query: QueryObject = {
  23. datasource: '5__table',
  24. viz_type: VizType.Table,
  25. time_range: '1 year ago : 2013',
  26. orderby: [['count(*)', true]],
  27. };
  28. expect(normalizeOrderBy(query)).toEqual(query);
  29. });
  30. it('has series_limit_metric in queryObject', () => {
  31. const query: QueryObject = {
  32. datasource: '5__table',
  33. viz_type: VizType.Table,
  34. time_range: '1 year ago : 2013',
  35. metrics: ['count(*)'],
  36. series_limit_metric: {
  37. expressionType: 'SIMPLE',
  38. column: {
  39. id: 1,
  40. column_name: 'sales',
  41. },
  42. aggregate: 'SUM',
  43. },
  44. order_desc: true,
  45. };
  46. const expectedQueryObject = normalizeOrderBy(query);
  47. expect(expectedQueryObject).not.toHaveProperty('series_limit_metric');
  48. expect(expectedQueryObject).not.toHaveProperty('order_desc');
  49. expect(expectedQueryObject).toEqual({
  50. datasource: '5__table',
  51. viz_type: VizType.Table,
  52. time_range: '1 year ago : 2013',
  53. metrics: ['count(*)'],
  54. orderby: [
  55. [
  56. {
  57. expressionType: 'SIMPLE',
  58. column: {
  59. id: 1,
  60. column_name: 'sales',
  61. },
  62. aggregate: 'SUM',
  63. },
  64. false,
  65. ],
  66. ],
  67. });
  68. });
  69. it('should transform legacy_order_by in queryObject', () => {
  70. const query: QueryObject = {
  71. datasource: '5__table',
  72. viz_type: VizType.Table,
  73. time_range: '1 year ago : 2013',
  74. metrics: ['count(*)'],
  75. legacy_order_by: {
  76. expressionType: 'SIMPLE',
  77. column: {
  78. id: 1,
  79. column_name: 'sales',
  80. },
  81. aggregate: 'SUM',
  82. },
  83. order_desc: true,
  84. };
  85. const expectedQueryObject = normalizeOrderBy(query);
  86. expect(expectedQueryObject).not.toHaveProperty('legacy_order_by');
  87. expect(expectedQueryObject).not.toHaveProperty('order_desc');
  88. expect(expectedQueryObject).toEqual({
  89. datasource: '5__table',
  90. viz_type: VizType.Table,
  91. time_range: '1 year ago : 2013',
  92. metrics: ['count(*)'],
  93. orderby: [
  94. [
  95. {
  96. expressionType: 'SIMPLE',
  97. column: {
  98. id: 1,
  99. column_name: 'sales',
  100. },
  101. aggregate: 'SUM',
  102. },
  103. false,
  104. ],
  105. ],
  106. });
  107. });
  108. it('has metrics in queryObject', () => {
  109. const query: QueryObject = {
  110. datasource: '5__table',
  111. viz_type: VizType.Table,
  112. time_range: '1 year ago : 2013',
  113. metrics: ['count(*)'],
  114. order_desc: true,
  115. };
  116. const expectedQueryObject = normalizeOrderBy(query);
  117. expect(expectedQueryObject).not.toHaveProperty('series_limit_metric');
  118. expect(expectedQueryObject).not.toHaveProperty('order_desc');
  119. expect(expectedQueryObject).toEqual({
  120. datasource: '5__table',
  121. viz_type: VizType.Table,
  122. time_range: '1 year ago : 2013',
  123. metrics: ['count(*)'],
  124. orderby: [['count(*)', false]],
  125. });
  126. });
  127. it('should not change', () => {
  128. const query: QueryObject = {
  129. datasource: '5__table',
  130. viz_type: VizType.Table,
  131. time_range: '1 year ago : 2013',
  132. };
  133. expect(normalizeOrderBy(query)).toEqual(query);
  134. });
  135. it('remove empty orderby', () => {
  136. const query: QueryObject = {
  137. datasource: '5__table',
  138. viz_type: VizType.Table,
  139. time_range: '1 year ago : 2013',
  140. orderby: [],
  141. };
  142. expect(normalizeOrderBy(query)).not.toHaveProperty('orderby');
  143. });
  144. it('remove orderby with an empty array', () => {
  145. const query: QueryObject = {
  146. datasource: '5__table',
  147. viz_type: VizType.Table,
  148. time_range: '1 year ago : 2013',
  149. orderby: [[]],
  150. };
  151. expect(normalizeOrderBy(query)).not.toHaveProperty('orderby');
  152. });
  153. it('remove orderby with an empty metric', () => {
  154. const query: QueryObject = {
  155. datasource: '5__table',
  156. viz_type: VizType.Table,
  157. time_range: '1 year ago : 2013',
  158. orderby: [['', true]],
  159. };
  160. expect(normalizeOrderBy(query)).not.toHaveProperty('orderby');
  161. });
  162. it('remove orderby with an empty adhoc metric', () => {
  163. const query: QueryObject = {
  164. datasource: '5__table',
  165. viz_type: VizType.Table,
  166. time_range: '1 year ago : 2013',
  167. orderby: [[{}, true]],
  168. };
  169. expect(normalizeOrderBy(query)).not.toHaveProperty('orderby');
  170. });
  171. it('remove orderby with an non-boolean type', () => {
  172. const query: QueryObject = {
  173. datasource: '5__table',
  174. viz_type: VizType.Table,
  175. time_range: '1 year ago : 2013',
  176. // @ts-ignore
  177. orderby: [['count(*)', 'true']],
  178. };
  179. expect(normalizeOrderBy(query)).not.toHaveProperty('orderby');
  180. });
  181. });