query_builder.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 ignore condition if operator is a value operator', () => {
  31. const builder = new InfluxQueryBuilder({
  32. measurement: '',
  33. tags: [{ key: 'value', value: '10', operator: '>' }],
  34. });
  35. const query = builder.buildExploreQuery('TAG_KEYS');
  36. expect(query).toBe('SHOW TAG KEYS');
  37. });
  38. it('should have no conditions in measurement query for query with no tags', () => {
  39. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  40. const query = builder.buildExploreQuery('MEASUREMENTS');
  41. expect(query).toBe('SHOW MEASUREMENTS LIMIT 100');
  42. });
  43. it('should have no conditions in measurement query for query with no tags and empty query', () => {
  44. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  45. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, '');
  46. expect(query).toBe('SHOW MEASUREMENTS LIMIT 100');
  47. });
  48. it('should have WITH MEASUREMENT in measurement query for non-empty query with no tags', () => {
  49. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  50. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  51. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /(?i)something/ LIMIT 100');
  52. });
  53. it('should escape the regex value in measurement query', () => {
  54. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  55. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'abc/edf/');
  56. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /(?i)abc\\/edf\\// LIMIT 100');
  57. });
  58. it('should have WITH MEASUREMENT WHERE in measurement query for non-empty query with tags', () => {
  59. const builder = new InfluxQueryBuilder({
  60. measurement: '',
  61. tags: [{ key: 'app', value: 'email' }],
  62. });
  63. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  64. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /(?i)something/ WHERE "app" = \'email\' LIMIT 100');
  65. });
  66. it('should have where condition in measurement query for query with tags', () => {
  67. const builder = new InfluxQueryBuilder({
  68. measurement: '',
  69. tags: [{ key: 'app', value: 'email' }],
  70. });
  71. const 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', () => {
  75. const builder = new InfluxQueryBuilder({
  76. measurement: '',
  77. tags: [{ key: 'app', value: 'asdsadsad' }],
  78. });
  79. const 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', () => {
  83. const builder = new InfluxQueryBuilder({
  84. measurement: 'cpu',
  85. tags: [
  86. { key: 'app', value: 'email' },
  87. { key: 'host', value: 'server1' },
  88. ],
  89. });
  90. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  91. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  92. });
  93. it('should select from policy correctly if policy is specified', () => {
  94. const builder = new InfluxQueryBuilder({
  95. measurement: 'cpu',
  96. policy: 'one_week',
  97. tags: [
  98. { key: 'app', value: 'email' },
  99. { key: 'host', value: 'server1' },
  100. ],
  101. });
  102. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  103. expect(query).toBe('SHOW TAG VALUES FROM "one_week"."cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  104. });
  105. it('should not include policy when policy is default', () => {
  106. const builder = new InfluxQueryBuilder({
  107. measurement: 'cpu',
  108. policy: 'default',
  109. tags: [],
  110. });
  111. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  112. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app"');
  113. });
  114. it('should switch to regex operator in tag condition', () => {
  115. const builder = new InfluxQueryBuilder({
  116. measurement: 'cpu',
  117. tags: [{ key: 'host', value: '/server.*/' }],
  118. });
  119. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  120. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" =~ /server.*/');
  121. });
  122. it('should build show field query', () => {
  123. const builder = new InfluxQueryBuilder({
  124. measurement: 'cpu',
  125. tags: [{ key: 'app', value: 'email' }],
  126. });
  127. const query = builder.buildExploreQuery('FIELDS');
  128. expect(query).toBe('SHOW FIELD KEYS FROM "cpu"');
  129. });
  130. it('should build show field query with regexp', () => {
  131. const builder = new InfluxQueryBuilder({
  132. measurement: '/$var/',
  133. tags: [{ key: 'app', value: 'email' }],
  134. });
  135. const query = builder.buildExploreQuery('FIELDS');
  136. expect(query).toBe('SHOW FIELD KEYS FROM /$var/');
  137. });
  138. it('should build show retention policies query', () => {
  139. const builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] }, 'site');
  140. const query = builder.buildExploreQuery('RETENTION POLICIES');
  141. expect(query).toBe('SHOW RETENTION POLICIES on "site"');
  142. });
  143. it('should handle tag-value=number-ish when getting measurements', () => {
  144. const builder = new InfluxQueryBuilder(
  145. { measurement: undefined, tags: [{ key: 'app', value: '42', operator: '==' }] },
  146. undefined
  147. );
  148. const query = builder.buildExploreQuery('MEASUREMENTS');
  149. expect(query).toBe(`SHOW MEASUREMENTS WHERE "app" == 42 LIMIT 100`);
  150. });
  151. it('should handle tag-value=number-ish getting tag-keys', () => {
  152. const builder = new InfluxQueryBuilder(
  153. { measurement: undefined, tags: [{ key: 'app', value: '42', operator: '==' }] },
  154. undefined
  155. );
  156. const query = builder.buildExploreQuery('TAG_KEYS');
  157. expect(query).toBe(`SHOW TAG KEYS WHERE "app" == 42`);
  158. });
  159. it('should handle tag-value-contains-backslash-character getting tag-keys', () => {
  160. const builder = new InfluxQueryBuilder(
  161. { measurement: undefined, tags: [{ key: 'app', value: 'lab\\el', operator: '==' }] },
  162. undefined
  163. );
  164. const query = builder.buildExploreQuery('TAG_KEYS');
  165. expect(query).toBe(`SHOW TAG KEYS WHERE "app" == 'lab\\\\el'`);
  166. });
  167. it('should handle tag-value-contains-single-quote-character getting tag-keys', () => {
  168. const builder = new InfluxQueryBuilder(
  169. { measurement: undefined, tags: [{ key: 'app', value: "lab'el", operator: '==' }] },
  170. undefined
  171. );
  172. const query = builder.buildExploreQuery('TAG_KEYS');
  173. expect(query).toBe(`SHOW TAG KEYS WHERE "app" == 'lab\\'el'`);
  174. });
  175. it('should handle tag-value=emptry-string when getting measurements', () => {
  176. const builder = new InfluxQueryBuilder(
  177. { measurement: undefined, tags: [{ key: 'app', value: '', operator: '==' }] },
  178. undefined
  179. );
  180. const query = builder.buildExploreQuery('MEASUREMENTS');
  181. expect(query).toBe(`SHOW MEASUREMENTS WHERE "app" == '' LIMIT 100`);
  182. });
  183. it('should handle tag-value=emptry-string when getting tag-keys', () => {
  184. const builder = new InfluxQueryBuilder(
  185. { measurement: undefined, tags: [{ key: 'app', value: '', operator: '==' }] },
  186. undefined
  187. );
  188. const query = builder.buildExploreQuery('TAG_KEYS');
  189. expect(query).toBe(`SHOW TAG KEYS WHERE "app" == ''`);
  190. });
  191. });
  192. });