query_builder.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { InfluxQueryBuilder } from '../query_builder';
  2. describe('InfluxQueryBuilder', () => {
  3. describe('when building explore queries', () => {
  4. it('should only have measurement condition in tag keys query given query with measurement', () => {
  5. const builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] });
  6. const query = builder.buildExploreQuery('TAG_KEYS');
  7. expect(query).toBe('SHOW TAG KEYS FROM "cpu"');
  8. });
  9. it('should handle regex measurement in tag keys query', () => {
  10. const builder = new InfluxQueryBuilder({
  11. measurement: '/.*/',
  12. tags: [],
  13. });
  14. const query = builder.buildExploreQuery('TAG_KEYS');
  15. expect(query).toBe('SHOW TAG KEYS FROM /.*/');
  16. });
  17. it('should have no conditions in tags keys query given query with no measurement or tag', () => {
  18. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  19. const query = builder.buildExploreQuery('TAG_KEYS');
  20. expect(query).toBe('SHOW TAG KEYS');
  21. });
  22. it('should have where condition in tag keys query with tags', () => {
  23. const builder = new InfluxQueryBuilder({
  24. measurement: '',
  25. tags: [{ key: 'host', value: 'se1' }],
  26. });
  27. const query = builder.buildExploreQuery('TAG_KEYS');
  28. expect(query).toBe('SHOW TAG KEYS WHERE "host" = \'se1\'');
  29. });
  30. it('should have no conditions in measurement query for query with no tags', () => {
  31. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  32. const query = builder.buildExploreQuery('MEASUREMENTS');
  33. expect(query).toBe('SHOW MEASUREMENTS LIMIT 100');
  34. });
  35. it('should have no conditions in measurement query for query with no tags and empty query', () => {
  36. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  37. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, '');
  38. expect(query).toBe('SHOW MEASUREMENTS LIMIT 100');
  39. });
  40. it('should have WITH MEASUREMENT in measurement query for non-empty query with no tags', () => {
  41. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  42. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  43. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ LIMIT 100');
  44. });
  45. it('should escape the regex value in measurement query', () => {
  46. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  47. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'abc/edf/');
  48. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /abc\\/edf\\// LIMIT 100');
  49. });
  50. it('should have WITH MEASUREMENT WHERE in measurement query for non-empty query with tags', () => {
  51. const builder = new InfluxQueryBuilder({
  52. measurement: '',
  53. tags: [{ key: 'app', value: 'email' }],
  54. });
  55. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  56. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ WHERE "app" = \'email\' LIMIT 100');
  57. });
  58. it('should have where condition in measurement query for query with tags', () => {
  59. const builder = new InfluxQueryBuilder({
  60. measurement: '',
  61. tags: [{ key: 'app', value: 'email' }],
  62. });
  63. const query = builder.buildExploreQuery('MEASUREMENTS');
  64. expect(query).toBe('SHOW MEASUREMENTS WHERE "app" = \'email\' LIMIT 100');
  65. });
  66. it('should have where tag name IN filter in tag values query for query with one tag', () => {
  67. const builder = new InfluxQueryBuilder({
  68. measurement: '',
  69. tags: [{ key: 'app', value: 'asdsadsad' }],
  70. });
  71. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  72. expect(query).toBe('SHOW TAG VALUES WITH KEY = "app"');
  73. });
  74. it('should have measurement tag condition and tag name IN filter in tag values query', () => {
  75. const builder = new InfluxQueryBuilder({
  76. measurement: 'cpu',
  77. tags: [{ key: 'app', value: 'email' }, { key: 'host', value: 'server1' }],
  78. });
  79. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  80. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  81. });
  82. it('should select from policy correctly if policy is specified', () => {
  83. const builder = new InfluxQueryBuilder({
  84. measurement: 'cpu',
  85. policy: 'one_week',
  86. tags: [{ key: 'app', value: 'email' }, { key: 'host', value: 'server1' }],
  87. });
  88. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  89. expect(query).toBe('SHOW TAG VALUES FROM "one_week"."cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  90. });
  91. it('should not include policy when policy is default', () => {
  92. const builder = new InfluxQueryBuilder({
  93. measurement: 'cpu',
  94. policy: 'default',
  95. tags: [],
  96. });
  97. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  98. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app"');
  99. });
  100. it('should switch to regex operator in tag condition', () => {
  101. const builder = new InfluxQueryBuilder({
  102. measurement: 'cpu',
  103. tags: [{ key: 'host', value: '/server.*/' }],
  104. });
  105. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  106. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" =~ /server.*/');
  107. });
  108. it('should build show field query', () => {
  109. const builder = new InfluxQueryBuilder({
  110. measurement: 'cpu',
  111. tags: [{ key: 'app', value: 'email' }],
  112. });
  113. const query = builder.buildExploreQuery('FIELDS');
  114. expect(query).toBe('SHOW FIELD KEYS FROM "cpu"');
  115. });
  116. it('should build show field query with regexp', () => {
  117. const builder = new InfluxQueryBuilder({
  118. measurement: '/$var/',
  119. tags: [{ key: 'app', value: 'email' }],
  120. });
  121. const query = builder.buildExploreQuery('FIELDS');
  122. expect(query).toBe('SHOW FIELD KEYS FROM /$var/');
  123. });
  124. it('should build show retention policies query', () => {
  125. const builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] }, 'site');
  126. const query = builder.buildExploreQuery('RETENTION POLICIES');
  127. expect(query).toBe('SHOW RETENTION POLICIES on "site"');
  128. });
  129. });
  130. });