defineSavedMetrics.test.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 {
  20. DatasourceType,
  21. DEFAULT_METRICS,
  22. QueryResponse,
  23. testQuery,
  24. } from '@superset-ui/core';
  25. import { defineSavedMetrics } from '@superset-ui/chart-controls';
  26. describe('defineSavedMetrics', () => {
  27. it('defines saved metrics if source is a Dataset', () => {
  28. const dataset = {
  29. id: 1,
  30. metrics: [
  31. {
  32. metric_name: 'COUNT(*) non-default-dataset-metric',
  33. expression: 'COUNT(*) non-default-dataset-metric',
  34. uuid: '1',
  35. },
  36. ],
  37. type: DatasourceType.Table,
  38. main_dttm_col: 'test',
  39. time_grain_sqla: [],
  40. columns: [],
  41. verbose_map: {},
  42. column_formats: {},
  43. datasource_name: 'my_datasource',
  44. description: 'this is my datasource',
  45. };
  46. expect(defineSavedMetrics(dataset)).toEqual([
  47. {
  48. metric_name: 'COUNT(*) non-default-dataset-metric',
  49. expression: 'COUNT(*) non-default-dataset-metric',
  50. uuid: '1',
  51. },
  52. ]);
  53. // @ts-ignore
  54. expect(defineSavedMetrics({ ...dataset, metrics: undefined })).toEqual([]);
  55. });
  56. it('returns default saved metrics if source is a Query', () => {
  57. expect(defineSavedMetrics(testQuery as QueryResponse)).toEqual(
  58. DEFAULT_METRICS,
  59. );
  60. });
  61. });