tokenUtils.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { monacoTypes } from '@grafana/ui';
  2. import {
  3. multiLineFullQuery,
  4. singleLineFullQuery,
  5. singleLineTwoQueries,
  6. multiLineIncompleteQueryWithoutNamespace,
  7. } from '../../__mocks__/cloudwatch-sql-test-data';
  8. import MonacoMock from '../../__mocks__/monarch/Monaco';
  9. import TextModel from '../../__mocks__/monarch/TextModel';
  10. import { LinkedToken } from '../../monarch/LinkedToken';
  11. import { linkedTokenBuilder } from '../../monarch/linkedTokenBuilder';
  12. import cloudWatchSqlLanguageDefinition from '../definition';
  13. import { SELECT } from '../language';
  14. import { getMetricNameToken, getNamespaceToken, getSelectStatisticToken, getSelectToken } from './tokenUtils';
  15. import { SQLTokenTypes } from './types';
  16. const getToken = (
  17. query: string,
  18. position: monacoTypes.IPosition,
  19. invokeFunction: (token: LinkedToken | null) => LinkedToken | null
  20. ) => {
  21. const testModel = TextModel(query);
  22. const current = linkedTokenBuilder(
  23. MonacoMock,
  24. cloudWatchSqlLanguageDefinition,
  25. testModel as monacoTypes.editor.ITextModel,
  26. position,
  27. SQLTokenTypes
  28. );
  29. return invokeFunction(current);
  30. };
  31. describe('tokenUtils', () => {
  32. test.each([
  33. [singleLineFullQuery.query, { lineNumber: 1, column: 50 }],
  34. [multiLineFullQuery.query, { lineNumber: 5, column: 10 }],
  35. [singleLineTwoQueries.query, { lineNumber: 1, column: 30 }],
  36. [singleLineTwoQueries.query, { lineNumber: 1, column: 185 }],
  37. ])('getSelectToken should return the right token', (query: string, position: monacoTypes.IPosition) => {
  38. const token = getToken(query, position, getSelectToken);
  39. expect(token).not.toBeNull();
  40. expect(token?.value).toBe(SELECT);
  41. expect(token?.type).toBe(SQLTokenTypes.Keyword);
  42. });
  43. test.each([
  44. [singleLineFullQuery.query, { lineNumber: 1, column: 50 }],
  45. [multiLineFullQuery.query, { lineNumber: 5, column: 10 }],
  46. [singleLineTwoQueries.query, { lineNumber: 1, column: 30 }],
  47. [singleLineTwoQueries.query, { lineNumber: 1, column: 185 }],
  48. ])('getSelectToken should return the right token', (query: string, position: monacoTypes.IPosition) => {
  49. const token = getToken(query, position, getSelectStatisticToken);
  50. expect(token).not.toBeNull();
  51. expect(token?.type).toBe(SQLTokenTypes.Function);
  52. });
  53. test.each([
  54. [singleLineFullQuery.query, 'AVG', { lineNumber: 1, column: 50 }],
  55. [multiLineFullQuery.query, 'AVG', { lineNumber: 5, column: 10 }],
  56. [singleLineTwoQueries.query, 'AVG', { lineNumber: 1, column: 30 }],
  57. [singleLineTwoQueries.query, 'SUM', { lineNumber: 1, column: 185 }],
  58. ])(
  59. 'getSelectStatisticToken should return the right token',
  60. (query: string, value: string, position: monacoTypes.IPosition) => {
  61. const token = getToken(query, position, getSelectStatisticToken);
  62. expect(token).not.toBeNull();
  63. expect(token?.value).toBe(value);
  64. expect(token?.type).toBe(SQLTokenTypes.Function);
  65. }
  66. );
  67. test.each([
  68. [singleLineFullQuery.query, 'CPUUtilization', { lineNumber: 1, column: 50 }],
  69. [multiLineFullQuery.query, 'CPUUtilization', { lineNumber: 5, column: 10 }],
  70. [singleLineTwoQueries.query, 'CPUUtilization', { lineNumber: 1, column: 30 }],
  71. [singleLineTwoQueries.query, 'CPUCreditUsage', { lineNumber: 1, column: 185 }],
  72. ])(
  73. 'getMetricNameToken should return the right token',
  74. (query: string, value: string, position: monacoTypes.IPosition) => {
  75. const token = getToken(query, position, getMetricNameToken);
  76. expect(token).not.toBeNull();
  77. expect(token?.value).toBe(value);
  78. expect(token?.type).toBe(SQLTokenTypes.Identifier);
  79. }
  80. );
  81. test.each([
  82. [singleLineFullQuery.query, '"AWS/EC2"', SQLTokenTypes.Type, { lineNumber: 1, column: 50 }],
  83. [multiLineFullQuery.query, '"AWS/ECS"', SQLTokenTypes.Type, { lineNumber: 5, column: 10 }],
  84. [singleLineTwoQueries.query, '"AWS/EC2"', SQLTokenTypes.Type, { lineNumber: 1, column: 30 }],
  85. [singleLineTwoQueries.query, '"AWS/ECS"', SQLTokenTypes.Type, { lineNumber: 1, column: 185 }],
  86. [multiLineIncompleteQueryWithoutNamespace.query, undefined, undefined, { lineNumber: 2, column: 5 }],
  87. ])(
  88. 'getNamespaceToken should return the right token',
  89. (query: string, value: string | undefined, tokenType: string | undefined, position: monacoTypes.IPosition) => {
  90. const token = getToken(query, position, getNamespaceToken);
  91. expect(token?.value).toBe(value);
  92. expect(token?.type).toBe(tokenType);
  93. }
  94. );
  95. });