query_builder.jest.ts 6.3 KB

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