datasource_srv.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import config from 'app/core/config';
  2. import 'app/features/plugins/datasource_srv';
  3. import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
  4. import { PluginMeta } from '@grafana/ui/src/types';
  5. // Datasource variable $datasource with current value 'BBB'
  6. const templateSrv = {
  7. variables: [
  8. {
  9. type: 'datasource',
  10. name: 'datasource',
  11. current: {
  12. value: 'BBB',
  13. },
  14. },
  15. ],
  16. };
  17. describe('datasource_srv', () => {
  18. const _datasourceSrv = new DatasourceSrv({}, {}, {}, templateSrv);
  19. describe('when loading external datasources', () => {
  20. beforeEach(() => {
  21. config.datasources = {
  22. buildInDs: {
  23. id: 1,
  24. type: 'b',
  25. name: 'buildIn',
  26. meta: { builtIn: true } as PluginMeta,
  27. jsonData: {},
  28. },
  29. nonBuildIn: {
  30. id: 2,
  31. type: 'e',
  32. name: 'external1',
  33. meta: { builtIn: false } as PluginMeta,
  34. jsonData: {},
  35. },
  36. nonExplore: {
  37. id: 3,
  38. type: 'e2',
  39. name: 'external2',
  40. meta: {} as PluginMeta,
  41. jsonData: {},
  42. },
  43. };
  44. });
  45. it('should return list of explore sources', () => {
  46. const externalSources = _datasourceSrv.getExternal();
  47. expect(externalSources.length).toBe(2);
  48. expect(externalSources[0].name).toBe('external1');
  49. expect(externalSources[1].name).toBe('external2');
  50. });
  51. });
  52. describe('when loading metric sources', () => {
  53. let metricSources;
  54. const unsortedDatasources = {
  55. mmm: {
  56. type: 'test-db',
  57. meta: { metrics: { m: 1 } },
  58. },
  59. '--Grafana--': {
  60. type: 'grafana',
  61. meta: { builtIn: true, metrics: { m: 1 }, id: 'grafana' },
  62. },
  63. '--Mixed--': {
  64. type: 'test-db',
  65. meta: { builtIn: true, metrics: { m: 1 }, id: 'mixed' },
  66. },
  67. ZZZ: {
  68. type: 'test-db',
  69. meta: { metrics: { m: 1 } },
  70. },
  71. aaa: {
  72. type: 'test-db',
  73. meta: { metrics: { m: 1 } },
  74. },
  75. BBB: {
  76. type: 'test-db',
  77. meta: { metrics: { m: 1 } },
  78. },
  79. };
  80. beforeEach(() => {
  81. config.datasources = unsortedDatasources as any;
  82. metricSources = _datasourceSrv.getMetricSources({});
  83. config.defaultDatasource = 'BBB';
  84. });
  85. it('should return a list of sources sorted case insensitively with builtin sources last', () => {
  86. expect(metricSources[1].name).toBe('aaa');
  87. expect(metricSources[2].name).toBe('BBB');
  88. expect(metricSources[3].name).toBe('mmm');
  89. expect(metricSources[4].name).toBe('ZZZ');
  90. expect(metricSources[5].name).toBe('--Grafana--');
  91. expect(metricSources[6].name).toBe('--Mixed--');
  92. });
  93. it('should set default data source', () => {
  94. expect(metricSources[3].name).toBe('default');
  95. expect(metricSources[3].sort).toBe('BBB');
  96. });
  97. it('should set default inject the variable datasources', () => {
  98. expect(metricSources[0].name).toBe('$datasource');
  99. expect(metricSources[0].sort).toBe('$datasource');
  100. });
  101. });
  102. });