SQLishFormatter.spec.tsx 5.5 KB

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