QueryInsightsTable.utils.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. formatDuration,
  4. formatQueryDisplay,
  5. getColumnName,
  6. getQueryType,
  7. getTableName,
  8. } from './QueryInsightsTable.utils'
  9. describe('formatDuration', () => {
  10. it('returns ms for values under 1000', () => {
  11. expect(formatDuration(0)).toBe('0ms')
  12. expect(formatDuration(500)).toBe('500ms')
  13. expect(formatDuration(999)).toBe('999ms')
  14. })
  15. it('delegates to formatDurationLong for values >= 1000', () => {
  16. expect(formatDuration(1000)).toBe('1.00s')
  17. expect(formatDuration(60000)).toBe('1m')
  18. })
  19. })
  20. describe('getQueryType', () => {
  21. it('returns null for empty/null/undefined input', () => {
  22. expect(getQueryType(null)).toBeNull()
  23. expect(getQueryType(undefined)).toBeNull()
  24. expect(getQueryType('')).toBeNull()
  25. })
  26. it('returns the SQL keyword for standard statement types', () => {
  27. expect(getQueryType('SELECT * FROM users')).toBe('SELECT')
  28. expect(getQueryType('INSERT INTO orders VALUES (1)')).toBe('INSERT')
  29. expect(getQueryType('UPDATE users SET name = $1')).toBe('UPDATE')
  30. expect(getQueryType('DELETE FROM logs')).toBe('DELETE')
  31. expect(getQueryType('CREATE TABLE foo (id int)')).toBe('CREATE')
  32. expect(getQueryType('DROP TABLE foo')).toBe('DROP')
  33. expect(getQueryType('ALTER TABLE foo ADD COLUMN bar text')).toBe('ALTER')
  34. expect(getQueryType('TRUNCATE foo')).toBe('TRUNCATE')
  35. })
  36. it('returns WITH for simple CTEs', () => {
  37. expect(getQueryType('WITH cte AS (SELECT 1) SELECT * FROM cte')).toBe('WITH')
  38. })
  39. it('is case-insensitive', () => {
  40. expect(getQueryType('select * from users')).toBe('SELECT')
  41. expect(getQueryType('insert into foo values (1)')).toBe('INSERT')
  42. })
  43. })
  44. describe('getTableName', () => {
  45. it('returns null for empty/null/undefined input', () => {
  46. expect(getTableName(null)).toBeNull()
  47. expect(getTableName(undefined)).toBeNull()
  48. expect(getTableName('')).toBeNull()
  49. })
  50. it('extracts table from SELECT FROM', () => {
  51. expect(getTableName('SELECT * FROM users')).toBe('users')
  52. expect(getTableName('SELECT id FROM public.orders WHERE id = 1')).toBe('orders')
  53. })
  54. it('extracts table from INSERT INTO', () => {
  55. expect(getTableName('INSERT INTO orders (id) VALUES (1)')).toBe('orders')
  56. })
  57. it('extracts table from UPDATE', () => {
  58. expect(getTableName('UPDATE users SET name = $1 WHERE id = 1')).toBe('users')
  59. })
  60. it('extracts table from DELETE FROM', () => {
  61. expect(getTableName('DELETE FROM logs WHERE id = 1')).toBe('logs')
  62. })
  63. it('extracts table from CREATE TABLE', () => {
  64. expect(getTableName('CREATE TABLE foo (id int)')).toBe('foo')
  65. expect(getTableName('CREATE TABLE IF NOT EXISTS bar (id int)')).toBe('bar')
  66. })
  67. it('extracts table from ALTER TABLE', () => {
  68. expect(getTableName('ALTER TABLE users ADD COLUMN email text')).toBe('users')
  69. })
  70. it('extracts table from DROP TABLE', () => {
  71. expect(getTableName('DROP TABLE IF EXISTS old_table')).toBe('old_table')
  72. })
  73. it('extracts table from TRUNCATE', () => {
  74. expect(getTableName('TRUNCATE TABLE logs')).toBe('logs')
  75. expect(getTableName('TRUNCATE logs')).toBe('logs')
  76. })
  77. it('strips schema prefix', () => {
  78. expect(getTableName('SELECT * FROM public.users')).toBe('users')
  79. })
  80. it('strips quotes', () => {
  81. expect(getTableName('SELECT * FROM "my_table"')).toBe('my_table')
  82. })
  83. })
  84. describe('getColumnName', () => {
  85. it('returns null for empty/null/undefined input', () => {
  86. expect(getColumnName(null)).toBeNull()
  87. expect(getColumnName(undefined)).toBeNull()
  88. expect(getColumnName('')).toBeNull()
  89. })
  90. it('extracts column from WHERE clause', () => {
  91. expect(getColumnName('SELECT * FROM users WHERE id = 1')).toBe('id')
  92. })
  93. it('extracts column from ORDER BY when no WHERE clause', () => {
  94. expect(getColumnName('SELECT * FROM users ORDER BY created_at')).toBe('created_at')
  95. })
  96. it('extracts column from GROUP BY', () => {
  97. expect(getColumnName('SELECT status, count(*) FROM orders GROUP BY status')).toBe('status')
  98. })
  99. it('extracts column from UPDATE SET when no WHERE clause', () => {
  100. expect(getColumnName('UPDATE users SET email = $1')).toBe('email')
  101. })
  102. it('extracts first column from INSERT INTO', () => {
  103. expect(getColumnName('INSERT INTO users (id, name) VALUES ($1, $2)')).toBe('id')
  104. })
  105. })
  106. describe('formatQueryDisplay', () => {
  107. it('formats all three parts', () => {
  108. expect(formatQueryDisplay('SELECT', 'users', 'id')).toBe('SELECT in users, id')
  109. })
  110. it('uses dash placeholders for null values', () => {
  111. expect(formatQueryDisplay(null, null, null)).toBe('– in –, –')
  112. expect(formatQueryDisplay('SELECT', null, null)).toBe('SELECT in –, –')
  113. })
  114. })