datasource_spec.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {Datasource} from "../module";
  2. import Q from "q";
  3. describe('GenericDatasource', function() {
  4. var ctx = {};
  5. beforeEach(function() {
  6. ctx.$q = Q;
  7. ctx.backendSrv = {};
  8. ctx.templateSrv = {};
  9. ctx.ds = new Datasource({}, ctx.$q, ctx.backendSrv, ctx.templateSrv);
  10. });
  11. it('should return an empty array when no targets are set', function(done) {
  12. ctx.ds.query({targets: []}).then(function(result) {
  13. expect(result.data).to.have.length(0);
  14. done();
  15. });
  16. });
  17. it('should return the server results when a target is set', function(done) {
  18. ctx.backendSrv.datasourceRequest = function(request) {
  19. return ctx.$q.when({
  20. _request: request,
  21. data: [
  22. {
  23. target: 'X',
  24. datapoints: [1, 2, 3]
  25. }
  26. ]
  27. });
  28. };
  29. ctx.templateSrv.replace = function(data) {
  30. return data;
  31. }
  32. ctx.ds.query({targets: ['hits']}).then(function(result) {
  33. expect(result._request.data.targets).to.have.length(1);
  34. var series = result.data[0];
  35. expect(series.target).to.equal('X');
  36. expect(series.datapoints).to.have.length(3);
  37. done();
  38. });
  39. });
  40. it ('should return the metric results when a target is null', function(done) {
  41. ctx.backendSrv.datasourceRequest = function(request) {
  42. return ctx.$q.when({
  43. _request: request,
  44. data: [
  45. "metric_0",
  46. "metric_1",
  47. "metric_2",
  48. ]
  49. });
  50. };
  51. ctx.templateSrv.replace = function(data) {
  52. return data;
  53. }
  54. ctx.ds.metricFindQuery({target: null}).then(function(result) {
  55. expect(result).to.have.length(3);
  56. expect(result[0].text).to.equal('metric_0');
  57. expect(result[0].value).to.equal('metric_0');
  58. expect(result[1].text).to.equal('metric_1');
  59. expect(result[1].value).to.equal('metric_1');
  60. expect(result[2].text).to.equal('metric_2');
  61. expect(result[2].value).to.equal('metric_2');
  62. done();
  63. });
  64. });
  65. it ('should return the metric target results when a target is set', function(done) {
  66. ctx.backendSrv.datasourceRequest = function(request) {
  67. var target = request.data.target;
  68. var result = [target + "_0", target + "_1", target + "_2"];
  69. return ctx.$q.when({
  70. _request: request,
  71. data: result
  72. });
  73. };
  74. ctx.templateSrv.replace = function(data) {
  75. return data;
  76. }
  77. ctx.ds.metricFindQuery('search').then(function(result) {
  78. expect(result).to.have.length(3);
  79. expect(result[0].text).to.equal('search_0');
  80. expect(result[0].value).to.equal('search_0');
  81. expect(result[1].text).to.equal('search_1');
  82. expect(result[1].value).to.equal('search_1');
  83. expect(result[2].text).to.equal('search_2');
  84. expect(result[2].value).to.equal('search_2');
  85. done();
  86. });
  87. });
  88. it ('should return the metric results when the target is an empty string', function(done) {
  89. ctx.backendSrv.datasourceRequest = function(request) {
  90. return ctx.$q.when({
  91. _request: request,
  92. data: [
  93. "metric_0",
  94. "metric_1",
  95. "metric_2",
  96. ]
  97. });
  98. };
  99. ctx.templateSrv.replace = function(data) {
  100. return data;
  101. }
  102. ctx.ds.metricFindQuery('').then(function(result) {
  103. expect(result).to.have.length(3);
  104. expect(result[0].text).to.equal('metric_0');
  105. expect(result[0].value).to.equal('metric_0');
  106. expect(result[1].text).to.equal('metric_1');
  107. expect(result[1].value).to.equal('metric_1');
  108. expect(result[2].text).to.equal('metric_2');
  109. expect(result[2].value).to.equal('metric_2');
  110. done();
  111. });
  112. });
  113. it ('should return the metric results when the args are an empty object', function(done) {
  114. ctx.backendSrv.datasourceRequest = function(request) {
  115. return ctx.$q.when({
  116. _request: request,
  117. data: [
  118. "metric_0",
  119. "metric_1",
  120. "metric_2",
  121. ]
  122. });
  123. };
  124. ctx.templateSrv.replace = function(data) {
  125. return data;
  126. }
  127. ctx.ds.metricFindQuery().then(function(result) {
  128. expect(result).to.have.length(3);
  129. expect(result[0].text).to.equal('metric_0');
  130. expect(result[0].value).to.equal('metric_0');
  131. expect(result[1].text).to.equal('metric_1');
  132. expect(result[1].value).to.equal('metric_1');
  133. expect(result[2].text).to.equal('metric_2');
  134. expect(result[2].value).to.equal('metric_2');
  135. done();
  136. });
  137. });
  138. it ('should return the metric target results when the args are a string', function(done) {
  139. ctx.backendSrv.datasourceRequest = function(request) {
  140. var target = request.data.target;
  141. var result = [target + "_0", target + "_1", target + "_2"];
  142. return ctx.$q.when({
  143. _request: request,
  144. data: result
  145. });
  146. };
  147. ctx.templateSrv.replace = function(data) {
  148. return data;
  149. }
  150. ctx.ds.metricFindQuery('search').then(function(result) {
  151. expect(result).to.have.length(3);
  152. expect(result[0].text).to.equal('search_0');
  153. expect(result[0].value).to.equal('search_0');
  154. expect(result[1].text).to.equal('search_1');
  155. expect(result[1].value).to.equal('search_1');
  156. expect(result[2].text).to.equal('search_2');
  157. expect(result[2].value).to.equal('search_2');
  158. done();
  159. });
  160. });
  161. it ('should return data as text and as value', function(done) {
  162. var result = ctx.ds.mapToTextValue({data: ["zero", "one", "two"]});
  163. expect(result).to.have.length(3);
  164. expect(result[0].text).to.equal('zero');
  165. expect(result[0].value).to.equal('zero');
  166. expect(result[1].text).to.equal('one');
  167. expect(result[1].value).to.equal('one');
  168. expect(result[2].text).to.equal('two');
  169. expect(result[2].value).to.equal('two');
  170. done();
  171. });
  172. it ('should return text as text and value as value', function(done) {
  173. var data = [
  174. {text: "zero", value: "value_0"},
  175. {text: "one", value: "value_1"},
  176. {text: "two", value: "value_2"},
  177. ];
  178. var result = ctx.ds.mapToTextValue({data: data});
  179. expect(result).to.have.length(3);
  180. expect(result[0].text).to.equal('zero');
  181. expect(result[0].value).to.equal('value_0');
  182. expect(result[1].text).to.equal('one');
  183. expect(result[1].value).to.equal('value_1');
  184. expect(result[2].text).to.equal('two');
  185. expect(result[2].value).to.equal('value_2');
  186. done();
  187. });
  188. it ('should return data as text and index as value', function(done) {
  189. var data = [
  190. {a: "zero", b: "value_0"},
  191. {a: "one", b: "value_1"},
  192. {a: "two", b: "value_2"},
  193. ];
  194. var result = ctx.ds.mapToTextValue({data: data});
  195. expect(result).to.have.length(3);
  196. expect(result[0].text).to.equal(data[0]);
  197. expect(result[0].value).to.equal(0);
  198. expect(result[1].text).to.equal(data[1]);
  199. expect(result[1].value).to.equal(1);
  200. expect(result[2].text).to.equal(data[2]);
  201. expect(result[2].value).to.equal(2);
  202. done();
  203. });
  204. it('should support tag keys', function(done) {
  205. var data = [{'type': 'string', 'text': 'One', 'key': 'one'}, {'type': 'string', 'text': 'two', 'key': 'Two'}];
  206. ctx.backendSrv.datasourceRequest = function(request) {
  207. return ctx.$q.when({
  208. _request: request,
  209. data: data
  210. });
  211. };
  212. ctx.ds.getTagKeys().then(function(result) {
  213. expect(result).to.have.length(2);
  214. expect(result[0].type).to.equal(data[0].type);
  215. expect(result[0].text).to.equal(data[0].text);
  216. expect(result[0].key).to.equal(data[0].key);
  217. expect(result[1].type).to.equal(data[1].type);
  218. expect(result[1].text).to.equal(data[1].text);
  219. expect(result[1].key).to.equal(data[1].key);
  220. done();
  221. });
  222. });
  223. it('should support tag values', function(done) {
  224. var data = [{'key': 'eins', 'text': 'Eins!'}, {'key': 'zwei', 'text': 'Zwei'}, {'key': 'drei', 'text': 'Drei!'}];
  225. ctx.backendSrv.datasourceRequest = function(request) {
  226. return ctx.$q.when({
  227. _request: request,
  228. data: data
  229. });
  230. };
  231. ctx.ds.getTagValues().then(function(result) {
  232. expect(result).to.have.length(3);
  233. expect(result[0].text).to.equal(data[0].text);
  234. expect(result[0].key).to.equal(data[0].key);
  235. expect(result[1].text).to.equal(data[1].text);
  236. expect(result[1].key).to.equal(data[1].key);
  237. expect(result[2].text).to.equal(data[2].text);
  238. expect(result[2].key).to.equal(data[2].key);
  239. done();
  240. });
  241. });
  242. });