SQLishParser.spec.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {SQLishParser} from 'sentry/views/starfish/utils/sqlish/SQLishParser';
  2. describe('SQLishParser', function () {
  3. describe('SQLishParser()', () => {
  4. const parser = new SQLishParser();
  5. it.each([
  6. 'SELECT;',
  7. 'SELECT hello;',
  8. 'SELECT *;', // Wildcards
  9. 'WHERE age = 10;', // Equality
  10. 'WHERE age != 10;', // Inequality
  11. 'total / time', // Division
  12. 'sum(age)::numeric(0, 5)', // Type casting
  13. 'WHERE age > 10 AND age < 20;', // Comparison
  14. "WHERE$1 ILIKE ' % ' || 'text'", // Conditionals
  15. 'SELECT id, name;', // Column lists
  16. 'columns AS `tags[column]`', // ClickHouse backtics
  17. 'SELECT id, nam*', // Truncation
  18. 'AND created >= :c1', // PHP-Style I
  19. 'LIMIT $2', // PHP-style II
  20. 'created >= %s', // Python-style
  21. 'created >= $1', // Rails-style
  22. '@@ to_tsquery', // Postgres full-text search
  23. 'flags & %s)', // Bitwise AND
  24. 'flags | %s)', // Bitwise OR
  25. 'flags ^ %s)', // Bitwise XOR
  26. 'flags ~ %s)', // Bitwise NOT
  27. ])('Parses %s', sql => {
  28. expect(() => {
  29. parser.parse(sql);
  30. }).not.toThrow();
  31. });
  32. });
  33. describe('SQLishParser.parse', () => {
  34. const parser = new SQLishParser();
  35. it('Detects collapsed columns', () => {
  36. expect(parser.parse('select ..')).toEqual([
  37. {
  38. type: 'Keyword',
  39. content: 'SELECT',
  40. },
  41. {
  42. type: 'Whitespace',
  43. content: ' ',
  44. },
  45. {
  46. type: 'CollapsedColumns',
  47. content: '..',
  48. },
  49. ]);
  50. });
  51. it('Detects whitespace between generic tokens and JOIN commands', () => {
  52. expect(parser.parse('sentry_users INNER JOIN sentry_messages')).toEqual([
  53. {
  54. type: 'GenericToken',
  55. content: 'sentry_users',
  56. },
  57. {type: 'Whitespace', content: ' '},
  58. {type: 'Keyword', content: 'INNER'},
  59. {type: 'Whitespace', content: ' '},
  60. {type: 'Keyword', content: 'JOIN'},
  61. {type: 'Whitespace', content: ' '},
  62. {
  63. type: 'GenericToken',
  64. content: 'sentry_messages',
  65. },
  66. ]);
  67. });
  68. });
  69. });