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