extractTimegrain.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 { extractTimegrain, QueryFormData } from '@superset-ui/core';
  20. describe('extractTimegrain', () => {
  21. const baseFormData: QueryFormData = {
  22. datasource: 'table__1',
  23. viz_type: 'my_viz',
  24. };
  25. it('should extract regular from form data', () => {
  26. expect(
  27. extractTimegrain({
  28. ...baseFormData,
  29. time_grain_sqla: 'P1D',
  30. }),
  31. ).toEqual('P1D');
  32. });
  33. it('should extract filter time grain from form data', () => {
  34. expect(
  35. extractTimegrain({
  36. ...baseFormData,
  37. time_grain_sqla: 'P1D',
  38. extra_filters: [
  39. {
  40. col: '__time_grain',
  41. op: '==',
  42. val: 'P1M',
  43. },
  44. ],
  45. }),
  46. ).toEqual('P1M');
  47. });
  48. it('should extract native filter time grain from form data', () => {
  49. expect(
  50. extractTimegrain({
  51. ...baseFormData,
  52. time_grain_sqla: 'P1D',
  53. extra_form_data: {
  54. time_grain_sqla: 'P1W',
  55. },
  56. }),
  57. ).toEqual('P1W');
  58. });
  59. it('should give priority to native filters', () => {
  60. expect(
  61. extractTimegrain({
  62. ...baseFormData,
  63. time_grain_sqla: 'P1D',
  64. extra_filters: [
  65. {
  66. col: '__time_grain',
  67. op: '==',
  68. val: 'P1M',
  69. },
  70. ],
  71. extra_form_data: {
  72. time_grain_sqla: 'P1W',
  73. },
  74. }),
  75. ).toEqual('P1W');
  76. });
  77. it('returns undefined if timegrain not defined', () => {
  78. expect(extractTimegrain({ ...baseFormData })).toEqual(undefined);
  79. });
  80. });