datasource.test.ts 3.5 KB

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