parser.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import {loadFixtures} from 'sentry-test/loadFixtures';
  2. import {
  3. ParseResult,
  4. parseSearch,
  5. Token,
  6. TokenResult,
  7. } from 'sentry/components/searchSyntax/parser';
  8. import {treeTransformer} from 'sentry/components/searchSyntax/utils';
  9. type TestCase = {
  10. /**
  11. * The search query string under parsing test
  12. */
  13. query: string;
  14. /**
  15. * The expected result for the query
  16. */
  17. result: ParseResult;
  18. /**
  19. * This is set when the query is expected to completely fail to parse.
  20. */
  21. raisesError?: boolean;
  22. };
  23. /**
  24. * Normalize results to match the json test cases
  25. */
  26. const normalizeResult = (tokens: TokenResult<Token>[]) =>
  27. treeTransformer({
  28. tree: tokens,
  29. transform: token => {
  30. // XXX: This attempts to keep the test data simple, only including keys
  31. // that are really needed to validate functionality.
  32. // @ts-expect-error
  33. delete token.location;
  34. // @ts-expect-error
  35. delete token.text;
  36. // @ts-expect-error
  37. delete token.config;
  38. if (token.type === Token.Filter && token.invalid === null) {
  39. // @ts-expect-error
  40. delete token.invalid;
  41. }
  42. if (token.type === Token.ValueIso8601Date) {
  43. // Date values are represented as ISO strings in the test case json
  44. return {...token, value: token.value.toISOString()};
  45. }
  46. return token;
  47. },
  48. });
  49. describe('searchSyntax/parser', function () {
  50. const testData = loadFixtures('search-syntax') as unknown as Record<string, TestCase[]>;
  51. const registerTestCase = (testCase: TestCase) =>
  52. it(`handles ${testCase.query}`, () => {
  53. const result = parseSearch(testCase.query);
  54. // Handle errors
  55. if (testCase.raisesError) {
  56. expect(result).toBeNull();
  57. return;
  58. }
  59. if (result === null) {
  60. throw new Error('Parsed result as null without raiseError true');
  61. }
  62. expect(normalizeResult(result)).toEqual(testCase.result);
  63. });
  64. Object.entries(testData).map(([name, cases]) =>
  65. describe(`${name}`, () => {
  66. cases.map(registerTestCase);
  67. })
  68. );
  69. // These test cases test the unique invalid aggregate warnings that are present on the frontend but not on the backend. Thus, separate from the fixtures.
  70. describe('invalid aggregate queries', () => {
  71. describe('return values', () => {
  72. it('aggregate does not return percentage', () => {
  73. const result = parseSearch('count():>23%');
  74. expect(result).not.toBeNull();
  75. if (result) {
  76. expect(normalizeResult(result)).toEqual([
  77. {type: 'spaces', value: ''},
  78. {
  79. type: 'filter',
  80. filter: 'aggregatePercentage',
  81. key: {
  82. type: 'keyAggregate',
  83. args: null,
  84. argsSpaceAfter: {
  85. type: 'spaces',
  86. value: '',
  87. },
  88. argsSpaceBefore: {
  89. type: 'spaces',
  90. value: '',
  91. },
  92. name: {
  93. quoted: false,
  94. type: 'keySimple',
  95. value: 'count',
  96. },
  97. },
  98. value: {type: 'valuePercentage', value: 23},
  99. negated: false,
  100. operator: '>',
  101. invalid: {reason: "'count' returns a number; '23%' is not valid here."},
  102. },
  103. {type: 'spaces', value: ''},
  104. ]);
  105. }
  106. });
  107. it('aggregate does not return date', () => {
  108. const result = parseSearch('count():2022-03-21');
  109. expect(result).not.toBeNull();
  110. if (result) {
  111. expect(normalizeResult(result)).toEqual([
  112. {type: 'spaces', value: ''},
  113. {
  114. type: 'filter',
  115. filter: 'aggregateDate',
  116. key: {
  117. type: 'keyAggregate',
  118. args: null,
  119. argsSpaceAfter: {
  120. type: 'spaces',
  121. value: '',
  122. },
  123. argsSpaceBefore: {
  124. type: 'spaces',
  125. value: '',
  126. },
  127. name: {
  128. quoted: false,
  129. type: 'keySimple',
  130. value: 'count',
  131. },
  132. },
  133. value: {
  134. type: 'valueIso8601Date',
  135. value: '2022-03-21T00:00:00.000Z',
  136. },
  137. negated: false,
  138. operator: '',
  139. invalid: {
  140. reason: "'count' returns a number; '2022-03-21' is not valid here.",
  141. },
  142. },
  143. {type: 'spaces', value: ''},
  144. ]);
  145. }
  146. });
  147. it('aggregate does not return relative date', () => {
  148. const result = parseSearch('count():+1d');
  149. expect(result).not.toBeNull();
  150. if (result) {
  151. expect(normalizeResult(result)).toEqual([
  152. {type: 'spaces', value: ''},
  153. {
  154. type: 'filter',
  155. filter: 'aggregateRelativeDate',
  156. key: {
  157. type: 'keyAggregate',
  158. args: null,
  159. argsSpaceAfter: {
  160. type: 'spaces',
  161. value: '',
  162. },
  163. argsSpaceBefore: {
  164. type: 'spaces',
  165. value: '',
  166. },
  167. name: {
  168. quoted: false,
  169. type: 'keySimple',
  170. value: 'count',
  171. },
  172. },
  173. value: {
  174. sign: '+',
  175. type: 'valueRelativeDate',
  176. unit: 'd',
  177. value: 1,
  178. },
  179. negated: false,
  180. operator: '',
  181. invalid: {
  182. reason: "'count' returns a number; '+1d' is not valid here.",
  183. },
  184. },
  185. {type: 'spaces', value: ''},
  186. ]);
  187. }
  188. });
  189. });
  190. describe('arguments', () => {
  191. it('aggregate does not take argument', () => {
  192. const result = parseSearch('count(202):>200');
  193. expect(result).not.toBeNull();
  194. if (result) {
  195. expect(normalizeResult(result)).toEqual([
  196. {type: 'spaces', value: ''},
  197. {
  198. type: 'filter',
  199. filter: 'aggregateNumeric',
  200. key: {
  201. type: 'keyAggregate',
  202. args: {
  203. args: [
  204. {
  205. separator: '',
  206. value: {
  207. quoted: false,
  208. type: 'keyAggregateParam',
  209. value: '202',
  210. },
  211. },
  212. ],
  213. type: 'keyAggregateArgs',
  214. },
  215. argsSpaceAfter: {
  216. type: 'spaces',
  217. value: '',
  218. },
  219. argsSpaceBefore: {
  220. type: 'spaces',
  221. value: '',
  222. },
  223. name: {
  224. quoted: false,
  225. type: 'keySimple',
  226. value: 'count',
  227. },
  228. },
  229. value: {
  230. rawValue: 200,
  231. type: 'valueNumber',
  232. unit: null,
  233. value: '200',
  234. },
  235. negated: false,
  236. operator: '>',
  237. invalid: {
  238. reason: "'count' does not take any arguments.",
  239. },
  240. },
  241. {type: 'spaces', value: ''},
  242. ]);
  243. }
  244. });
  245. it('aggregate does not take specific argument', () => {
  246. const result = parseSearch('p95(user.id):>200');
  247. expect(result).not.toBeNull();
  248. if (result) {
  249. expect(normalizeResult(result)).toEqual([
  250. {type: 'spaces', value: ''},
  251. {
  252. type: 'filter',
  253. filter: 'aggregateNumeric',
  254. key: {
  255. type: 'keyAggregate',
  256. args: {
  257. args: [
  258. {
  259. separator: '',
  260. value: {
  261. quoted: false,
  262. type: 'keyAggregateParam',
  263. value: 'user.id',
  264. },
  265. },
  266. ],
  267. type: 'keyAggregateArgs',
  268. },
  269. argsSpaceAfter: {
  270. type: 'spaces',
  271. value: '',
  272. },
  273. argsSpaceBefore: {
  274. type: 'spaces',
  275. value: '',
  276. },
  277. name: {
  278. quoted: false,
  279. type: 'keySimple',
  280. value: 'p95',
  281. },
  282. },
  283. value: {
  284. rawValue: 200,
  285. type: 'valueNumber',
  286. unit: null,
  287. value: '200',
  288. },
  289. negated: false,
  290. operator: '>',
  291. invalid: {
  292. reason: "'p95' is not expecting 'user.id' as an argument.",
  293. },
  294. },
  295. {type: 'spaces', value: ''},
  296. ]);
  297. }
  298. });
  299. it('mismatch type with argument', () => {
  300. const result = parseSearch('avg(measurements.stall_count):>20%');
  301. expect(result).not.toBeNull();
  302. if (result) {
  303. expect(normalizeResult(result)).toEqual([
  304. {type: 'spaces', value: ''},
  305. {
  306. type: 'filter',
  307. filter: 'aggregatePercentage',
  308. invalid: {
  309. reason:
  310. "'avg' is not expecting 'measurements.stall_count' as an argument.",
  311. },
  312. key: {
  313. type: 'keyAggregate',
  314. args: {
  315. args: [
  316. {
  317. separator: '',
  318. value: {
  319. quoted: false,
  320. type: 'keyAggregateParam',
  321. value: 'measurements.stall_count',
  322. },
  323. },
  324. ],
  325. type: 'keyAggregateArgs',
  326. },
  327. argsSpaceAfter: {
  328. type: 'spaces',
  329. value: '',
  330. },
  331. argsSpaceBefore: {
  332. type: 'spaces',
  333. value: '',
  334. },
  335. name: {
  336. quoted: false,
  337. type: 'keySimple',
  338. value: 'avg',
  339. },
  340. },
  341. value: {
  342. type: 'valuePercentage',
  343. value: 20,
  344. },
  345. negated: false,
  346. operator: '>',
  347. },
  348. {type: 'spaces', value: ''},
  349. ]);
  350. }
  351. });
  352. });
  353. });
  354. });