datasource_srv.test.ts 2.8 KB

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