mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Context
_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_
We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.
## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
189 lines
6.2 KiB
TypeScript
189 lines
6.2 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { QueryPerformanceSort } from './QueryPerformance.types'
|
|
import { generateQueryPerformanceSql } from './useQueryPerformanceQuery'
|
|
|
|
describe('generateQueryPerformanceSql', () => {
|
|
it('generates sql with no filters', () => {
|
|
const result = generateQueryPerformanceSql({ preset: 'mostFrequentlyInvoked' })
|
|
expect(result.sql).toBeDefined()
|
|
expect(result.whereSql).toBe('')
|
|
expect(result.orderBySql).toBeUndefined()
|
|
})
|
|
|
|
it('generates ORDER BY clause', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
orderBy: { column: 'calls', order: 'desc' },
|
|
})
|
|
expect(result.orderBySql).toBe('ORDER BY calls desc')
|
|
})
|
|
|
|
it('filters by roles', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
roles: ['postgres', 'anon'],
|
|
})
|
|
expect(result.whereSql).toContain("auth.rolname in ('postgres', 'anon')")
|
|
})
|
|
|
|
it('filters by search query', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
searchQuery: 'SELECT',
|
|
})
|
|
expect(result.whereSql).toContain("statements.query ~* 'SELECT'")
|
|
})
|
|
|
|
it('filters by dashboard source only', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
sources: ['dashboard'],
|
|
})
|
|
expect(result.whereSql).toContain("statements.query ~* 'source: dashboard'")
|
|
})
|
|
|
|
it('filters by non-dashboard source only', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
sources: ['non-dashboard'],
|
|
})
|
|
expect(result.whereSql).toContain("statements.query !~* 'source: dashboard'")
|
|
})
|
|
|
|
it('does not add source filter when both sources are selected', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
sources: ['dashboard', 'non-dashboard'],
|
|
})
|
|
expect(result.whereSql).not.toContain('source: dashboard')
|
|
})
|
|
|
|
it('filters by minimum calls', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
minCalls: 10,
|
|
})
|
|
expect(result.whereSql).toContain('statements.calls >= 10')
|
|
})
|
|
|
|
it('does not filter by minCalls when 0', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
minCalls: 0,
|
|
})
|
|
expect(result.whereSql).not.toContain('statements.calls')
|
|
})
|
|
|
|
it('filters by minimum total time', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
minTotalTime: 500,
|
|
})
|
|
expect(result.whereSql).toContain(
|
|
'(statements.total_exec_time + statements.total_plan_time) >= 500'
|
|
)
|
|
})
|
|
|
|
it('combines multiple WHERE conditions with AND', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
roles: ['postgres'],
|
|
searchQuery: 'SELECT',
|
|
minCalls: 5,
|
|
})
|
|
expect(result.whereSql).toContain("auth.rolname in ('postgres')")
|
|
expect(result.whereSql).toContain("statements.query ~* 'SELECT'")
|
|
expect(result.whereSql).toContain('statements.calls >= 5')
|
|
expect(result.whereSql.split(' AND ').length).toBe(3)
|
|
})
|
|
|
|
it('passes WHERE clause to base sql function', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
roles: ['postgres'],
|
|
})
|
|
expect(result.sql).toContain('WHERE')
|
|
})
|
|
|
|
it.each([
|
|
'mostFrequentlyInvoked',
|
|
'mostTimeConsuming',
|
|
'slowestExecutionTime',
|
|
'queryHitRate',
|
|
'unified',
|
|
] as const)('generates sql for the %s preset', (preset) => {
|
|
expect(generateQueryPerformanceSql({ preset }).sql.length).toBeGreaterThan(0)
|
|
})
|
|
|
|
it.each([0, -5])('clamps page=%s to page=1 (no negative offset)', (page) => {
|
|
const result = generateQueryPerformanceSql({ preset: 'unified', page, pageSize: 20 })
|
|
expect(result.sql).toContain('offset 0')
|
|
expect(result.sql).not.toMatch(/offset -\d/)
|
|
})
|
|
|
|
it('clamps pageSize above 100 to 100', () => {
|
|
const result = generateQueryPerformanceSql({ preset: 'unified', page: 1, pageSize: 9999 })
|
|
expect(result.sql).toContain('limit 100')
|
|
expect(result.sql).not.toContain('limit 9999')
|
|
})
|
|
|
|
it('applies LIMIT and OFFSET for page 2', () => {
|
|
const result = generateQueryPerformanceSql({ preset: 'unified', page: 2, pageSize: 20 })
|
|
expect(result.sql).toContain('limit 20 offset 20')
|
|
})
|
|
|
|
it('does not produce NaN in SQL when page is NaN', () => {
|
|
const result = generateQueryPerformanceSql({ preset: 'unified', page: NaN, pageSize: 20 })
|
|
expect(result.sql).not.toContain('NaN')
|
|
expect(result.sql).toContain('offset 0')
|
|
})
|
|
|
|
it('does not produce NaN in SQL when pageSize is NaN', () => {
|
|
const result = generateQueryPerformanceSql({ preset: 'unified', page: 1, pageSize: NaN })
|
|
expect(result.sql).not.toContain('NaN')
|
|
expect(result.sql).toContain('limit 20')
|
|
})
|
|
})
|
|
|
|
describe('generateQueryPerformanceSql - ORDER BY column validation', () => {
|
|
it('rejects SQL injection in orderBy column', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
orderBy: { column: 'calls; DROP TABLE users--' as any, order: 'asc' },
|
|
})
|
|
expect(result.orderBySql).toBeUndefined()
|
|
expect(result.sql).not.toContain('DROP TABLE')
|
|
})
|
|
|
|
it('rejects an orderBy column containing a colon (old URL format) and falls back to the default', () => {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
orderBy: { column: 'created_at:asc' as any, order: 'asc' },
|
|
})
|
|
expect(result.orderBySql).toBeUndefined()
|
|
expect(result.sql).toContain('order by statements.calls desc')
|
|
})
|
|
|
|
it('accepts all valid sort columns', () => {
|
|
const columns: QueryPerformanceSort['column'][] = [
|
|
'query',
|
|
'rolname',
|
|
'total_time',
|
|
'prop_total_time',
|
|
'calls',
|
|
'avg_rows',
|
|
'max_time',
|
|
'mean_time',
|
|
'min_time',
|
|
]
|
|
for (const column of columns) {
|
|
const result = generateQueryPerformanceSql({
|
|
preset: 'mostFrequentlyInvoked',
|
|
orderBy: { column, order: 'asc' },
|
|
})
|
|
expect(result.orderBySql).toBe(`ORDER BY ${column} asc`)
|
|
}
|
|
})
|
|
})
|