extractExtras.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 { TimeGranularity } from '@superset-ui/core';
  20. import extractExtras from '../../src/query/extractExtras';
  21. describe('extractExtras', () => {
  22. const baseQueryFormData = {
  23. datasource: '1__table',
  24. granularity_sqla: 'ds',
  25. time_grain_sqla: TimeGranularity.MINUTE,
  26. viz_type: 'my_viz',
  27. };
  28. it('should populate time range endpoints and override formData with double underscored date options', () => {
  29. expect(
  30. extractExtras({
  31. ...baseQueryFormData,
  32. extra_filters: [
  33. {
  34. col: '__time_col',
  35. op: '==',
  36. val: 'ds2',
  37. },
  38. {
  39. col: '__time_grain',
  40. op: '==',
  41. val: 'PT5M',
  42. },
  43. {
  44. col: '__time_range',
  45. op: '==',
  46. val: '2009-07-17T00:00:00 : 2020-07-17T00:00:00',
  47. },
  48. ],
  49. }),
  50. ).toEqual({
  51. applied_time_extras: {
  52. __time_col: 'ds2',
  53. __time_grain: 'PT5M',
  54. __time_range: '2009-07-17T00:00:00 : 2020-07-17T00:00:00',
  55. },
  56. extras: {
  57. time_grain_sqla: 'PT5M',
  58. },
  59. filters: [],
  60. granularity: 'ds2',
  61. time_range: '2009-07-17T00:00:00 : 2020-07-17T00:00:00',
  62. });
  63. });
  64. it('should create regular filters from non-reserved columns', () => {
  65. expect(
  66. extractExtras({
  67. ...baseQueryFormData,
  68. extra_filters: [
  69. {
  70. col: 'gender',
  71. op: '==',
  72. val: 'girl',
  73. },
  74. {
  75. col: 'name',
  76. op: 'IN',
  77. val: ['Eve', 'Evelyn'],
  78. },
  79. ],
  80. }),
  81. ).toEqual({
  82. applied_time_extras: {},
  83. extras: {
  84. time_grain_sqla: 'PT1M',
  85. },
  86. filters: [
  87. {
  88. col: 'gender',
  89. op: '==',
  90. val: 'girl',
  91. },
  92. {
  93. col: 'name',
  94. op: 'IN',
  95. val: ['Eve', 'Evelyn'],
  96. },
  97. ],
  98. granularity: 'ds',
  99. });
  100. });
  101. it('should create regular filters from reserved and non-reserved columns', () => {
  102. expect(
  103. extractExtras({
  104. ...baseQueryFormData,
  105. extra_filters: [
  106. {
  107. col: 'gender',
  108. op: '==',
  109. val: 'girl',
  110. },
  111. {
  112. col: '__time_col',
  113. op: '==',
  114. val: 'ds2',
  115. },
  116. {
  117. col: '__time_grain',
  118. op: '==',
  119. val: 'PT5M',
  120. },
  121. {
  122. col: '__time_range',
  123. op: '==',
  124. val: '2009-07-17T00:00:00 : 2020-07-17T00:00:00',
  125. },
  126. ],
  127. }),
  128. ).toEqual({
  129. applied_time_extras: {
  130. __time_col: 'ds2',
  131. __time_grain: 'PT5M',
  132. __time_range: '2009-07-17T00:00:00 : 2020-07-17T00:00:00',
  133. },
  134. extras: {
  135. time_grain_sqla: 'PT5M',
  136. },
  137. filters: [
  138. {
  139. col: 'gender',
  140. op: '==',
  141. val: 'girl',
  142. },
  143. ],
  144. granularity: 'ds2',
  145. time_range: '2009-07-17T00:00:00 : 2020-07-17T00:00:00',
  146. });
  147. });
  148. });