query_utils.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { getHighlighterExpressionsFromQuery, getNormalizedLokiQuery } from './query_utils';
  2. import { LokiQuery, LokiQueryType } from './types';
  3. describe('getHighlighterExpressionsFromQuery', () => {
  4. it('returns no expressions for empty query', () => {
  5. expect(getHighlighterExpressionsFromQuery('')).toEqual([]);
  6. });
  7. it('returns no expression for query with empty filter ', () => {
  8. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= ``')).toEqual([]);
  9. });
  10. it('returns no expression for query with empty filter and parser', () => {
  11. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= `` | json count="counter" | __error__=``')).toEqual([]);
  12. });
  13. it('returns no expression for query with empty filter and chained filter', () => {
  14. expect(
  15. getHighlighterExpressionsFromQuery('{foo="bar"} |= `` |= `highlight` | json count="counter" | __error__=``')
  16. ).toEqual(['highlight']);
  17. });
  18. it('returns no expression for query with empty filter, chained and regex filter', () => {
  19. expect(
  20. getHighlighterExpressionsFromQuery(
  21. '{foo="bar"} |= `` |= `highlight` |~ `high.ight` | json count="counter" | __error__=``'
  22. )
  23. ).toEqual(['highlight', 'high.ight']);
  24. });
  25. it('returns no expression for query with empty filter, chained and regex quotes filter', () => {
  26. expect(
  27. getHighlighterExpressionsFromQuery(
  28. '{foo="bar"} |= `` |= `highlight` |~ "highlight\\\\d" | json count="counter" | __error__=``'
  29. )
  30. ).toEqual(['highlight', 'highlight\\d']);
  31. });
  32. it('returns an expression for query with filter using quotes', () => {
  33. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x"')).toEqual(['x']);
  34. });
  35. it('returns an expression for query with filter using backticks', () => {
  36. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= `x`')).toEqual(['x']);
  37. });
  38. it('returns expressions for query with filter chain', () => {
  39. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ "y"')).toEqual(['x', 'y']);
  40. });
  41. it('returns expressions for query with filter chain using both backticks and quotes', () => {
  42. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ `y`')).toEqual(['x', 'y']);
  43. });
  44. it('returns expression for query with log parser', () => {
  45. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" | logfmt')).toEqual(['x']);
  46. });
  47. it('returns expressions for query with filter chain folowed by log parser', () => {
  48. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ "y" | logfmt')).toEqual(['x', 'y']);
  49. });
  50. it('returns drops expressions for query with negative filter chain using quotes', () => {
  51. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" != "y"')).toEqual(['x']);
  52. });
  53. it('returns expressions for query with filter chain using backticks', () => {
  54. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= `x` |~ `y`')).toEqual(['x', 'y']);
  55. });
  56. it('returns expressions for query with filter chain using quotes and backticks', () => {
  57. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ `y`')).toEqual(['x', 'y']);
  58. });
  59. it('returns null if filter term is not wrapped in double quotes', () => {
  60. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= x')).toEqual([]);
  61. });
  62. it('escapes filter term if regex filter operator is not used', () => {
  63. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x[yz].w"')).toEqual(['x\\[yz\\]\\.w']);
  64. });
  65. it('does not escape filter term if regex filter operator is used', () => {
  66. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |~ "x[yz].w" |~ "z.+"')).toEqual(['x[yz].w', 'z.+']);
  67. });
  68. it('removes extra backslash escaping if regex filter operator and quotes are used', () => {
  69. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |~ "\\\\w+"')).toEqual(['\\w+']);
  70. });
  71. it('does not remove backslash escaping if regex filter operator and backticks are used', () => {
  72. expect(getHighlighterExpressionsFromQuery('{foo="bar"} |~ `\\w+`')).toEqual(['\\w+']);
  73. });
  74. });
  75. describe('getNormalizedLokiQuery', () => {
  76. function expectNormalized(inputProps: Object, outputQueryType: LokiQueryType) {
  77. const input: LokiQuery = { refId: 'A', expr: 'test1', ...inputProps };
  78. const output = getNormalizedLokiQuery(input);
  79. expect(output).toStrictEqual({ refId: 'A', expr: 'test1', queryType: outputQueryType });
  80. }
  81. it('handles no props case', () => {
  82. expectNormalized({}, LokiQueryType.Range);
  83. });
  84. it('handles old-style instant case', () => {
  85. expectNormalized({ instant: true, range: false }, LokiQueryType.Instant);
  86. });
  87. it('handles old-style range case', () => {
  88. expectNormalized({ instant: false, range: true }, LokiQueryType.Range);
  89. });
  90. it('handles new+old style instant', () => {
  91. expectNormalized({ instant: true, range: false, queryType: LokiQueryType.Range }, LokiQueryType.Range);
  92. });
  93. it('handles new+old style range', () => {
  94. expectNormalized({ instant: false, range: true, queryType: LokiQueryType.Instant }, LokiQueryType.Instant);
  95. });
  96. it('handles new<>old conflict (new wins), range', () => {
  97. expectNormalized({ instant: false, range: true, queryType: LokiQueryType.Range }, LokiQueryType.Range);
  98. });
  99. it('handles new<>old conflict (new wins), instant', () => {
  100. expectNormalized({ instant: true, range: false, queryType: LokiQueryType.Instant }, LokiQueryType.Instant);
  101. });
  102. it('handles invalid new, range', () => {
  103. expectNormalized({ queryType: 'invalid' }, LokiQueryType.Range);
  104. });
  105. it('handles invalid new, when old-range exists, use old', () => {
  106. expectNormalized({ instant: false, range: true, queryType: 'invalid' }, LokiQueryType.Range);
  107. });
  108. it('handles invalid new, when old-instant exists, use old', () => {
  109. expectNormalized({ instant: true, range: false, queryType: 'invalid' }, LokiQueryType.Instant);
  110. });
  111. });