SQLishFormatter.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React from 'react';
  2. import {render} from 'sentry-test/reactTestingLibrary';
  3. import {SQLishFormatter} from 'sentry/views/starfish/utils/sqlish/SQLishFormatter';
  4. describe('SQLishFormatter', function () {
  5. describe('SQLishFormatter.toString()', () => {
  6. const formatter = new SQLishFormatter();
  7. it('Falls back to original string if unable to parse', () => {
  8. expect(formatter.toString('😤')).toEqual('😤');
  9. });
  10. it('Adds newlines for keywords in SELECTs', () => {
  11. expect(formatter.toString('SELECT hello FROM users ORDER BY name DESC LIMIT 1;'))
  12. .toMatchInlineSnapshot(`
  13. "SELECT hello
  14. FROM users
  15. ORDER BY name DESC
  16. LIMIT 1;"
  17. `);
  18. });
  19. it('Adds newlines for keywords in INSERTs', () => {
  20. expect(
  21. formatter.toString('INSERT INTO users (id, name) VALUES (:c0, :c1) RETURNING *')
  22. ).toMatchInlineSnapshot(`
  23. "INSERT INTO users (id, name)
  24. VALUES (
  25. :c0, :c1
  26. )
  27. RETURNING *"
  28. `);
  29. });
  30. it('Adds indentation for keywords followed by parentheses', () => {
  31. expect(formatter.toString('SELECT * FROM (SELECT * FROM users))'))
  32. .toMatchInlineSnapshot(`
  33. "SELECT *
  34. FROM (
  35. SELECT *
  36. FROM users
  37. ))"
  38. `);
  39. });
  40. it('Adds indentation for SELECTS in conditions', () => {
  41. expect(
  42. formatter.toString(
  43. 'SELECT * FROM "sentry_users" WHERE (id IN (SELECT VO."id" FROM "sentry_vips" VO LIMIT 1)) AND (id IN (SELECT V1."id" FROM "sentry_currentusers" V1 LIMIT 1)) LIMIT 1'
  44. )
  45. ).toMatchInlineSnapshot(`
  46. "SELECT *
  47. FROM "sentry_users"
  48. WHERE (
  49. id IN (
  50. SELECT VO."id"
  51. FROM "sentry_vips" VO
  52. LIMIT 1
  53. )
  54. ) AND (id IN (
  55. SELECT V1."id"
  56. FROM "sentry_currentusers" V1
  57. LIMIT 1
  58. ))
  59. LIMIT 1"
  60. `);
  61. });
  62. });
  63. describe('SQLishFormatter.toSimpleMarkup()', () => {
  64. const formatter = new SQLishFormatter();
  65. const getMarkup = (markup: any): string => {
  66. const {container} = render(<React.Fragment>{markup}</React.Fragment>);
  67. return container.innerHTML;
  68. };
  69. beforeEach(() => {
  70. // The renderer throws an error because elements in the list do not have
  71. // a `"key"` prop, but that's intentional. The list elements are spans
  72. // with no semantic meaning, and their keys are not meaningful.
  73. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  74. });
  75. it('Capitalizes keywords', () => {
  76. expect(getMarkup(formatter.toSimpleMarkup('select hello'))).toMatchInlineSnapshot(
  77. `"<b>SELECT</b><span> </span><span>hello</span>"`
  78. );
  79. });
  80. it('Wraps every token in a `<span>` element', () => {
  81. expect(getMarkup(formatter.toSimpleMarkup('SELECT hello;'))).toMatchInlineSnapshot(
  82. `"<b>SELECT</b><span> </span><span>hello;</span>"`
  83. );
  84. });
  85. });
  86. });