datasource_specs.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import moment from 'moment';
  3. import helpers from 'test/specs/helpers';
  4. import {MysqlDatasource} from '../datasource';
  5. describe('MySQLDatasource', function() {
  6. var ctx = new helpers.ServiceTestContext();
  7. var instanceSettings = {name: 'mysql'};
  8. beforeEach(angularMocks.module('grafana.core'));
  9. beforeEach(angularMocks.module('grafana.services'));
  10. beforeEach(ctx.providePhase(['backendSrv']));
  11. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  12. ctx.$q = $q;
  13. ctx.$httpBackend = $httpBackend;
  14. ctx.$rootScope = $rootScope;
  15. ctx.ds = $injector.instantiate(MysqlDatasource, {instanceSettings: instanceSettings});
  16. $httpBackend.when('GET', /\.html$/).respond('');
  17. }));
  18. describe('When performing annotationQuery', function() {
  19. let results;
  20. const annotationName = 'MyAnno';
  21. const options = {
  22. annotation: {
  23. name: annotationName,
  24. rawQuery: 'select time_sec, title, text, tags from table;'
  25. },
  26. range: {
  27. from: moment(1432288354),
  28. to: moment(1432288401)
  29. }
  30. };
  31. const response = {
  32. results: {
  33. MyAnno: {
  34. refId: annotationName,
  35. tables: [
  36. {
  37. columns: [{text: 'time_sec'}, {text: 'title'}, {text: 'text'}, {text: 'tags'}],
  38. rows: [
  39. [1432288355, 'aTitle', 'some text', 'TagA,TagB'],
  40. [1432288390, 'aTitle2', 'some text2', ' TagB , TagC'],
  41. [1432288400, 'aTitle3', 'some text3']
  42. ]
  43. }
  44. ]
  45. }
  46. }
  47. };
  48. beforeEach(function() {
  49. ctx.backendSrv.datasourceRequest = function(options) {
  50. return ctx.$q.when({data: response, status: 200});
  51. };
  52. ctx.ds.annotationQuery(options).then(function(data) { results = data; });
  53. ctx.$rootScope.$apply();
  54. });
  55. it('should return annotation list', function() {
  56. expect(results.length).to.be(3);
  57. expect(results[0].title).to.be('aTitle');
  58. expect(results[0].text).to.be('some text');
  59. expect(results[0].tags[0]).to.be('TagA');
  60. expect(results[0].tags[1]).to.be('TagB');
  61. expect(results[1].tags[0]).to.be('TagB');
  62. expect(results[1].tags[1]).to.be('TagC');
  63. expect(results[2].tags.length).to.be(0);
  64. });
  65. });
  66. });