columnChoices.test.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 { DatasourceType, testQueryResponse } from '@superset-ui/core';
  20. import { GenericDataType } from '@apache-superset/core/api/core';
  21. import { columnChoices } from '../../src';
  22. describe('columnChoices()', () => {
  23. it('should convert columns to choices when source is a Dataset', () => {
  24. expect(
  25. columnChoices({
  26. id: 1,
  27. metrics: [],
  28. type: DatasourceType.Table,
  29. main_dttm_col: 'test',
  30. time_grain_sqla: [],
  31. columns: [
  32. {
  33. column_name: 'fiz',
  34. type: 'INT',
  35. type_generic: GenericDataType.Numeric,
  36. },
  37. {
  38. column_name: 'about',
  39. verbose_name: 'right',
  40. type: 'VARCHAR',
  41. type_generic: GenericDataType.String,
  42. },
  43. {
  44. column_name: 'foo',
  45. verbose_name: undefined,
  46. type: 'TIMESTAMP',
  47. type_generic: GenericDataType.Temporal,
  48. },
  49. ],
  50. verbose_map: {},
  51. column_formats: { fiz: 'NUMERIC', about: 'STRING', foo: 'DATE' },
  52. datasource_name: 'my_datasource',
  53. description: 'this is my datasource',
  54. }),
  55. ).toEqual([
  56. ['fiz', 'fiz'],
  57. ['foo', 'foo'],
  58. ['about', 'right'],
  59. ]);
  60. });
  61. it('should return empty array when no columns', () => {
  62. expect(columnChoices(undefined)).toEqual([]);
  63. });
  64. it('should convert columns to choices when source is a Query', () => {
  65. expect(columnChoices(testQueryResponse)).toEqual([
  66. ['Column 1', 'Column 1'],
  67. ['Column 2', 'Column 2'],
  68. ['Column 3', 'Column 3'],
  69. ]);
  70. });
  71. it('should return choices of a specific type', () => {
  72. expect(columnChoices(testQueryResponse, GenericDataType.Temporal)).toEqual([
  73. ['Column 2', 'Column 2'],
  74. ]);
  75. });
  76. it('should use name when verbose_name key exists but is not defined', () => {
  77. expect(
  78. columnChoices({
  79. id: 1,
  80. metrics: [],
  81. type: DatasourceType.Table,
  82. main_dttm_col: 'test',
  83. time_grain_sqla: [],
  84. columns: [
  85. {
  86. column_name: 'foo',
  87. verbose_name: null,
  88. type: 'VARCHAR',
  89. type_generic: GenericDataType.String,
  90. },
  91. {
  92. column_name: 'bar',
  93. verbose_name: null,
  94. type: 'VARCHAR',
  95. type_generic: GenericDataType.String,
  96. },
  97. ],
  98. verbose_map: {},
  99. column_formats: { fiz: 'NUMERIC', about: 'STRING', foo: 'DATE' },
  100. datasource_name: 'my_datasource',
  101. description: 'this is my datasource',
  102. }),
  103. ).toEqual([
  104. ['bar', 'bar'],
  105. ['foo', 'foo'],
  106. ]);
  107. });
  108. });