query_builder.jest.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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('MEASUREMENTS', undefined, 'something');
  44. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ LIMIT 100');
  45. });
  46. it('should have WITH MEASUREMENT WHERE in measurement query for non-empty query with tags', function() {
  47. var builder = new InfluxQueryBuilder({
  48. measurement: '',
  49. tags: [{ key: 'app', value: 'email' }],
  50. });
  51. var query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  52. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ WHERE "app" = \'email\' LIMIT 100');
  53. });
  54. it('should have where condition in measurement query for query with tags', function() {
  55. var builder = new InfluxQueryBuilder({
  56. measurement: '',
  57. tags: [{ key: 'app', value: 'email' }],
  58. });
  59. var query = builder.buildExploreQuery('MEASUREMENTS');
  60. expect(query).toBe('SHOW MEASUREMENTS WHERE "app" = \'email\' LIMIT 100');
  61. });
  62. it('should have where tag name IN filter in tag values query for query with one tag', function() {
  63. var builder = new InfluxQueryBuilder({
  64. measurement: '',
  65. tags: [{ key: 'app', value: 'asdsadsad' }],
  66. });
  67. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  68. expect(query).toBe('SHOW TAG VALUES WITH KEY = "app"');
  69. });
  70. it('should have measurement tag condition and tag name IN filter in tag values query', function() {
  71. var builder = new InfluxQueryBuilder({
  72. measurement: 'cpu',
  73. tags: [{ key: 'app', value: 'email' }, { key: 'host', value: 'server1' }],
  74. });
  75. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  76. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  77. });
  78. it('should select from policy correctly if policy is specified', function() {
  79. var builder = new InfluxQueryBuilder({
  80. measurement: 'cpu',
  81. policy: 'one_week',
  82. tags: [{ key: 'app', value: 'email' }, { key: 'host', value: 'server1' }],
  83. });
  84. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  85. expect(query).toBe('SHOW TAG VALUES FROM "one_week"."cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  86. });
  87. it('should not include policy when policy is default', function() {
  88. var builder = new InfluxQueryBuilder({
  89. measurement: 'cpu',
  90. policy: 'default',
  91. tags: [],
  92. });
  93. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  94. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app"');
  95. });
  96. it('should switch to regex operator in tag condition', function() {
  97. var builder = new InfluxQueryBuilder({
  98. measurement: 'cpu',
  99. tags: [{ key: 'host', value: '/server.*/' }],
  100. });
  101. var query = builder.buildExploreQuery('TAG_VALUES', 'app');
  102. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" =~ /server.*/');
  103. });
  104. it('should build show field query', function() {
  105. var builder = new InfluxQueryBuilder({
  106. measurement: 'cpu',
  107. tags: [{ key: 'app', value: 'email' }],
  108. });
  109. var query = builder.buildExploreQuery('FIELDS');
  110. expect(query).toBe('SHOW FIELD KEYS FROM "cpu"');
  111. });
  112. it('should build show field query with regexp', function() {
  113. var builder = new InfluxQueryBuilder({
  114. measurement: '/$var/',
  115. tags: [{ key: 'app', value: 'email' }],
  116. });
  117. var query = builder.buildExploreQuery('FIELDS');
  118. expect(query).toBe('SHOW FIELD KEYS FROM /$var/');
  119. });
  120. it('should build show retention policies query', function() {
  121. var builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] }, 'site');
  122. var query = builder.buildExploreQuery('RETENTION POLICIES');
  123. expect(query).toBe('SHOW RETENTION POLICIES on "site"');
  124. });
  125. });
  126. });