SQLishParser.spec.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. ])('Parses %s', sql => {
  23. expect(() => {
  24. parser.parse(sql);
  25. }).not.toThrow();
  26. });
  27. });
  28. describe('SQLishParser.parse', () => {
  29. const parser = new SQLishParser();
  30. it('Detects collapsed columns', () => {
  31. expect(parser.parse('select ..')).toEqual([
  32. {
  33. type: 'Keyword',
  34. content: 'select',
  35. },
  36. {
  37. type: 'Whitespace',
  38. content: ' ',
  39. },
  40. {
  41. type: 'CollapsedColumns',
  42. content: '..',
  43. },
  44. ]);
  45. });
  46. it('Detects whitespace between generic tokens and JOIN commands', () => {
  47. expect(parser.parse('table1 INNER JOIN table2')).toEqual([
  48. {
  49. type: 'GenericToken',
  50. content: 'table1',
  51. },
  52. {type: 'Whitespace', content: ' '},
  53. {type: 'Keyword', content: 'INNER JOIN'},
  54. {type: 'Whitespace', content: ' '},
  55. {
  56. type: 'GenericToken',
  57. content: 'table2',
  58. },
  59. ]);
  60. });
  61. });
  62. });