mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 03:15:06 +03:00
108 lines
3.8 KiB
TypeScript
108 lines
3.8 KiB
TypeScript
import { snapshot } from 'valtio'
|
|
import { beforeEach, describe, expect, it } from 'vitest'
|
|
|
|
import { sqlEditorSessionState } from './sql-editor-session-state'
|
|
|
|
// The session store is a module singleton, so reset its mutable fields before
|
|
// each test to keep cases independent.
|
|
beforeEach(() => {
|
|
sqlEditorSessionState.results = {}
|
|
sqlEditorSessionState.explainResults = {}
|
|
sqlEditorSessionState.limit = 100
|
|
})
|
|
|
|
describe('sqlEditorSessionState', () => {
|
|
describe('results', () => {
|
|
it('addResult stores rows (and optional autoLimit) keyed by snippet id', () => {
|
|
sqlEditorSessionState.addResult('a', [{ x: 1 }], 50)
|
|
|
|
const result = sqlEditorSessionState.results['a']?.[0]
|
|
expect(result?.rows).toEqual([{ x: 1 }])
|
|
expect(result?.autoLimit).toBe(50)
|
|
expect(result?.error).toBeUndefined()
|
|
})
|
|
|
|
it('addResult stores Warehouse result metadata', () => {
|
|
sqlEditorSessionState.addResult('a', [{ x: 1 }], undefined, {
|
|
type: 'search_path',
|
|
schema: 'public_warehouse',
|
|
})
|
|
|
|
const result = sqlEditorSessionState.results['a']?.[0]
|
|
expect(result?.warehouseResultSource).toEqual({
|
|
type: 'search_path',
|
|
schema: 'public_warehouse',
|
|
})
|
|
})
|
|
|
|
it('addResultError stores the error with empty rows', () => {
|
|
sqlEditorSessionState.addResultError('a', { message: 'boom' }, 25)
|
|
|
|
const result = sqlEditorSessionState.results['a']?.[0]
|
|
expect(result?.rows).toEqual([])
|
|
expect(result?.error).toEqual({ message: 'boom' })
|
|
expect(result?.autoLimit).toBe(25)
|
|
})
|
|
|
|
it('resetResult clears the rows for a snippet', () => {
|
|
sqlEditorSessionState.addResult('a', [{ x: 1 }])
|
|
sqlEditorSessionState.resetResult('a')
|
|
|
|
expect(sqlEditorSessionState.results['a']).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('explain results', () => {
|
|
it('addExplainResult stores rows keyed by snippet id', () => {
|
|
const rows = [{ 'QUERY PLAN': 'Seq Scan' }]
|
|
sqlEditorSessionState.addExplainResult('a', rows)
|
|
|
|
expect(sqlEditorSessionState.explainResults['a']?.rows).toEqual(rows)
|
|
expect(sqlEditorSessionState.explainResults['a']?.error).toBeUndefined()
|
|
})
|
|
|
|
it('addExplainResultError stores the error with empty rows', () => {
|
|
sqlEditorSessionState.addExplainResultError('a', { message: 'bad plan' })
|
|
|
|
expect(sqlEditorSessionState.explainResults['a']?.rows).toEqual([])
|
|
expect(sqlEditorSessionState.explainResults['a']?.error).toEqual({ message: 'bad plan' })
|
|
})
|
|
})
|
|
|
|
describe('resetResults', () => {
|
|
it('clears both the query result and the explain result for a snippet', () => {
|
|
sqlEditorSessionState.addResult('a', [{ x: 1 }])
|
|
sqlEditorSessionState.addExplainResult('a', [{ 'QUERY PLAN': 'Seq Scan' }])
|
|
|
|
sqlEditorSessionState.resetResults('a')
|
|
|
|
expect(sqlEditorSessionState.results['a']).toEqual([])
|
|
expect(sqlEditorSessionState.explainResults['a']).toEqual({ rows: [] })
|
|
})
|
|
})
|
|
|
|
describe('clearForSnippet', () => {
|
|
it('deletes both results and explain results for a snippet, leaving others intact', () => {
|
|
sqlEditorSessionState.addResult('a', [{ x: 1 }])
|
|
sqlEditorSessionState.addExplainResult('a', [{ 'QUERY PLAN': 'Seq Scan' }])
|
|
sqlEditorSessionState.addResult('b', [{ y: 2 }])
|
|
|
|
sqlEditorSessionState.clearForSnippet('a')
|
|
|
|
expect(sqlEditorSessionState.results['a']).toBeUndefined()
|
|
expect(sqlEditorSessionState.explainResults['a']).toBeUndefined()
|
|
expect(sqlEditorSessionState.results['b']?.[0]?.rows).toEqual([{ y: 2 }])
|
|
})
|
|
})
|
|
|
|
describe('limit', () => {
|
|
it('defaults to 100 and is updated by setLimit', () => {
|
|
expect(snapshot(sqlEditorSessionState).limit).toBe(100)
|
|
|
|
sqlEditorSessionState.setLimit(500)
|
|
|
|
expect(snapshot(sqlEditorSessionState).limit).toBe(500)
|
|
})
|
|
})
|
|
})
|