mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +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
171 lines
6.5 KiB
TypeScript
171 lines
6.5 KiB
TypeScript
import { safeSql, type SafeSqlFragment } from '@supabase/pg-meta'
|
|
import { describe, expect, it, test } from 'vitest'
|
|
|
|
import { applyAutoLimit, getSqlErrorLines, trimTrailingSemicolons } from '../utils'
|
|
|
|
describe('getSqlErrorLines', () => {
|
|
it('returns formattedError lines when present', () => {
|
|
const lines = getSqlErrorLines({
|
|
message: 'permission denied for table users',
|
|
formattedError:
|
|
'ERROR: 42501: permission denied for table users\n' +
|
|
'HINT: To grant access to anon on a specific table:\n' +
|
|
' GRANT SELECT ON TABLE public.users TO anon;',
|
|
})
|
|
|
|
expect(lines).toEqual([
|
|
'ERROR: 42501: permission denied for table users',
|
|
'HINT: To grant access to anon on a specific table:',
|
|
' GRANT SELECT ON TABLE public.users TO anon;',
|
|
])
|
|
})
|
|
|
|
it('strips empty lines from formattedError', () => {
|
|
const lines = getSqlErrorLines({
|
|
formattedError: 'ERROR: boom\n\nHINT: retry\n',
|
|
})
|
|
|
|
expect(lines).toEqual(['ERROR: boom', 'HINT: retry'])
|
|
})
|
|
|
|
it('falls back to message lines when formattedError is missing and message is multi-line', () => {
|
|
const lines = getSqlErrorLines({
|
|
message:
|
|
'ERROR: 42501: permission denied for table users\n' +
|
|
'HINT: To grant access to anon on a specific table:\n' +
|
|
' GRANT SELECT ON TABLE public.users TO anon;',
|
|
})
|
|
|
|
expect(lines).toEqual([
|
|
'ERROR: 42501: permission denied for table users',
|
|
'HINT: To grant access to anon on a specific table:',
|
|
' GRANT SELECT ON TABLE public.users TO anon;',
|
|
])
|
|
})
|
|
|
|
// A single-line (or empty) message returns no lines so callers render their fallback.
|
|
it.each([
|
|
['a single-line message', { message: 'permission denied for table users' }],
|
|
['both fields missing', {}],
|
|
['an empty message', { message: '' }],
|
|
['a message of only newlines', { message: '\n\n' }],
|
|
])('returns an empty array for %s', (_, error) => {
|
|
expect(getSqlErrorLines(error)).toEqual([])
|
|
})
|
|
|
|
it('prefers formattedError even when message is also multi-line', () => {
|
|
const lines = getSqlErrorLines({
|
|
message: 'message line 1\nmessage line 2',
|
|
formattedError: 'formatted line 1\nformatted line 2',
|
|
})
|
|
|
|
expect(lines).toEqual(['formatted line 1', 'formatted line 2'])
|
|
})
|
|
|
|
it('falls through to message when formattedError is empty string', () => {
|
|
const lines = getSqlErrorLines({
|
|
message: 'ERROR: line 1\nHINT: line 2',
|
|
formattedError: '',
|
|
})
|
|
|
|
expect(lines).toEqual(['ERROR: line 1', 'HINT: line 2'])
|
|
})
|
|
})
|
|
|
|
describe('trimTrailingSemicolons', () => {
|
|
test('removes a single trailing semicolon', () => {
|
|
const sql = safeSql`select * from countries;`
|
|
expect(trimTrailingSemicolons(sql)).toBe('select * from countries')
|
|
})
|
|
test('removes multiple trailing semicolons', () => {
|
|
const sql = safeSql`select * from countries;;;;;;;`
|
|
expect(trimTrailingSemicolons(sql)).toBe('select * from countries')
|
|
})
|
|
test('leaves a fragment with no trailing semicolon unchanged', () => {
|
|
const sql = safeSql`select * from countries`
|
|
expect(trimTrailingSemicolons(sql)).toBe('select * from countries')
|
|
})
|
|
test('does not touch semicolons that are not trailing', () => {
|
|
const sql = safeSql`select 1; select 2`
|
|
expect(trimTrailingSemicolons(sql)).toBe('select 1; select 2')
|
|
})
|
|
})
|
|
|
|
describe('applyAutoLimit', () => {
|
|
test('does not append when the limit passed is <= 0', () => {
|
|
expect(applyAutoLimit(safeSql`select * from countries;`, -1).appendAutoLimit).toBe(false)
|
|
})
|
|
|
|
test.each([
|
|
['without a semicolon', 'select * from countries'],
|
|
['with a semicolon', 'select * from countries;'],
|
|
['with multiple semicolons', 'select * from countries;;;;;;;'],
|
|
])('appends the limit to a query %s', (_, query) => {
|
|
expect(applyAutoLimit(query as SafeSqlFragment, 100)).toEqual({
|
|
sql: 'select * from countries limit 100;',
|
|
appendAutoLimit: true,
|
|
})
|
|
})
|
|
|
|
test('appends the limit to a CTE query', () => {
|
|
const sql = safeSql`WITH numbers AS (SELECT generate_series(1, 2000) AS n) SELECT n FROM numbers;`
|
|
const result = applyAutoLimit(sql, 100)
|
|
expect(result.appendAutoLimit).toBe(true)
|
|
expect(result.sql).toBe(
|
|
'WITH numbers AS (SELECT generate_series(1, 2000) AS n) SELECT n FROM numbers limit 100;'
|
|
)
|
|
})
|
|
|
|
test.each([
|
|
[
|
|
'a recursive CTE whose inner query has its own limit',
|
|
'with recursive t as (select 1 as n limit 5) select * from t',
|
|
],
|
|
[
|
|
'a read-only CTE whose literals or identifiers contain write keywords',
|
|
`with t as (select 'update' as "delete") select * from t`,
|
|
],
|
|
])('appends the limit to %s', (_, query) => {
|
|
expect(applyAutoLimit(query as SafeSqlFragment, 100).appendAutoLimit).toBe(true)
|
|
})
|
|
|
|
test.each([
|
|
// Existing limits, in any casing/whitespace/order, including LIMIT ALL / NULL
|
|
'select * from countries limit 10;',
|
|
'SELECT * FROM countries LIMIT 10;',
|
|
'select * from countries limit 10 ;',
|
|
'select * from countries limit 10 offset 0;',
|
|
'select * from countries limit 10 offset 0 ;',
|
|
'select * from countries offset 0 limit 1;',
|
|
'select * from countries limit',
|
|
'select * from countries limit all;',
|
|
'select * from countries limit all',
|
|
'SELECT * FROM countries LIMIT ALL ;',
|
|
'select * from countries limit null;',
|
|
'select * from countries limit all offset 5;',
|
|
'select * from countries offset 5 limit null',
|
|
'with t as (select 1) select * from t limit 10',
|
|
// FETCH FIRST / NEXT instead of limit
|
|
'select * from countries FETCH FIRST 5 rows only',
|
|
'select * from countries fetch first 5 rows only',
|
|
'select * from countries offset 5 rows FETCH NEXT 5 rows only',
|
|
// CTEs that write
|
|
'with d as (delete from t returning *) select * from d',
|
|
'with s as (select 1) insert into t select * from s',
|
|
'with s as (select 1) update t set a = 1',
|
|
'with t as (select 1) select * into new_t from t',
|
|
// Non-select statements and multiple statements
|
|
'create table test (id int8 primary key, name varchar);',
|
|
'select * from countries;\nselect * from cities;',
|
|
'select * from countries;\nselect * from cities',
|
|
// [Joshen] Opting to just avoid appending with comments to keep the logic simple atm
|
|
'-- This is a comment\nselect * from cities',
|
|
'select * from cities\n-- This is a comment',
|
|
])('leaves the query unchanged: %s', (query) => {
|
|
expect(applyAutoLimit(query as SafeSqlFragment, 100)).toEqual({
|
|
sql: query,
|
|
appendAutoLimit: false,
|
|
})
|
|
})
|
|
})
|