SQLishFormatter.spec.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {Fragment} 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('Capitalizes lowercase keywords', () => {
  41. expect(formatter.toString('select * from users;')).toMatchInlineSnapshot(`
  42. "SELECT *
  43. FROM users;"
  44. `);
  45. });
  46. it('Adds indentation for SELECTS in conditions', () => {
  47. expect(
  48. formatter.toString(
  49. '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'
  50. )
  51. ).toMatchInlineSnapshot(`
  52. "SELECT *
  53. FROM "sentry_users"
  54. WHERE (
  55. id IN (
  56. SELECT VO."id"
  57. FROM "sentry_vips" VO
  58. LIMIT 1
  59. )
  60. ) AND (id IN (
  61. SELECT V1."id"
  62. FROM "sentry_currentusers" V1
  63. LIMIT 1
  64. ))
  65. LIMIT 1"
  66. `);
  67. });
  68. it('Reflows long lines to a max length', () => {
  69. expect(
  70. formatter.toString(
  71. 'SELECT "sentry_organization"."id", "sentry_organization"."name", "sentry_organization"."slug", "sentry_organization"."status", "sentry_organization"."date_added", "sentry_organization"."default_role", "sentry_organization"."is_test", "sentry_organization"."flags" FROM "sentry_organization" WHERE "sentry_organization"."id" = %s LIMIT 21'
  72. )
  73. ).toMatchInlineSnapshot(`
  74. "SELECT "sentry_organization"."id", "sentry_organization"."name", "sentry_organization"."slug",
  75. "sentry_organization"."status", "sentry_organization"."date_added",
  76. "sentry_organization"."default_role", "sentry_organization"."is_test", "sentry_organization"."flags"
  77. FROM "sentry_organization"
  78. WHERE "sentry_organization"."id" = %s
  79. LIMIT 21"
  80. `);
  81. });
  82. it('Reflows to specified width', () => {
  83. expect(
  84. formatter.toString(
  85. 'SELECT "sentry_organization"."id", "sentry_organization"."name", "sentry_organization"."slug", "sentry_organization"."status", "sentry_organization"."date_added" FROM "sentry_organization" WHERE "sentry_organization"."id" = %s LIMIT 21',
  86. {maxLineLength: 40}
  87. )
  88. ).toMatchInlineSnapshot(`
  89. "SELECT "sentry_organization"."id",
  90. "sentry_organization"."name",
  91. "sentry_organization"."slug",
  92. "sentry_organization"."status",
  93. "sentry_organization"."date_added"
  94. FROM "sentry_organization"
  95. WHERE "sentry_organization"."id" = %s
  96. LIMIT 21"
  97. `);
  98. });
  99. it('Reflows avoid unnecessary newlines', () => {
  100. expect(
  101. formatter.toString(
  102. 'SELECT "sentry_team"."org_role" FROM "sentry_team" INNER JOIN "sentry_organizationmember_teams" ON ("sentry_team"."id" = "sentry_organizationmember_teams"."team_id" WHERE ( "sentry_organizationmember_teams"."organizationmember_id" = %s AND NOT ("sentry_team"."org_role" IS NULL)'
  103. )
  104. ).toMatchInlineSnapshot(`
  105. "SELECT "sentry_team"."org_role"
  106. FROM "sentry_team"
  107. INNER JOIN "sentry_organizationmember_teams" ON ("sentry_team"."id" =
  108. "sentry_organizationmember_teams"."team_id"
  109. WHERE (
  110. "sentry_organizationmember_teams"."organizationmember_id" = %s AND NOT ("sentry_team"."org_role" IS
  111. NULL)"
  112. `);
  113. });
  114. });
  115. describe('SQLishFormatter.toSimpleMarkup()', () => {
  116. const formatter = new SQLishFormatter();
  117. const getMarkup = (markup: any): string => {
  118. const {container} = render(<Fragment>{markup}</Fragment>);
  119. return container.innerHTML;
  120. };
  121. beforeEach(() => {
  122. // The renderer throws an error because elements in the list do not have
  123. // a `"key"` prop, but that's intentional. The list elements are spans
  124. // with no semantic meaning, and their keys are not meaningful.
  125. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  126. });
  127. it('Capitalizes keywords', () => {
  128. expect(getMarkup(formatter.toSimpleMarkup('select hello'))).toMatchInlineSnapshot(
  129. `"<b>SELECT</b><span> </span><span>hello</span>"`
  130. );
  131. });
  132. it('Wraps every token in a `<span>` element', () => {
  133. expect(getMarkup(formatter.toSimpleMarkup('SELECT hello;'))).toMatchInlineSnapshot(
  134. `"<b>SELECT</b><span> </span><span>hello;</span>"`
  135. );
  136. });
  137. });
  138. });