query_builder_specs.ts 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
  2. import InfluxQueryBuilder from '../query_builder';
  3. describe('InfluxQueryBuilder', function() {
  4. describe('when building explore queries', function() {
  5. it('should only have measurement condition in tag keys query given query with measurement', function() {
  6. var builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] });
  7. var query = builder.buildExploreQuery('TAG_KEYS');
  8. expect(query).to.be('SHOW TAG KEYS FROM "cpu"');
  9. });
  10. it('should handle regex measurement in tag keys query', function() {
  11. var builder = new InfluxQueryBuilder({
  12. measurement: '/.*/', tags: []
  13. });
  14. var query = builder.buildExploreQuery('TAG_KEYS');
  15. expect(query).to.be('SHOW TAG KEYS FROM /.*/');
  16. });
  17. it('should have no conditions in tags keys query given query with no measurement or tag', function() {
  18. var builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  19. var query = builder.buildExploreQuery('TAG_KEYS');
  20. expect(query).to.be('SHOW TAG KEYS');
  21. });
  22. it('should have where condition in tag keys query with tags', function() {
  23. var builder = new InfluxQueryBuilder({ measurement: '', tags: [{key: 'host', value: 'se1'}] });
  24. var query = builder.buildExploreQuery('TAG_KEYS');
  25. expect(query).to.be("SHOW TAG KEYS WHERE \"host\" = 'se1'");
  26. });
  27. it('should have no conditions in measurement query for query with no tags', function() {
  28. var builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  29. var query = builder.buildExploreQuery('MEASUREMENTS');
  30. expect(query).to.be('SHOW MEASUREMENTS');
  31. });
  32. it('should have no conditions in measurement query for query with no tags and empty query', function() {
  33. var builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  34. var query = builder.buildExploreQuery('MEASUREMENTS', undefined, '');
  35. expect(query).to.be('SHOW MEASUREMENTS');
  36. });
  37. it('should have WITH MEASUREMENT in measurement query for non-empty query with no tags', function() {
  38. var builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  39. var query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  40. expect(query).to.be('SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/');
  41. });
  42. it('should have WITH MEASUREMENT WHERE in measurement query for non-empty query with tags', function() {
  43. var builder = new InfluxQueryBuilder({ measurement: '', tags: [{key: 'app', value: 'email'}] });
  44. var query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  45. expect(query).to.be("SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ WHERE \"app\" = 'email'");
  46. });
  47. it('should have where condition in measurement query for query with tags', function() {
  48. var builder = new InfluxQueryBuilder({measurement: '', tags: [{key: 'app', value: 'email'}]});
  49. var query = builder.buildExploreQuery('MEASUREMENTS');
  50. expect(query).to.be("SHOW MEASUREMENTS WHERE \"app\" = 'email'");
  51. });
  52. it('should have where tag name IN filter in tag values query for query with one tag', function() {
  53. var builder = new InfluxQueryBuilder({measurement: '', tags: [{key: 'app', value: 'asdsadsad'}]});
  54. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  55. expect(query).to.be('SHOW TAG VALUES WITH KEY = "app"');
  56. });
  57. it('should have measurement tag condition and tag name IN filter in tag values query', function() {
  58. var builder = new InfluxQueryBuilder({measurement: 'cpu', tags: [{key: 'app', value: 'email'}, {key: 'host', value: 'server1'}]});
  59. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  60. expect(query).to.be('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  61. });
  62. it('should switch to regex operator in tag condition', function() {
  63. var builder = new InfluxQueryBuilder({
  64. measurement: 'cpu',
  65. tags: [{key: 'host', value: '/server.*/'}]
  66. });
  67. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  68. expect(query).to.be('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" =~ /server.*/');
  69. });
  70. it('should build show field query', function() {
  71. var builder = new InfluxQueryBuilder({measurement: 'cpu', tags: [{key: 'app', value: 'email'}]});
  72. var query = builder.buildExploreQuery('FIELDS');
  73. expect(query).to.be('SHOW FIELD KEYS FROM "cpu"');
  74. });
  75. it('should build show retention policies query', function() {
  76. var builder = new InfluxQueryBuilder({measurement: 'cpu', tags: []}, 'site');
  77. var query = builder.buildExploreQuery('RETENTION POLICIES');
  78. expect(query).to.be('SHOW RETENTION POLICIES on "site"');
  79. });
  80. });
  81. });