datasource_srv.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. type: 'b',
  24. name: 'buildIn',
  25. meta: { builtIn: true } as PluginMeta,
  26. jsonData: {},
  27. },
  28. nonBuildIn: {
  29. type: 'e',
  30. name: 'external1',
  31. meta: { builtIn: false } as PluginMeta,
  32. jsonData: {},
  33. },
  34. nonExplore: {
  35. type: 'e2',
  36. name: 'external2',
  37. meta: {} as PluginMeta,
  38. jsonData: {},
  39. },
  40. };
  41. });
  42. it('should return list of explore sources', () => {
  43. const externalSources = _datasourceSrv.getExternal();
  44. expect(externalSources.length).toBe(2);
  45. expect(externalSources[0].name).toBe('external1');
  46. expect(externalSources[1].name).toBe('external2');
  47. });
  48. });
  49. describe('when loading metric sources', () => {
  50. let metricSources;
  51. const unsortedDatasources = {
  52. mmm: {
  53. type: 'test-db',
  54. meta: { metrics: { m: 1 } },
  55. },
  56. '--Grafana--': {
  57. type: 'grafana',
  58. meta: { builtIn: true, metrics: { m: 1 }, id: 'grafana' },
  59. },
  60. '--Mixed--': {
  61. type: 'test-db',
  62. meta: { builtIn: true, metrics: { m: 1 }, id: 'mixed' },
  63. },
  64. ZZZ: {
  65. type: 'test-db',
  66. meta: { metrics: { m: 1 } },
  67. },
  68. aaa: {
  69. type: 'test-db',
  70. meta: { metrics: { m: 1 } },
  71. },
  72. BBB: {
  73. type: 'test-db',
  74. meta: { metrics: { m: 1 } },
  75. },
  76. };
  77. beforeEach(() => {
  78. config.datasources = unsortedDatasources as any;
  79. metricSources = _datasourceSrv.getMetricSources({});
  80. config.defaultDatasource = 'BBB';
  81. });
  82. it('should return a list of sources sorted case insensitively with builtin sources last', () => {
  83. expect(metricSources[1].name).toBe('aaa');
  84. expect(metricSources[2].name).toBe('BBB');
  85. expect(metricSources[3].name).toBe('mmm');
  86. expect(metricSources[4].name).toBe('ZZZ');
  87. expect(metricSources[5].name).toBe('--Grafana--');
  88. expect(metricSources[6].name).toBe('--Mixed--');
  89. });
  90. it('should set default data source', () => {
  91. expect(metricSources[3].name).toBe('default');
  92. expect(metricSources[3].sort).toBe('BBB');
  93. });
  94. it('should set default inject the variable datasources', () => {
  95. expect(metricSources[0].name).toBe('$datasource');
  96. expect(metricSources[0].sort).toBe('$datasource');
  97. });
  98. });
  99. });