templateValuesSrv-specs.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. define([
  2. 'mocks/dashboard-mock',
  3. './helpers',
  4. 'services/templateValuesSrv'
  5. ], function(dashboardMock, helpers) {
  6. 'use strict';
  7. describe('templateValuesSrv', function() {
  8. var ctx = new helpers.ServiceTestContext();
  9. beforeEach(module('grafana.services'));
  10. beforeEach(ctx.providePhase(['datasourceSrv', 'timeSrv', 'templateSrv']));
  11. beforeEach(ctx.createService('templateValuesSrv'));
  12. describe('update time period variable options', function() {
  13. var variable = { type: 'interval', query: 'auto,1s,2h,5h,1d', name: 'test' };
  14. beforeEach(function() {
  15. ctx.service.updateOptions(variable);
  16. });
  17. it('should update options array', function() {
  18. expect(variable.options.length).to.be(5);
  19. expect(variable.options[1].text).to.be('1s');
  20. expect(variable.options[1].value).to.be('1s');
  21. });
  22. });
  23. function describeUpdateVariable(desc, fn) {
  24. describe(desc, function() {
  25. var scenario = {};
  26. scenario.setup = function(setupFn) {
  27. scenario.setupFn = setupFn;
  28. };
  29. beforeEach(function() {
  30. scenario.setupFn();
  31. var ds = {};
  32. ds.metricFindQuery = sinon.stub().returns(ctx.$q.when(scenario.queryResult));
  33. ctx.datasourceSrv.get = sinon.stub().returns(ds);
  34. ctx.service.updateOptions(scenario.variable);
  35. ctx.$rootScope.$digest();
  36. });
  37. fn(scenario);
  38. });
  39. }
  40. describeUpdateVariable('time period variable ', function(scenario) {
  41. scenario.setup(function() {
  42. scenario.variable = { type: 'interval', query: 'auto,1s,2h,5h,1d', name: 'test' };
  43. });
  44. it('should update options array', function() {
  45. expect(scenario.variable.options.length).to.be(5);
  46. expect(scenario.variable.options[1].text).to.be('1s');
  47. expect(scenario.variable.options[1].value).to.be('1s');
  48. });
  49. });
  50. describeUpdateVariable('basic query variable', function(scenario) {
  51. scenario.setup(function() {
  52. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  53. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  54. });
  55. it('should update options array', function() {
  56. expect(scenario.variable.options.length).to.be(2);
  57. expect(scenario.variable.options[0].text).to.be('backend1');
  58. expect(scenario.variable.options[0].value).to.be('backend1');
  59. expect(scenario.variable.options[1].value).to.be('backend2');
  60. });
  61. it('should select first option as value', function() {
  62. expect(scenario.variable.current.value).to.be('backend1');
  63. });
  64. });
  65. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  66. scenario.setup(function() {
  67. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  68. scenario.variable.current = { value: 'backend2'};
  69. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  70. });
  71. it('should keep variable value', function() {
  72. expect(scenario.variable.current.value).to.be('backend2');
  73. });
  74. });
  75. describeUpdateVariable('and regex pattern exists', function(scenario) {
  76. scenario.setup(function() {
  77. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  78. scenario.variable.regex = '/apps.*(backend_[0-9]+)/';
  79. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  80. });
  81. it('should extract and use match group', function() {
  82. expect(scenario.variable.options[0].value).to.be('backend_01');
  83. });
  84. });
  85. describeUpdateVariable('and regex pattern exists and no match', function(scenario) {
  86. scenario.setup(function() {
  87. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  88. scenario.variable.regex = '/apps.*(backendasd[0-9]+)/';
  89. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  90. });
  91. it('should not add non matching items', function() {
  92. expect(scenario.variable.options.length).to.be(0);
  93. });
  94. });
  95. describeUpdateVariable('regex pattern without slashes', function(scenario) {
  96. scenario.setup(function() {
  97. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  98. scenario.variable.regex = 'backend_01';
  99. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_02.counters.req'}];
  100. });
  101. it('should return matches options', function() {
  102. expect(scenario.variable.options.length).to.be(1);
  103. });
  104. });
  105. describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
  106. scenario.setup(function() {
  107. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  108. scenario.variable.regex = 'backend_01';
  109. scenario.queryResult = [{text: 'apps.backend.backend_01.counters.req'}, {text: 'apps.backend.backend_01.counters.req'}];
  110. });
  111. it('should return matches options', function() {
  112. expect(scenario.variable.options.length).to.be(1);
  113. });
  114. });
  115. describeUpdateVariable('and existing value still exists in options', function(scenario) {
  116. scenario.setup(function() {
  117. scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
  118. scenario.variable.current = { value: 'backend2'};
  119. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}];
  120. });
  121. it('should keep variable value', function() {
  122. expect(scenario.variable.current.value).to.be('backend2');
  123. });
  124. });
  125. describeUpdateVariable('with include All glob syntax', function(scenario) {
  126. scenario.setup(function() {
  127. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  128. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  129. });
  130. it('should add All Glob option', function() {
  131. expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
  132. });
  133. });
  134. describeUpdateVariable('with include all wildcard', function(scenario) {
  135. scenario.setup(function() {
  136. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  137. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  138. });
  139. it('should add All wildcard option', function() {
  140. expect(scenario.variable.options[0].value).to.be('*');
  141. });
  142. });
  143. describeUpdateVariable('with include all wildcard', function(scenario) {
  144. scenario.setup(function() {
  145. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
  146. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  147. });
  148. it('should add All wildcard option', function() {
  149. expect(scenario.variable.options[0].value).to.be('.*');
  150. });
  151. });
  152. describeUpdateVariable('with include all regex values', function(scenario) {
  153. scenario.setup(function() {
  154. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
  155. scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
  156. });
  157. it('should add All wildcard option', function() {
  158. expect(scenario.variable.options[0].value).to.be('*');
  159. });
  160. });
  161. describeUpdateVariable('with include all glob no values', function(scenario) {
  162. scenario.setup(function() {
  163. scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
  164. scenario.queryResult = [];
  165. });
  166. it('should add empty glob', function() {
  167. expect(scenario.variable.options[0].value).to.be('{}');
  168. });
  169. });
  170. });
  171. });