Files
supabase/apps/studio/tests/features/logs/Logs.utils.test.ts
T
3c588294a6 Improve Integration Test Setup and re-add LogPreviewer tests (#35358)
* fix logs previewer and msw

* refactor api mocking for better dx

* update readme

* comment out error handler for vitest

* rm unnecessary tests

* fix custom render nuqs type

* add logs search test

* rm unnecessary import

* update readme with customRender and customRednerHook

* rm unnecessary api handler

* Move the NODE_ENV to the studio build command in turbo.json.

* Update apps/studio/tests/README.md

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* add cursor rule

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-05-06 09:50:56 +02:00

99 lines
3.8 KiB
TypeScript

import {
checkForILIKEClause,
checkForWildcard,
checkForWithClause,
} from 'components/interfaces/Settings/Logs/Logs.utils'
import { describe, test, expect } from 'vitest'
describe('checkForWithClause', () => {
test('basic queries', () => {
expect(checkForWithClause('SELECT * FROM table')).toBe(false)
expect(checkForWithClause('SELECT * FROM table WITH clause')).toBe(true)
expect(checkForWithClause('WITH test AS (SELECT * FROM table) SELECT * FROM test')).toBe(true)
expect(checkForWithClause('SELECT * FROM withsomething')).toBe(false)
})
test('case sensitivity', () => {
expect(checkForWithClause('with test AS (SELECT * FROM table) SELECT * FROM test')).toBe(true)
expect(checkForWithClause('WiTh test AS (SELECT * FROM table) SELECT * FROM test')).toBe(true)
})
test('comments', () => {
expect(checkForWithClause('SELECT * FROM table -- WITH clause')).toBe(false)
expect(checkForWithClause('SELECT * FROM table /* WITH clause */')).toBe(false)
expect(checkForWithClause('-- WITH clause\nSELECT * FROM table')).toBe(false)
expect(checkForWithClause('/* WITH clause */\nSELECT * FROM table')).toBe(false)
})
test('string literals', () => {
expect(checkForWithClause("SELECT 'WITH' FROM table")).toBe(false)
expect(checkForWithClause("SELECT * FROM table WHERE column = 'WITH clause'")).toBe(false)
})
test('subqueries', () => {
expect(
checkForWithClause('SELECT * FROM (WITH subquery AS (SELECT 1) SELECT * FROM subquery)')
).toBe(true)
expect(
checkForWithClause(
'SELECT * FROM table WHERE column IN (WITH subquery AS (SELECT 1) SELECT * FROM subquery)'
)
).toBe(true)
})
})
describe('checkForILIKEClause', () => {
test('basic queries', () => {
expect(checkForILIKEClause('SELECT * FROM table')).toBe(false)
expect(checkForILIKEClause('SELECT * FROM table WHERE column ILIKE "%value%"')).toBe(true)
expect(checkForILIKEClause('SELECT * FROM table WHERE column LIKE "%value%"')).toBe(false)
expect(checkForILIKEClause('SELECT * FROM ilikesomething')).toBe(false)
})
test('case sensitivity', () => {
expect(checkForILIKEClause('SELECT * FROM table WHERE column ilike "%value%"')).toBe(true)
expect(checkForILIKEClause('SELECT * FROM table WHERE column IlIkE "%value%"')).toBe(true)
})
test('comments', () => {
expect(checkForILIKEClause('SELECT * FROM table -- ILIKE clause')).toBe(false)
expect(checkForILIKEClause('SELECT * FROM table /* ILIKE clause */')).toBe(false)
expect(checkForILIKEClause('-- ILIKE clause\nSELECT * FROM table')).toBe(false)
expect(checkForILIKEClause('/* ILIKE clause */\nSELECT * FROM table')).toBe(false)
})
test('string literals', () => {
expect(checkForILIKEClause("SELECT 'ILIKE' FROM table")).toBe(false)
expect(checkForILIKEClause("SELECT * FROM table WHERE column = 'ILIKE clause'")).toBe(false)
})
test('subqueries', () => {
expect(
checkForILIKEClause('SELECT * FROM (SELECT * FROM table WHERE column ILIKE "%value%")')
).toBe(true)
expect(
checkForILIKEClause(
'SELECT * FROM table WHERE column IN (SELECT * FROM subtable WHERE column ILIKE "%value%")'
)
).toBe(true)
})
})
describe('checkForWildcard', () => {
test('basic queries', () => {
expect(checkForWildcard('SELECT * FROM table')).toBe(true)
expect(checkForWildcard('SELECT column FROM table')).toBe(false)
})
test('comments', () => {
expect(checkForWildcard('SELECT column FROM table -- *')).toBe(false)
expect(checkForWildcard('SELECT column FROM table /* * */')).toBe(false)
expect(checkForWildcard('-- *\nSELECT column FROM table')).toBe(false)
expect(checkForWildcard('/* * */\nSELECT column FROM table')).toBe(false)
})
test('count(*)', () => {
expect(checkForWildcard('SELECT count(*) FROM table')).toBe(false)
})
})