query_builder_specs.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 field query with regexp', function() {
  76. var builder = new InfluxQueryBuilder({measurement: '/$var/', tags: [{key: 'app', value: 'email'}]});
  77. var query = builder.buildExploreQuery('FIELDS');
  78. expect(query).to.be('SHOW FIELD KEYS FROM /$var/');
  79. });
  80. it('should build show retention policies query', function() {
  81. var builder = new InfluxQueryBuilder({measurement: 'cpu', tags: []}, 'site');
  82. var query = builder.buildExploreQuery('RETENTION POLICIES');
  83. expect(query).to.be('SHOW RETENTION POLICIES on "site"');
  84. });
  85. });
  86. });