statementPosition.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { monacoTypes } from '@grafana/ui';
  2. import {
  3. multiLineFullQuery,
  4. singleLineFullQuery,
  5. singleLineEmptyQuery,
  6. singleLineTwoQueries,
  7. } from '../../__mocks__/cloudwatch-sql-test-data';
  8. import MonacoMock from '../../__mocks__/monarch/Monaco';
  9. import TextModel from '../../__mocks__/monarch/TextModel';
  10. import { linkedTokenBuilder } from '../../monarch/linkedTokenBuilder';
  11. import { StatementPosition } from '../../monarch/types';
  12. import cloudWatchSqlLanguageDefinition from '../definition';
  13. import { getStatementPosition } from './statementPosition';
  14. import { SQLTokenTypes } from './types';
  15. describe('statementPosition', () => {
  16. function assertPosition(query: string, position: monacoTypes.IPosition, expected: StatementPosition) {
  17. const testModel = TextModel(query);
  18. const current = linkedTokenBuilder(
  19. MonacoMock,
  20. cloudWatchSqlLanguageDefinition,
  21. testModel as monacoTypes.editor.ITextModel,
  22. position,
  23. SQLTokenTypes
  24. );
  25. const statementPosition = getStatementPosition(current);
  26. expect(statementPosition).toBe(expected);
  27. }
  28. test.each([
  29. [singleLineFullQuery.query, { lineNumber: 1, column: 0 }],
  30. [multiLineFullQuery.query, { lineNumber: 1, column: 0 }],
  31. [singleLineEmptyQuery.query, { lineNumber: 1, column: 0 }],
  32. [singleLineTwoQueries.query, { lineNumber: 1, column: 154 }],
  33. ])('should be before select keyword', (query: string, position: monacoTypes.IPosition) => {
  34. assertPosition(query, position, StatementPosition.SelectKeyword);
  35. });
  36. test.each([
  37. [singleLineFullQuery.query, { lineNumber: 1, column: 7 }],
  38. [multiLineFullQuery.query, { lineNumber: 1, column: 7 }],
  39. [singleLineTwoQueries.query, { lineNumber: 1, column: 161 }],
  40. ])('should be after select keyword', (query: string, position: monacoTypes.IPosition) => {
  41. assertPosition(query, position, StatementPosition.AfterSelectKeyword);
  42. });
  43. test.each([
  44. [singleLineFullQuery.query, { lineNumber: 1, column: 12 }],
  45. [multiLineFullQuery.query, { lineNumber: 1, column: 12 }],
  46. [singleLineTwoQueries.query, { lineNumber: 1, column: 166 }],
  47. ])('should be first argument in select statistic function', (query: string, position: monacoTypes.IPosition) => {
  48. assertPosition(query, position, StatementPosition.AfterSelectFuncFirstArgument);
  49. });
  50. test.each([
  51. [singleLineFullQuery.query, { lineNumber: 1, column: 27 }],
  52. [multiLineFullQuery.query, { lineNumber: 2, column: 0 }],
  53. [singleLineTwoQueries.query, { lineNumber: 1, column: 181 }],
  54. ])('should be before the FROM keyword', (query: string, position: monacoTypes.IPosition) => {
  55. assertPosition(query, position, StatementPosition.FromKeyword);
  56. });
  57. test.each([
  58. [singleLineFullQuery.query, { lineNumber: 1, column: 32 }],
  59. [multiLineFullQuery.query, { lineNumber: 2, column: 5 }],
  60. [singleLineTwoQueries.query, { lineNumber: 1, column: 186 }],
  61. ])('should after the FROM keyword', (query: string, position: monacoTypes.IPosition) => {
  62. assertPosition(query, position, StatementPosition.AfterFromKeyword);
  63. });
  64. test.each([
  65. [singleLineFullQuery.query, { lineNumber: 1, column: 40 }],
  66. [multiLineFullQuery.query, { lineNumber: 2, column: 13 }],
  67. [singleLineTwoQueries.query, { lineNumber: 1, column: 40 }],
  68. ])('should be namespace arg in the schema func', (query: string, position: monacoTypes.IPosition) => {
  69. assertPosition(query, position, StatementPosition.SchemaFuncFirstArgument);
  70. });
  71. test.each([
  72. [singleLineFullQuery.query, { lineNumber: 1, column: 50 }],
  73. [multiLineFullQuery.query, { lineNumber: 2, column: 23 }],
  74. [singleLineTwoQueries.query, { lineNumber: 1, column: 50 }],
  75. ])('should be label key args within the schema func', (query: string, position: monacoTypes.IPosition) => {
  76. assertPosition(query, position, StatementPosition.SchemaFuncExtraArgument);
  77. });
  78. test.each([
  79. [singleLineFullQuery.query, { lineNumber: 1, column: 63 }],
  80. [multiLineFullQuery.query, { lineNumber: 3, column: 0 }],
  81. [singleLineTwoQueries.query, { lineNumber: 1, column: 63 }],
  82. ])('should be after from schema/namespace', (query: string, position: monacoTypes.IPosition) => {
  83. assertPosition(query, position, StatementPosition.AfterFrom);
  84. });
  85. test.each([
  86. [singleLineFullQuery.query, { lineNumber: 1, column: 69 }],
  87. [multiLineFullQuery.query, { lineNumber: 4, column: 6 }],
  88. [singleLineTwoQueries.query, { lineNumber: 1, column: 69 }],
  89. ])('should after where keyword and before label key', (query: string, position: monacoTypes.IPosition) => {
  90. assertPosition(query, position, StatementPosition.WhereKey);
  91. });
  92. test.each([
  93. [singleLineFullQuery.query, { lineNumber: 1, column: 79 }],
  94. [multiLineFullQuery.query, { lineNumber: 4, column: 17 }],
  95. [singleLineTwoQueries.query, { lineNumber: 1, column: 79 }],
  96. ])('should be before the comparison operator in a where filter', (query: string, position: monacoTypes.IPosition) => {
  97. assertPosition(query, position, StatementPosition.WhereComparisonOperator);
  98. });
  99. test.each([
  100. [singleLineFullQuery.query, { lineNumber: 1, column: 81 }],
  101. [multiLineFullQuery.query, { lineNumber: 4, column: 19 }],
  102. [singleLineTwoQueries.query, { lineNumber: 1, column: 81 }],
  103. ])('should be before or in the value in a where filter', (query: string, position: monacoTypes.IPosition) => {
  104. assertPosition(query, position, StatementPosition.WhereValue);
  105. });
  106. test.each([
  107. [singleLineFullQuery.query, { lineNumber: 1, column: 105 }],
  108. [multiLineFullQuery.query, { lineNumber: 5, column: 0 }],
  109. [singleLineTwoQueries.query, { lineNumber: 1, column: 105 }],
  110. ])('should be after a where value', (query: string, position: monacoTypes.IPosition) => {
  111. assertPosition(query, position, StatementPosition.AfterWhereValue);
  112. });
  113. test.each([
  114. [singleLineFullQuery.query, { lineNumber: 1, column: 115 }],
  115. [multiLineFullQuery.query, { lineNumber: 5, column: 10 }],
  116. [singleLineTwoQueries.query, { lineNumber: 1, column: 115 }],
  117. ])('should be after group by keywords', (query: string, position: monacoTypes.IPosition) => {
  118. assertPosition(query, position, StatementPosition.AfterGroupByKeywords);
  119. });
  120. test.each([
  121. [singleLineFullQuery.query, { lineNumber: 1, column: 123 }],
  122. [multiLineFullQuery.query, { lineNumber: 5, column: 22 }],
  123. [singleLineTwoQueries.query, { lineNumber: 1, column: 123 }],
  124. ])('should be after group by labels', (query: string, position: monacoTypes.IPosition) => {
  125. assertPosition(query, position, StatementPosition.AfterGroupBy);
  126. });
  127. test.each([
  128. [singleLineFullQuery.query, { lineNumber: 1, column: 132 }],
  129. [multiLineFullQuery.query, { lineNumber: 5, column: 31 }],
  130. [singleLineTwoQueries.query, { lineNumber: 1, column: 132 }],
  131. ])('should be after order by keywords', (query: string, position: monacoTypes.IPosition) => {
  132. assertPosition(query, position, StatementPosition.AfterOrderByKeywords);
  133. });
  134. test.each([
  135. [singleLineFullQuery.query, { lineNumber: 1, column: 138 }],
  136. [multiLineFullQuery.query, { lineNumber: 5, column: 37 }],
  137. [singleLineTwoQueries.query, { lineNumber: 1, column: 138 }],
  138. ])('should be after order by function', (query: string, position: monacoTypes.IPosition) => {
  139. assertPosition(query, position, StatementPosition.AfterOrderByFunction);
  140. });
  141. test.each([
  142. [singleLineFullQuery.query, { lineNumber: 1, column: 143 }],
  143. [multiLineFullQuery.query, { lineNumber: 6, column: 0 }],
  144. [singleLineTwoQueries.query, { lineNumber: 1, column: 145 }],
  145. ])('should be after order by direction', (query: string, position: monacoTypes.IPosition) => {
  146. assertPosition(query, position, StatementPosition.AfterOrderByDirection);
  147. });
  148. });