Files
supabase/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.test.ts
Joshen Lim ae8dafd0a4 Chore/optimizations for table editor query (#27553)
* Optimize table editor select query when cutting off column values

* Fix auto limit logic to consider offset
2024-06-26 17:15:20 +08:00

130 lines
5.3 KiB
TypeScript

import { checkIfAppendLimitRequired, suffixWithLimit } from './SQLEditor.utils'
describe('SQLEditor.utils.ts:checkIfAppendLimitRequired', () => {
test('Should return false if limit passed is <= 0', () => {
const sql = 'select * from countries;'
const limit = -1
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return true if limit passed is > 0', () => {
const sql = 'select * from countries;'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(true)
})
test('Should return false if query already has a limit', () => {
const sql = 'select * from countries limit 10;'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query already has a limit (check for case-insensitiveness)', () => {
const sql = 'SELECT * FROM countries LIMIT 10;'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query already has a limit and offset', () => {
const sql = 'select * from countries limit 10 offset 0;'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query already has a limit and offset (flip order of limit and offset)', () => {
const sql = 'select * from countries offset 0 limit 1;'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query already has a limit, even if no value provided for limit', () => {
const sql = 'select * from countries limit'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query uses `FETCH FIRST` instead of limit ', () => {
const sql = 'select * from countries FETCH FIRST 5 rows only'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query uses `fetch first` instead of limit ', () => {
const sql = 'select * from countries fetch first 5 rows only'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query uses `fetch first` (with random spaces) instead of limit ', () => {
const sql = 'select * from countries FETCH FIRST 5 rows only'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query is not a select statement', () => {
const sql = 'create table test (id int8 primary key, name varchar);'
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if there are multiple queries I', () => {
const sql1 = `
select * from countries;
select * from cities;
`.trim()
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql1, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if there are multiple queries II', () => {
const sql1 = `
select * from countries;
select * from cities
`.trim()
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql1, limit)
expect(appendAutoLimit).toBe(false)
})
// [Joshen] Opting to just avoid appending in this case to prevent making the logic overly complex atm
test('Should return false if query has with a comment I', () => {
const sql = `
-- This is a comment
select * from cities
`.trim()
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
test('Should return false if query has with a comment II', () => {
const sql = `
select * from cities
-- This is a comment
`.trim()
const limit = 100
const { appendAutoLimit } = checkIfAppendLimitRequired(sql, limit)
expect(appendAutoLimit).toBe(false)
})
})
// [Joshen] These will just need to test the cases when appendAutoLimit returns true then
describe('SQLEditor.utils.ts:suffixWithLimit', () => {
test('Should add the limit param properly if query ends without a semi colon', () => {
const sql = 'select * from countries'
const limit = 100
const formattedSql = suffixWithLimit(sql, limit)
expect(formattedSql).toBe('select * from countries limit 100;')
})
test('Should add the limit param properly if query ends with a semi colon', () => {
const sql = 'select * from countries;'
const limit = 100
const formattedSql = suffixWithLimit(sql, limit)
expect(formattedSql).toBe('select * from countries limit 100;')
})
test('Should add the limit param properly if query ends with multiple semi colon', () => {
const sql = 'select * from countries;;;;;;;'
const limit = 100
const formattedSql = suffixWithLimit(sql, limit)
expect(formattedSql).toBe('select * from countries limit 100;')
})
})