datasource.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import InfluxDatasource from '../datasource';
  2. import $q from 'q';
  3. import { TemplateSrvStub } from 'test/specs/helpers';
  4. describe('InfluxDataSource', () => {
  5. const ctx: any = {
  6. backendSrv: {},
  7. $q: $q,
  8. templateSrv: new TemplateSrvStub(),
  9. instanceSettings: { url: 'url', name: 'influxDb', jsonData: { httpMode: 'GET' } },
  10. };
  11. beforeEach(() => {
  12. ctx.instanceSettings.url = '/api/datasources/proxy/1';
  13. ctx.ds = new InfluxDatasource(ctx.instanceSettings, ctx.$q, ctx.backendSrv, ctx.templateSrv);
  14. });
  15. describe('When issuing metricFindQuery', () => {
  16. const query = 'SELECT max(value) FROM measurement WHERE $timeFilter';
  17. const queryOptions: any = {
  18. range: {
  19. from: '2018-01-01T00:00:00Z',
  20. to: '2018-01-02T00:00:00Z',
  21. },
  22. };
  23. let requestQuery, requestMethod, requestData;
  24. beforeEach(async () => {
  25. ctx.backendSrv.datasourceRequest = req => {
  26. requestMethod = req.method;
  27. requestQuery = req.params.q;
  28. requestData = req.data;
  29. return ctx.$q.when({
  30. results: [
  31. {
  32. series: [
  33. {
  34. name: 'measurement',
  35. columns: ['max'],
  36. values: [[1]],
  37. },
  38. ],
  39. },
  40. ],
  41. });
  42. };
  43. await ctx.ds.metricFindQuery(query, queryOptions).then(_ => {});
  44. });
  45. it('should replace $timefilter', () => {
  46. expect(requestQuery).toMatch('time >= 1514764800000ms and time <= 1514851200000ms');
  47. });
  48. it('should use the HTTP GET method', () => {
  49. expect(requestMethod).toBe('GET');
  50. });
  51. it('should not have any data in request body', () => {
  52. expect(requestData).toBeNull();
  53. });
  54. });
  55. });
  56. describe('InfluxDataSource in POST query mode', () => {
  57. const ctx: any = {
  58. backendSrv: {},
  59. $q: $q,
  60. templateSrv: new TemplateSrvStub(),
  61. instanceSettings: { url: 'url', name: 'influxDb', jsonData: { httpMode: 'POST' } },
  62. };
  63. beforeEach(() => {
  64. ctx.instanceSettings.url = '/api/datasources/proxy/1';
  65. ctx.ds = new InfluxDatasource(ctx.instanceSettings, ctx.$q, ctx.backendSrv, ctx.templateSrv);
  66. });
  67. describe('When issuing metricFindQuery', () => {
  68. const query = 'SELECT max(value) FROM measurement';
  69. const queryOptions: any = {};
  70. let requestMethod, requestQueryParameter, queryEncoded, requestQuery;
  71. beforeEach(async () => {
  72. ctx.backendSrv.datasourceRequest = req => {
  73. requestMethod = req.method;
  74. requestQueryParameter = req.params;
  75. requestQuery = req.data;
  76. return ctx.$q.when({
  77. results: [
  78. {
  79. series: [
  80. {
  81. name: 'measurement',
  82. columns: ['max'],
  83. values: [[1]],
  84. },
  85. ],
  86. },
  87. ],
  88. });
  89. };
  90. queryEncoded = await ctx.ds.serializeParams({ q: query });
  91. await ctx.ds.metricFindQuery(query, queryOptions).then(_ => {});
  92. });
  93. it('should have the query form urlencoded', () => {
  94. expect(requestQuery).toBe(queryEncoded);
  95. });
  96. it('should use the HTTP POST method', () => {
  97. expect(requestMethod).toBe('POST');
  98. });
  99. it('should not have q as a query parameter', () => {
  100. expect(requestQueryParameter).not.toHaveProperty('q');
  101. });
  102. });
  103. });